fix: sonarcloud warnings (#2804)

* fix: break will never be executed

* refactor: compile the name fallback string

* fix: replace redundant type with auto

* fix: use init-statement to reduce variable scope

* fix: implicit conversion loses precision

* fix: use in-class initializer

* fix: make variable a pointer-to-const

* fix: local variable name shadows class variable

* fix: implicit conversion may lose precision
This commit is contained in:
Charles Kerr 2022-03-24 00:18:41 -05:00 committed by GitHub
parent 6db3fbfe64
commit b8c3778cf4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 26 additions and 28 deletions

View File

@ -186,11 +186,9 @@ static std::optional<std::string> getErrorString(tr_stat const* st)
{
case TR_STAT_TRACKER_WARNING:
return fmt::format(_("Tracker warning: '{warning}'"), fmt::arg("warning", st->errorString));
break;
case TR_STAT_TRACKER_ERROR:
return fmt::format(_("Tracker Error: '{error}'"), fmt::arg("error", st->errorString));
break;
case TR_STAT_LOCAL_ERROR:
return fmt::format(_("Local error: '{error}'"), fmt::arg("error", st->errorString));

View File

@ -234,7 +234,7 @@ static int cacheTrim(tr_cache* cache)
static int getMaxBlocks(int64_t max_bytes)
{
return max_bytes / static_cast<double>(tr_block_info::BlockSize);
return std::lldiv(max_bytes, tr_block_info::BlockSize).quot;
}
int tr_cacheSetLimit(tr_cache* cache, int64_t max_bytes)

View File

@ -14,6 +14,8 @@
#include <event2/buffer.h>
#include <fmt/core.h>
#include <fmt/compile.h>
#include <fmt/format.h>
#include "transmission.h"
@ -248,7 +250,7 @@ void tr_logAddMessage(char const* file, int line, tr_log_level level, std::strin
if (std::empty(name))
{
auto const base = tr_sys_path_basename(file);
name_fallback = fmt::format("{}:{}", !std::empty(base) ? base : "?", line);
name_fallback = fmt::format(FMT_COMPILE("{}:{}"), !std::empty(base) ? base : "?", line);
name = name_fallback;
}

View File

@ -2506,7 +2506,7 @@ static void enforceSessionPeerLimit(tr_session* session)
// make a list of all the peers
auto peers = std::vector<tr_peer*>{};
peers.reserve(n_peers);
for (auto* const tor : session->torrents())
for (auto const* const tor : session->torrents())
{
size_t const n = tr_ptrArraySize(&tor->swarm->peers);
auto** base = (tr_peer**)tr_ptrArrayBase(&tor->swarm->peers);

View File

@ -2067,8 +2067,11 @@ static char const* sessionStats(
auto cumulativeStats = tr_session_stats{};
auto const& torrents = session->torrents();
int const total = std::size(torrents);
int const running = std::count_if(std::begin(torrents), std::end(torrents), [](auto const* tor) { return tor->isRunning; });
auto const total = std::size(torrents);
auto const running = std::count_if(
std::begin(torrents),
std::end(torrents),
[](auto const* tor) { return tor->isRunning; });
tr_sessionGetStats(session, &currentStats);
tr_sessionGetCumulativeStats(session, &cumulativeStats);

View File

@ -2096,14 +2096,15 @@ bool tr_torrent::setTrackerList(std::string_view text)
// magnet links
if (!has_metadata)
{
tr_error* error = nullptr;
if (!tr_ctorSaveMagnetContents(this, this->magnetFile(), &error))
tr_error* save_error = nullptr;
if (!tr_ctorSaveMagnetContents(this, this->magnetFile(), &save_error))
{
this->setLocalError(fmt::format(
_("Couldn't save '{path}': {error} ({error_code})"),
fmt::arg("path", this->magnetFile()),
fmt::arg("error", error->message),
fmt::arg("error_code", error->code)));
fmt::arg("error", save_error->message),
fmt::arg("error_code", save_error->code)));
tr_error_clear(&save_error);
}
}

View File

@ -42,16 +42,6 @@ struct CompareTorrentByHash
} // namespace
tr_torrents::tr_torrents()
// Insert an empty pointer at by_id_[0] to ensure that the first added
// torrent doesn't get an ID of 0; ie, that every torrent has a positive
// ID number. This constraint isn't needed by libtransmission code but
// the ID is exported in the RPC API to 3rd party clients that may be
// testing for >0 as a validity check.
: by_id_{ nullptr }
{
}
tr_torrent const* tr_torrents::get(int id) const
{
TR_ASSERT(0 < id);
@ -108,7 +98,7 @@ tr_torrent const* tr_torrents::get(tr_sha1_digest_t const& hash) const
int tr_torrents::add(tr_torrent* tor)
{
int const id = static_cast<int>(std::size(by_id_));
auto const id = static_cast<int>(std::size(by_id_));
by_id_.push_back(tor);
by_hash_.insert(std::lower_bound(std::begin(by_hash_), std::end(by_hash_), tor, CompareTorrentByHash{}), tor);
return id;

View File

@ -26,8 +26,6 @@ struct tr_torrent_metainfo;
class tr_torrents
{
public:
tr_torrents();
// returns a fast lookup id for `tor`
[[nodiscard]] int add(tr_torrent* tor);
@ -111,8 +109,14 @@ private:
// of a wasted slot in the lookup table whenever a torrent is
// removed. This improves speed for all use cases at the cost of
// wasting a small amount of memory in uncommon use cases, e.g. a
// long-lived session where thousands of torrents are removed
std::vector<tr_torrent*> by_id_;
// long-lived session where thousands of torrents are removed.
//
// Insert an empty pointer at by_id_[0] to ensure that the first
// added torrent doesn't get an ID of 0; ie, that every torrent has
// a positive ID number. This constraint isn't needed by libtransmission
// code but the ID is exported in the RPC API to 3rd party clients that
// may be testing for >0 as a validity check.
std::vector<tr_torrent*> by_id_{ nullptr };
std::map<int, time_t> removed_;
};

View File

@ -298,8 +298,8 @@ public:
void write_block_func()
{
auto* const buf = this->content();
auto* const tor = tr_torrentFindFromId(this->session, this->torrent_id);
if (tor != nullptr)
if (auto* const tor = tr_torrentFindFromId(this->session, this->torrent_id); tor != nullptr)
{
auto const len = evbuffer_get_length(buf);
TR_ASSERT(tor->blockSize(this->loc.block) == len);