fix: some use-init-statement sonarcloud warnings (#2563)

This commit is contained in:
Charles Kerr 2022-02-01 19:09:11 -06:00 committed by GitHub
parent 878405f862
commit e908193027
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 31 additions and 72 deletions

View File

@ -301,9 +301,7 @@ bool getSelectedFilesForeach(
Glib::RefPtr<Gtk::TreeSelection> const& sel, Glib::RefPtr<Gtk::TreeSelection> const& sel,
std::vector<tr_file_index_t>& indexBuf) std::vector<tr_file_index_t>& indexBuf)
{ {
bool const is_file = iter->children().empty(); if (bool const is_file = iter->children().empty(); is_file)
if (is_file)
{ {
/* active means: if it's selected or any ancestor is selected */ /* active means: if it's selected or any ancestor is selected */
bool is_active = sel->is_selected(iter); bool is_active = sel->is_selected(iter);
@ -345,9 +343,7 @@ bool getSubtreeForeach(
Gtk::TreeModel::Path const& subtree_path, Gtk::TreeModel::Path const& subtree_path,
std::vector<tr_file_index_t>& indexBuf) std::vector<tr_file_index_t>& indexBuf)
{ {
bool const is_file = iter->children().empty(); if (bool const is_file = iter->children().empty(); is_file)
if (is_file)
{ {
if (path == subtree_path || path.is_descendant(subtree_path)) if (path == subtree_path || path.is_descendant(subtree_path))
{ {
@ -690,9 +686,7 @@ bool FileList::Impl::getAndSelectEventPath(GdkEventButton const* event, Gtk::Tre
if (view_->get_path_at_pos(event->x, event->y, path, col, cell_x, cell_y)) if (view_->get_path_at_pos(event->x, event->y, path, col, cell_x, cell_y))
{ {
auto const sel = view_->get_selection(); if (auto const sel = view_->get_selection(); !sel->is_selected(path))
if (!sel->is_selected(path))
{ {
sel->unselect_all(); sel->unselect_all();
sel->select(path); sel->select(path);

View File

@ -376,9 +376,7 @@ bool MessageLogWindow::Impl::onRefresh()
if (!isPaused_) if (!isPaused_)
{ {
auto* msgs = tr_logGetQueue(); if (auto* msgs = tr_logGetQueue(); msgs != nullptr)
if (msgs != nullptr)
{ {
/* add the new messages and append them to the end of /* add the new messages and append them to the end of
* our persistent list */ * our persistent list */

View File

@ -174,8 +174,7 @@ std::vector<std::string> gtr_pref_strv_get(tr_quark const key)
{ {
std::vector<std::string> ret; std::vector<std::string> ret;
tr_variant* list = nullptr; if (tr_variant* list = nullptr; tr_variantDictFindList(getPrefs(), key, &list))
if (tr_variantDictFindList(getPrefs(), key, &list))
{ {
size_t const n = tr_variantListSize(list); size_t const n = tr_variantListSize(list);
ret.reserve(n); ret.reserve(n);

View File

@ -901,10 +901,8 @@ void Session::Impl::on_torrent_metadata_changed(tr_torrent* tor)
Glib::signal_idle().connect( Glib::signal_idle().connect(
[this, core = get_core_ptr(), torrent_id = tr_torrentId(tor)]() [this, core = get_core_ptr(), torrent_id = tr_torrentId(tor)]()
{ {
auto const* const tor2 = tr_torrentFindFromId(session_, torrent_id);
/* update the torrent's collated name */ /* update the torrent's collated name */
if (tor2 != nullptr) if (auto const* const tor2 = tr_torrentFindFromId(session_, torrent_id); tor2 != nullptr)
{ {
if (auto const iter = find_row_from_torrent_id(raw_model_, torrent_id); iter) if (auto const iter = find_row_from_torrent_id(raw_model_, torrent_id); iter)
{ {
@ -1135,9 +1133,8 @@ void Session::Impl::add_file_async_callback(
bool Session::Impl::add_file(Glib::RefPtr<Gio::File> const& file, bool do_start, bool do_prompt, bool do_notify) bool Session::Impl::add_file(Glib::RefPtr<Gio::File> const& file, bool do_start, bool do_prompt, bool do_notify)
{ {
bool handled = false; bool handled = false;
auto const* const session = get_session();
if (session != nullptr) if (auto const* const session = get_session(); session != nullptr)
{ {
tr_ctor* ctor; tr_ctor* ctor;
bool tried = false; bool tried = false;

View File

@ -134,11 +134,9 @@ Glib::ustring getIconName()
{ {
Glib::ustring icon_name; Glib::ustring icon_name;
auto theme = Gtk::IconTheme::get_default();
// if the tray's icon is a 48x48 file, use it. // if the tray's icon is a 48x48 file, use it.
// otherwise, use the fallback builtin icon. // otherwise, use the fallback builtin icon.
if (!theme->has_icon(TrayIconName)) if (auto theme = Gtk::IconTheme::get_default(); !theme->has_icon(TrayIconName))
{ {
icon_name = AppIconName; icon_name = AppIconName;
} }

View File

@ -272,9 +272,7 @@ bool on_tree_view_button_pressed(
* clear all the selections. */ * clear all the selections. */
bool on_tree_view_button_released(Gtk::TreeView* view, GdkEventButton* event) bool on_tree_view_button_released(Gtk::TreeView* view, GdkEventButton* event)
{ {
Gtk::TreeModel::Path path; if (Gtk::TreeModel::Path path; !view->get_path_at_pos((int)event->x, (int)event->y, path))
if (!view->get_path_at_pos((int)event->x, (int)event->y, path))
{ {
view->get_selection()->unselect_all(); view->get_selection()->unselect_all();
} }

View File

@ -88,8 +88,7 @@ bool tr_announce_list::add(std::string_view announce_url_sv, tr_tracker_tier_t t
tracker.id = nextUniqueId(); tracker.id = nextUniqueId();
tracker.host = tr_strvJoin(tracker.announce.host, ":"sv, tracker.announce.portstr); tracker.host = tr_strvJoin(tracker.announce.host, ":"sv, tracker.announce.portstr);
auto const scrape_str = announceToScrape(announce_url_sv); if (auto const scrape_str = announceToScrape(announce_url_sv); scrape_str)
if (scrape_str)
{ {
tracker.scrape_str = *scrape_str; tracker.scrape_str = *scrape_str;
tracker.scrape = *tr_urlParseTracker(tracker.scrape_str.sv()); tracker.scrape = *tr_urlParseTracker(tracker.scrape_str.sv());
@ -127,11 +126,11 @@ std::optional<std::string> tr_announce_list::announceToScrape(std::string_view a
tr_quark tr_announce_list::announceToScrape(tr_quark announce) tr_quark tr_announce_list::announceToScrape(tr_quark announce)
{ {
auto const scrape_str = announceToScrape(tr_quark_get_string_view(announce)); if (auto const scrape_str = announceToScrape(tr_quark_get_string_view(announce)); scrape_str)
if (scrape_str)
{ {
return tr_quark_new(*scrape_str); return tr_quark_new(*scrape_str);
} }
return TR_KEY_NONE; return TR_KEY_NONE;
} }

View File

@ -110,8 +110,7 @@ static std::string announce_url_new(tr_session const* session, tr_announce_reque
announce twice. At any rate, we're already computing our IPv6 announce twice. At any rate, we're already computing our IPv6
address (for the LTEP handshake), so this comes for free. */ address (for the LTEP handshake), so this comes for free. */
unsigned char const* const ipv6 = tr_globalIPv6(session); if (auto const* const ipv6 = tr_globalIPv6(session); ipv6 != nullptr)
if (ipv6 != nullptr)
{ {
auto ipv6_readable = std::array<char, INET6_ADDRSTRLEN>{}; auto ipv6_readable = std::array<char, INET6_ADDRSTRLEN>{};
evutil_inet_ntop(AF_INET6, ipv6, std::data(ipv6_readable), std::size(ipv6_readable)); evutil_inet_ntop(AF_INET6, ipv6, std::data(ipv6_readable), std::size(ipv6_readable));
@ -418,9 +417,7 @@ void tr_announcerParseHttpScrapeResponse(tr_scrape_response& response, std::stri
bool Int64(int64_t value, Context const& context) override bool Int64(int64_t value, Context const& context) override
{ {
auto const key = currentKey(); if (auto const key = currentKey(); row_ && key == "complete"sv)
if (row_ && key == "complete"sv)
{ {
response_.rows[*row_].seeders = value; response_.rows[*row_].seeders = value;
} }
@ -443,9 +440,7 @@ void tr_announcerParseHttpScrapeResponse(tr_scrape_response& response, std::stri
bool String(std::string_view value, Context const& context) override bool String(std::string_view value, Context const& context) override
{ {
auto const key = currentKey(); if (auto const key = currentKey(); depth() == 1 && key == "failure reason"sv)
if (depth() == 1 && key == "failure reason"sv)
{ {
response_.errmsg = value; response_.errmsg = value;
} }

View File

@ -1322,8 +1322,7 @@ static void on_scrape_done(tr_scrape_response const* response, void* vsession)
tier->scheduleNextScrape(); tier->scheduleNextScrape();
tr_logAddTorDbg(tier->tor, "Scrape successful. Rescraping in %d seconds.", tier->scrapeIntervalSec); tr_logAddTorDbg(tier->tor, "Scrape successful. Rescraping in %d seconds.", tier->scrapeIntervalSec);
tr_tracker* const tracker = tier->currentTracker(); if (tr_tracker* const tracker = tier->currentTracker(); tracker != nullptr)
if (tracker != nullptr)
{ {
if (row.seeders >= 0) if (row.seeders >= 0)
{ {

View File

@ -917,9 +917,7 @@ bool tr_sys_file_advise(
TR_ASSERT(native_advice != POSIX_FADV_NORMAL); TR_ASSERT(native_advice != POSIX_FADV_NORMAL);
int const code = posix_fadvise(handle, offset, size, native_advice); if (int const code = posix_fadvise(handle, offset, size, native_advice); code != 0)
if (code != 0)
{ {
set_system_error(error, code); set_system_error(error, code);
ret = false; ret = false;
@ -1052,8 +1050,7 @@ bool tr_sys_file_preallocate(tr_sys_file_t handle, uint64_t size, int flags, tr_
{ {
errno = 0; errno = 0;
auto const success = approach(handle, size); if (auto const success = approach(handle, size); success)
if (success)
{ {
return success; return success;
} }
@ -1315,9 +1312,8 @@ char const* tr_sys_dir_read_name(tr_sys_dir_t handle, tr_error** error)
char const* ret = nullptr; char const* ret = nullptr;
errno = 0; errno = 0;
struct dirent const* const entry = readdir((DIR*)handle);
if (entry != nullptr) if (auto const* const entry = readdir((DIR*)handle); entry != nullptr)
{ {
ret = entry->d_name; ret = entry->d_name;
} }

View File

@ -769,8 +769,7 @@ static ReadState readPadA(tr_handshake* handshake, struct evbuffer* inbuf)
return READ_NOW; return READ_NOW;
} }
size_t const len = evbuffer_get_length(inbuf); if (size_t const len = evbuffer_get_length(inbuf); len > SHA_DIGEST_LENGTH)
if (len > SHA_DIGEST_LENGTH)
{ {
evbuffer_drain(inbuf, len - SHA_DIGEST_LENGTH); evbuffer_drain(inbuf, len - SHA_DIGEST_LENGTH);
} }

View File

@ -598,9 +598,7 @@ static int tr_globalAddress(int af, void* addr, int* addr_len)
return -1; return -1;
} }
int const rc = get_source_address(sa, salen, (struct sockaddr*)&ss, &sslen); if (int const rc = get_source_address(sa, salen, (struct sockaddr*)&ss, &sslen); rc < 0)
if (rc < 0)
{ {
return -1; return -1;
} }

View File

@ -1156,8 +1156,7 @@ static void parseLtepHandshake(tr_peerMsgsImpl* msgs, uint32_t len, struct evbuf
msgs->peerSupportsPex = false; msgs->peerSupportsPex = false;
msgs->peerSupportsMetadataXfer = false; msgs->peerSupportsMetadataXfer = false;
tr_variant* sub = nullptr; if (tr_variant* sub = nullptr; tr_variantDictFindDict(&val, TR_KEY_m, &sub))
if (tr_variantDictFindDict(&val, TR_KEY_m, &sub))
{ {
if (tr_variantDictFindInt(sub, TR_KEY_ut_pex, &i)) if (tr_variantDictFindInt(sub, TR_KEY_ut_pex, &i))
{ {
@ -1307,9 +1306,7 @@ static void parseUtPex(tr_peerMsgsImpl* msgs, uint32_t msglen, struct evbuffer*
auto* tmp = tr_new(char, msglen); auto* tmp = tr_new(char, msglen);
tr_peerIoReadBytes(msgs->io, inbuf, tmp, msglen); tr_peerIoReadBytes(msgs->io, inbuf, tmp, msglen);
tr_variant val; if (tr_variant val; tr_variantFromBuf(&val, TR_VARIANT_PARSE_BENC | TR_VARIANT_PARSE_INPLACE, { tmp, msglen }))
if (tr_variantFromBuf(&val, TR_VARIANT_PARSE_BENC | TR_VARIANT_PARSE_INPLACE, { tmp, msglen }))
{ {
uint8_t const* added = nullptr; uint8_t const* added = nullptr;
auto added_len = size_t{}; auto added_len = size_t{};
@ -1914,8 +1911,7 @@ static int clientGotBlock(tr_peerMsgsImpl* msgs, struct evbuffer* data, struct p
*** Save the block *** Save the block
**/ **/
int const err = tr_cacheWriteBlock(msgs->session->cache, tor, req->index, req->offset, req->length, data); if (int const err = tr_cacheWriteBlock(msgs->session->cache, tor, req->index, req->offset, req->length, data); err != 0)
if (err != 0)
{ {
return err; return err;
} }

View File

@ -305,17 +305,14 @@ static uint64_t loadRatioLimits(tr_variant* dict, tr_torrent* tor)
{ {
auto ret = uint64_t{}; auto ret = uint64_t{};
tr_variant* d = nullptr; if (tr_variant* d = nullptr; tr_variantDictFindDict(dict, TR_KEY_ratio_limit, &d))
if (tr_variantDictFindDict(dict, TR_KEY_ratio_limit, &d))
{ {
auto dratio = double{}; if (auto dratio = double{}; tr_variantDictFindReal(d, TR_KEY_ratio_limit, &dratio))
if (tr_variantDictFindReal(d, TR_KEY_ratio_limit, &dratio))
{ {
tr_torrentSetRatioLimit(tor, dratio); tr_torrentSetRatioLimit(tor, dratio);
} }
auto i = int64_t{}; if (auto i = int64_t{}; tr_variantDictFindInt(d, TR_KEY_ratio_mode, &i))
if (tr_variantDictFindInt(d, TR_KEY_ratio_mode, &i))
{ {
tr_torrentSetRatioMode(tor, tr_ratiolimit(i)); tr_torrentSetRatioMode(tor, tr_ratiolimit(i));
} }
@ -330,17 +327,14 @@ static uint64_t loadIdleLimits(tr_variant* dict, tr_torrent* tor)
{ {
auto ret = uint64_t{}; auto ret = uint64_t{};
tr_variant* d = nullptr; if (tr_variant* d = nullptr; tr_variantDictFindDict(dict, TR_KEY_idle_limit, &d))
if (tr_variantDictFindDict(dict, TR_KEY_idle_limit, &d))
{ {
auto imin = int64_t{}; if (auto imin = int64_t{}; tr_variantDictFindInt(d, TR_KEY_idle_limit, &imin))
if (tr_variantDictFindInt(d, TR_KEY_idle_limit, &imin))
{ {
tr_torrentSetIdleLimit(tor, imin); tr_torrentSetIdleLimit(tor, imin);
} }
auto i = int64_t{}; if (auto i = int64_t{}; tr_variantDictFindInt(d, TR_KEY_idle_mode, &i))
if (tr_variantDictFindInt(d, TR_KEY_idle_mode, &i))
{ {
tr_torrentSetIdleMode(tor, tr_idlelimit(i)); tr_torrentSetIdleMode(tor, tr_idlelimit(i));
} }
@ -678,8 +672,7 @@ void tr_torrentSaveResume(tr_torrent* tor)
saveName(&top, tor); saveName(&top, tor);
saveLabels(&top, tor); saveLabels(&top, tor);
auto const err = tr_variantToFile(&top, TR_VARIANT_FMT_BENC, tor->resumeFile()); if (auto const err = tr_variantToFile(&top, TR_VARIANT_FMT_BENC, tor->resumeFile()); err != 0)
if (err != 0)
{ {
tor->setLocalError(tr_strvJoin("Unable to save resume file: ", tr_strerror(err))); tor->setLocalError(tr_strvJoin("Unable to save resume file: ", tr_strerror(err)));
} }