mirror of
https://github.com/transmission/transmission
synced 2024-12-31 20:16:57 +00:00
fix: sonarcloud cpp:S6004 (#4270)
reduce scope of variables by using if-based initializer
This commit is contained in:
parent
4adda4dc1e
commit
59335eac03
18 changed files with 32 additions and 47 deletions
|
@ -211,8 +211,7 @@ bool decodeBitCometClient(char* buf, size_t buflen, std::string_view peer_id)
|
|||
// replaced exbc with FUTB. The encoding for BitComet Peer IDs changed
|
||||
// to Azureus-style as of BitComet version 0.59.
|
||||
auto mod = std::string_view{};
|
||||
auto const lead = std::string_view{ std::data(peer_id), std::min(std::size(peer_id), size_t{ 4 }) };
|
||||
if (lead == "exbc")
|
||||
if (auto const lead = std::string_view{ std::data(peer_id), std::min(std::size(peer_id), size_t{ 4 }) }; lead == "exbc")
|
||||
{
|
||||
mod = ""sv;
|
||||
}
|
||||
|
|
|
@ -497,9 +497,7 @@ static ReadState readVC(tr_handshake* handshake, tr_peerIo* peer_io)
|
|||
|
||||
static ReadState readCryptoSelect(tr_handshake* handshake, tr_peerIo* peer_io)
|
||||
{
|
||||
static size_t const needlen = sizeof(uint32_t) + sizeof(uint16_t);
|
||||
|
||||
if (peer_io->readBufferSize() < needlen)
|
||||
if (static size_t constexpr NeedLen = sizeof(uint32_t) + sizeof(uint16_t); peer_io->readBufferSize() < NeedLen)
|
||||
{
|
||||
return READ_LATER;
|
||||
}
|
||||
|
|
|
@ -202,8 +202,8 @@ void tr_magnet_metainfo::addWebseed(std::string_view webseed)
|
|||
}
|
||||
|
||||
auto& urls = webseed_urls_;
|
||||
auto const it = std::find(std::begin(urls), std::end(urls), webseed);
|
||||
if (it != std::end(urls))
|
||||
|
||||
if (auto const it = std::find(std::begin(urls), std::end(urls), webseed); it != std::end(urls))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -924,8 +924,8 @@ static void sendLtepHandshake(tr_peerMsgsImpl* msgs)
|
|||
// It also adds "metadata_size" to the handshake message (not the
|
||||
// "m" dictionary) specifying an integer value of the number of
|
||||
// bytes of the metadata.
|
||||
auto const info_dict_size = msgs->torrent->infoDictSize();
|
||||
if (allow_metadata_xfer && msgs->torrent->hasMetainfo() && info_dict_size > 0)
|
||||
if (auto const info_dict_size = msgs->torrent->infoDictSize();
|
||||
allow_metadata_xfer && msgs->torrent->hasMetainfo() && info_dict_size > 0)
|
||||
{
|
||||
tr_variantDictAddInt(&val, TR_KEY_metadata_size, info_dict_size);
|
||||
}
|
||||
|
@ -1979,8 +1979,7 @@ static size_t fillOutputBuffer(tr_peerMsgsImpl* msgs, time_t now)
|
|||
*** Metadata Pieces
|
||||
**/
|
||||
|
||||
auto piece = int{};
|
||||
if (msgs->io->getWriteBufferSpace(now) >= METADATA_PIECE_SIZE && popNextMetadataRequest(msgs, &piece))
|
||||
if (auto piece = int{}; msgs->io->getWriteBufferSpace(now) >= METADATA_PIECE_SIZE && popNextMetadataRequest(msgs, &piece))
|
||||
{
|
||||
auto ok = bool{ false };
|
||||
|
||||
|
|
|
@ -152,8 +152,8 @@ size_t tr_getDefaultConfigDirToBuf(char const* appname, char* buf, size_t buflen
|
|||
static std::string getXdgEntryFromUserDirs(std::string_view key)
|
||||
{
|
||||
auto content = std::vector<char>{};
|
||||
auto const filename = fmt::format("{:s}/{:s}"sv, xdgConfigHome(), "user-dirs.dirs"sv);
|
||||
if (!tr_sys_path_exists(filename) || !tr_loadFile(filename, content) || std::empty(content))
|
||||
if (auto const filename = fmt::format("{:s}/{:s}"sv, xdgConfigHome(), "user-dirs.dirs"sv);
|
||||
!tr_sys_path_exists(filename) || !tr_loadFile(filename, content) || std::empty(content))
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
|
|
@ -443,8 +443,8 @@ std::optional<tr_quark> tr_quark_lookup(std::string_view key)
|
|||
// is it in our static array?
|
||||
auto constexpr Sbegin = std::begin(MyStatic);
|
||||
auto constexpr Send = std::end(MyStatic);
|
||||
auto const sit = std::lower_bound(Sbegin, Send, key);
|
||||
if (sit != Send && *sit == key)
|
||||
|
||||
if (auto const sit = std::lower_bound(Sbegin, Send, key); sit != Send && *sit == key)
|
||||
{
|
||||
return std::distance(Sbegin, sit);
|
||||
}
|
||||
|
@ -452,8 +452,7 @@ std::optional<tr_quark> tr_quark_lookup(std::string_view key)
|
|||
/* was it added during runtime? */
|
||||
auto const rbegin = std::begin(my_runtime);
|
||||
auto const rend = std::end(my_runtime);
|
||||
auto const rit = std::find(rbegin, rend, key);
|
||||
if (rit != rend)
|
||||
if (auto const rit = std::find(rbegin, rend, key); rit != rend)
|
||||
{
|
||||
return TR_N_KEYS + std::distance(rbegin, rit);
|
||||
}
|
||||
|
|
|
@ -162,9 +162,8 @@ static auto loadDND(tr_variant* dict, tr_torrent* tor)
|
|||
{
|
||||
auto ret = tr_resume::fields_t{};
|
||||
tr_variant* list = nullptr;
|
||||
auto const n = tor->fileCount();
|
||||
|
||||
if (tr_variantDictFindList(dict, TR_KEY_dnd, &list) && tr_variantListSize(list) == n)
|
||||
if (auto const n = tor->fileCount(); tr_variantDictFindList(dict, TR_KEY_dnd, &list) && tr_variantListSize(list) == n)
|
||||
{
|
||||
auto wanted = std::vector<tr_file_index_t>{};
|
||||
auto unwanted = std::vector<tr_file_index_t>{};
|
||||
|
|
|
@ -449,8 +449,8 @@ static void handle_request(struct evhttp_request* req, void* arg)
|
|||
|
||||
if (req->type == EVHTTP_REQ_OPTIONS)
|
||||
{
|
||||
char const* headers = evhttp_find_header(req->input_headers, "Access-Control-Request-Headers");
|
||||
if (headers != nullptr)
|
||||
if (char const* headers = evhttp_find_header(req->input_headers, "Access-Control-Request-Headers");
|
||||
headers != nullptr)
|
||||
{
|
||||
evhttp_add_header(req->output_headers, "Access-Control-Allow-Headers", headers);
|
||||
}
|
||||
|
|
|
@ -1129,8 +1129,8 @@ static char const* addTrackerUrls(tr_torrent* tor, tr_variant* urls)
|
|||
for (size_t i = 0, n = tr_variantListSize(urls); i < n; ++i)
|
||||
{
|
||||
auto announce = std::string_view();
|
||||
auto const* const val = tr_variantListChild(urls, i);
|
||||
if (val == nullptr || !tr_variantGetStrView(val, &announce))
|
||||
|
||||
if (auto const* const val = tr_variantListChild(urls, i); val == nullptr || !tr_variantGetStrView(val, &announce))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
@ -1181,8 +1181,8 @@ static char const* removeTrackers(tr_torrent* tor, tr_variant* ids)
|
|||
for (size_t i = 0, n = tr_variantListSize(ids); i < n; ++i)
|
||||
{
|
||||
auto id = int64_t{};
|
||||
auto const* const val = tr_variantListChild(ids, i);
|
||||
if (val == nullptr || !tr_variantGetInt(val, &id))
|
||||
|
||||
if (auto const* const val = tr_variantListChild(ids, i); val == nullptr || !tr_variantGetInt(val, &id))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
|
|
@ -134,8 +134,7 @@ std::optional<std::vector<std::byte>> tr_torrentGetMetadataPiece(tr_torrent cons
|
|||
auto const info_dict_size = tor->infoDictSize();
|
||||
TR_ASSERT(info_dict_size > 0);
|
||||
auto const offset_in_info_dict = static_cast<uint64_t>(piece) * METADATA_PIECE_SIZE;
|
||||
auto const offset_in_file = tor->infoDictOffset() + offset_in_info_dict;
|
||||
if (!in.seekg(offset_in_file))
|
||||
if (auto const offset_in_file = tor->infoDictOffset() + offset_in_info_dict; !in.seekg(offset_in_file))
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
|
|
@ -426,8 +426,7 @@ private:
|
|||
|
||||
// If it doesn't look like a BEP14 message, discard it
|
||||
auto const msg = std::string_view{ std::data(foreign_msg), static_cast<size_t>(res) };
|
||||
static auto constexpr SearchKey = "BT-SEARCH * HTTP/"sv;
|
||||
if (msg.find(SearchKey) == std::string_view::npos)
|
||||
if (static auto constexpr SearchKey = "BT-SEARCH * HTTP/"sv; msg.find(SearchKey) == std::string_view::npos)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -152,8 +152,7 @@ bool tr_saveFile(std::string_view filename, std::string_view contents, tr_error*
|
|||
}
|
||||
|
||||
// If we saved it to disk successfully, move it from '.tmp' to the correct filename
|
||||
auto const szfilename = tr_pathbuf{ filename };
|
||||
if (!tr_sys_file_close(fd, error) || !ok || !tr_sys_path_rename(tmp, szfilename, error))
|
||||
if (!tr_sys_file_close(fd, error) || !ok || !tr_sys_path_rename(tmp, tr_pathbuf{ filename }, error))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -393,9 +393,7 @@ static tr_variant* containerReserve(tr_variant* v, size_t count)
|
|||
{
|
||||
TR_ASSERT(tr_variantIsContainer(v));
|
||||
|
||||
size_t const needed = v->val.l.count + count;
|
||||
|
||||
if (needed > v->val.l.alloc)
|
||||
if (size_t const needed = v->val.l.count + count; needed > v->val.l.alloc)
|
||||
{
|
||||
/* scale the alloc size in powers-of-2 */
|
||||
size_t n = v->val.l.alloc != 0 ? v->val.l.alloc : 8;
|
||||
|
|
|
@ -119,9 +119,7 @@ bool tr_verify_worker::verifyTorrent(tr_torrent* tor, std::atomic<bool> const& s
|
|||
/* if we're finishing a piece... */
|
||||
if (left_in_piece == 0)
|
||||
{
|
||||
auto const has_piece = sha->finish() == tor->pieceHash(piece);
|
||||
|
||||
if (has_piece || had_piece)
|
||||
if (auto const has_piece = sha->finish() == tor->pieceHash(piece); has_piece || had_piece)
|
||||
{
|
||||
tor->setHasPiece(piece, has_piece);
|
||||
changed |= has_piece != had_piece;
|
||||
|
|
|
@ -39,14 +39,13 @@ void DBusInteropHelper::registerObject(QObject* parent)
|
|||
return;
|
||||
}
|
||||
|
||||
auto const service_name = QStringLiteral("com.transmissionbt.Transmission");
|
||||
if (!bus.registerService(service_name))
|
||||
if (auto const service_name = QStringLiteral("com.transmissionbt.Transmission"); !bus.registerService(service_name))
|
||||
{
|
||||
qWarning() << "couldn't register" << qPrintable(service_name);
|
||||
}
|
||||
|
||||
auto const object_path = QStringLiteral("/com/transmissionbt/Transmission");
|
||||
if (!bus.registerObject(object_path, new InteropObject(parent), QDBusConnection::ExportAllSlots))
|
||||
if (auto const object_path = QStringLiteral("/com/transmissionbt/Transmission");
|
||||
!bus.registerObject(object_path, new InteropObject(parent), QDBusConnection::ExportAllSlots))
|
||||
{
|
||||
qWarning() << "couldn't register" << qPrintable(object_path);
|
||||
}
|
||||
|
|
|
@ -286,8 +286,8 @@ void FilterBar::refreshPref(int key)
|
|||
case Prefs::FILTER_TRACKERS:
|
||||
{
|
||||
auto const display_name = prefs_.getString(key);
|
||||
auto rows = tracker_model_->findItems(display_name);
|
||||
if (!rows.isEmpty())
|
||||
|
||||
if (auto rows = tracker_model_->findItems(display_name); !rows.isEmpty())
|
||||
{
|
||||
tracker_combo_->setCurrentIndex(rows.front()->row());
|
||||
}
|
||||
|
|
|
@ -2317,8 +2317,7 @@ static int flush(char const* rpcurl, tr_variant* benc, Config& config)
|
|||
fmt::print(stderr, "posting:\n--------\n{:s}\n--------\n", json);
|
||||
}
|
||||
|
||||
auto const res = curl_easy_perform(curl);
|
||||
if (res != CURLE_OK)
|
||||
if (auto const res = curl_easy_perform(curl); res != CURLE_OK)
|
||||
{
|
||||
tr_logAddWarn(fmt::format(" ({}) {}", rpcurl_http, curl_easy_strerror(res)));
|
||||
status |= EXIT_FAILURE;
|
||||
|
|
|
@ -365,8 +365,8 @@ void doScrape(tr_torrent_metainfo const& metainfo)
|
|||
// print it out
|
||||
tr_variant top;
|
||||
auto* const begin = (char const*)evbuffer_pullup(buf, -1);
|
||||
auto sv = std::string_view{ begin, evbuffer_get_length(buf) };
|
||||
if (!tr_variantFromBuf(&top, TR_VARIANT_PARSE_BENC | TR_VARIANT_PARSE_INPLACE, sv))
|
||||
if (auto sv = std::string_view{ begin, evbuffer_get_length(buf) };
|
||||
!tr_variantFromBuf(&top, TR_VARIANT_PARSE_BENC | TR_VARIANT_PARSE_INPLACE, sv))
|
||||
{
|
||||
fmt::print("error parsing scrape response\n");
|
||||
continue;
|
||||
|
|
Loading…
Reference in a new issue