fix: sonarcloud code smells (#2486)

This commit is contained in:
Charles Kerr 2022-01-23 18:53:35 -06:00 committed by GitHub
parent a2e30235c4
commit 26bb9f3440
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 74 additions and 87 deletions

View File

@ -423,9 +423,8 @@ bool Application::Impl::on_rpc_changed_idle(tr_rpc_callback_type type, int torre
for (int i = 0; tr_variantDictChild(&tmp, i, &key, &newval); ++i)
{
bool changed;
tr_variant const* oldval = tr_variantDictFind(oldvals, key);
if (oldval == nullptr)
if (tr_variant const* oldval = tr_variantDictFind(oldvals, key); oldval == nullptr)
{
changed = true;
}
@ -1045,20 +1044,15 @@ void Application::Impl::on_prefs_changed(tr_quark const key)
break;
case TR_KEY_show_notification_area_icon:
if (bool const show = gtr_pref_flag_get(key); show && icon_ == nullptr)
{
bool const show = gtr_pref_flag_get(key);
if (show && icon_ == nullptr)
{
icon_ = std::make_unique<SystemTrayIcon>(*wind_, core_);
}
else if (!show && icon_ != nullptr)
{
icon_.reset();
}
break;
icon_ = std::make_unique<SystemTrayIcon>(*wind_, core_);
}
else if (!show && icon_ != nullptr)
{
icon_.reset();
}
break;
case TR_KEY_speed_limit_down_enabled:
tr_sessionLimitSpeed(tr, TR_DOWN, gtr_pref_flag_get(key));
@ -1359,9 +1353,8 @@ tr_torrent* Application::Impl::get_first_selected_torrent() const
{
tr_torrent* tor = nullptr;
Glib::RefPtr<Gtk::TreeModel> m;
auto const l = sel_->get_selected_rows(m);
if (!l.empty())
if (auto const l = sel_->get_selected_rows(m); !l.empty())
{
if (auto iter = m->get_iter(l.front()); iter)
{

View File

@ -556,9 +556,8 @@ void renderDownload(Gtk::CellRenderer* renderer, Gtk::TreeModel::iterator const&
void renderPriority(Gtk::CellRenderer* renderer, Gtk::TreeModel::iterator const& iter)
{
Glib::ustring text;
auto const priority = iter->get_value(file_cols.priority);
switch (priority)
switch (auto const priority = iter->get_value(file_cols.priority); priority)
{
case TR_PRI_HIGH:
text = _("High");

View File

@ -588,9 +588,7 @@ void MainWindow::Impl::updateStats()
auto const* const session = core_->get_session();
/* update the stats */
auto const pch = gtr_pref_string_get(TR_KEY_statusbar_stats);
if (pch == "session-ratio")
if (auto const pch = gtr_pref_string_get(TR_KEY_statusbar_stats); pch == "session-ratio")
{
tr_sessionGetStats(session, &stats);
buf = gtr_sprintf(_("Ratio: %s"), tr_strlratio(stats.ratio));

View File

@ -390,11 +390,10 @@ void MakeDialog::Impl::on_drag_data_received(
guint time_)
{
bool success = false;
auto const uris = selection_data.get_uris();
if (!uris.empty())
if (auto const uris = selection_data.get_uris(); !uris.empty())
{
auto const uri = uris.front();
auto const& uri = uris.front();
auto const filename = Glib::filename_from_uri(uri);
if (Glib::file_test(filename, Glib::FILE_TEST_IS_DIR))

View File

@ -421,11 +421,11 @@ Gtk::ComboBox* gtr_combo_box_new_enum(std::vector<std::pair<Glib::ustring, int>>
{
auto store = Gtk::ListStore::create(enum_combo_cols);
for (auto const& item : items)
for (auto const& [label, value] : items)
{
auto const iter = store->append();
(*iter)[enum_combo_cols.value] = item.second;
(*iter)[enum_combo_cols.label] = item.first;
(*iter)[enum_combo_cols.value] = value;
(*iter)[enum_combo_cols.label] = label;
}
auto w = Gtk::make_managed<Gtk::ComboBox>(static_cast<Glib::RefPtr<Gtk::TreeModel> const&>(store));

View File

@ -86,8 +86,7 @@ static std::string announce_url_new(tr_session const* session, tr_announce_reque
evbuffer_add_printf(buf, "&corrupt=%" PRIu64, req->corrupt);
}
char const* str = get_event_string(req);
if (!tr_str_is_empty(str))
if (char const* str = get_event_string(req); !tr_str_is_empty(str))
{
evbuffer_add_printf(buf, "&event=%s", str);
}

View File

@ -168,7 +168,6 @@ struct tr_announcer
explicit tr_announcer(tr_session* session_in)
: session{ session_in }
, upkeep_timer{ evtimer_new(session_in->event_base, onUpkeepTimer, this) }
, key{ tr_rand_int(INT_MAX) }
{
scheduleNextUpdate();
}
@ -188,7 +187,7 @@ struct tr_announcer
tr_session* const session;
event* const upkeep_timer;
int const key;
int const key = tr_rand_int(INT_MAX);
time_t tau_upkeep_at = 0;
};
@ -591,8 +590,7 @@ static void publishMessage(tr_tier* tier, std::string_view msg, TrackerEventType
event.messageType = type;
event.text = msg;
auto* const current_tracker = tier->currentTracker();
if (current_tracker != nullptr)
if (auto const* const current_tracker = tier->currentTracker(); current_tracker != nullptr)
{
event.announce_url = current_tracker->announce_url;
}
@ -865,7 +863,7 @@ void tr_announcerRemoveTorrent(tr_announcer* announcer, tr_torrent* tor)
return;
}
for (auto& tier : ta->tiers)
for (auto const& tier : ta->tiers)
{
if (tier.isRunning)
{
@ -1405,7 +1403,7 @@ static void multiscrape(tr_announcer* announcer, std::vector<tr_tier*> const& ti
/* otherwise, if there's room for another request, build a new one */
if (!found && request_count < MaxScrapesPerUpkeep)
{
auto* const req = &requests[request_count++];
auto* const req = &requests[request_count];
req->scrape_url = scrape_info->scrape_url;
tier->buildLogName(req->log_name, sizeof(req->log_name));
@ -1413,6 +1411,8 @@ static void multiscrape(tr_announcer* announcer, std::vector<tr_tier*> const& ti
++req->info_hash_count;
tier->isScraping = true;
tier->lastScrapeStartTime = now;
++request_count;
}
}

View File

@ -26,11 +26,11 @@
using namespace std::literals;
/* enable LibTransmission extension protocol */
#define ENABLE_LTEP * /
#define ENABLE_LTEP
/* fast extensions */
#define ENABLE_FAST * /
#define ENABLE_FAST
/* DHT */
#define ENABLE_DHT * /
#define ENABLE_DHT
/***
****

View File

@ -151,55 +151,55 @@ public:
[[nodiscard]] constexpr auto blockCount() const
{
return blockInfo().blockCount();
return metainfo_.blockCount();
}
[[nodiscard]] constexpr auto blockOf(uint64_t offset) const
{
return blockInfo().blockOf(offset);
return metainfo_.blockOf(offset);
}
[[nodiscard]] constexpr auto blockOf(tr_piece_index_t piece, uint32_t offset, uint32_t length = 0) const
{
return blockInfo().blockOf(piece, offset, length);
return metainfo_.blockOf(piece, offset, length);
}
[[nodiscard]] constexpr auto blockSize() const
{
return blockInfo().blockSize();
return metainfo_.blockSize();
}
[[nodiscard]] constexpr auto blockSize(tr_block_index_t block) const
{
return blockInfo().blockSize(block);
return metainfo_.blockSize(block);
}
[[nodiscard]] constexpr auto blockSpanForPiece(tr_piece_index_t piece) const
{
return blockInfo().blockSpanForPiece(piece);
return metainfo_.blockSpanForPiece(piece);
}
[[nodiscard]] constexpr auto offset(tr_piece_index_t piece, uint32_t offset, uint32_t length = 0) const
{
return blockInfo().offset(piece, offset, length);
return metainfo_.offset(piece, offset, length);
}
[[nodiscard]] constexpr auto pieceCount() const
{
return blockInfo().pieceCount();
return metainfo_.pieceCount();
}
[[nodiscard]] constexpr auto pieceForBlock(tr_block_index_t block) const
{
return blockInfo().pieceForBlock(block);
return metainfo_.pieceForBlock(block);
}
[[nodiscard]] constexpr auto pieceOf(uint64_t offset) const
{
return blockInfo().pieceOf(offset);
return metainfo_.pieceOf(offset);
}
[[nodiscard]] constexpr auto pieceSize() const
{
return blockInfo().pieceSize();
return metainfo_.pieceSize();
}
[[nodiscard]] constexpr auto pieceSize(tr_piece_index_t piece) const
{
return blockInfo().pieceSize(piece);
return metainfo_.pieceSize(piece);
}
[[nodiscard]] constexpr auto totalSize() const
{
return blockInfo().totalSize();
return metainfo_.totalSize();
}
/// COMPLETION

View File

@ -298,7 +298,7 @@ uint8_t* tr_loadFile(char const* path, size_t* size, tr_error** error)
{
tr_logAddError(err_fmt, path, my_error->message);
tr_sys_file_close(fd, nullptr);
free(buf);
tr_free(buf);
tr_error_propagate(error, &my_error);
return nullptr;
}

View File

@ -829,6 +829,7 @@ protected:
}
}
private:
// When walking `v`'s children, this is the index of the next child
size_t child_index = 0;

View File

@ -262,10 +262,10 @@ MainWindow::MainWindow(Session& session, Prefs& prefs, TorrentModel& model, bool
// NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks)
auto* action_group = new QActionGroup(this);
for (auto const& mode : sort_modes)
for (auto const& [action, mode] : sort_modes)
{
mode.first->setProperty(SortModeKey, mode.second);
action_group->addAction(mode.first);
action->setProperty(SortModeKey, mode);
action_group->addAction(action);
}
connect(action_group, &QActionGroup::triggered, this, &MainWindow::onSortModeChanged);
@ -1605,8 +1605,8 @@ bool MainWindow::event(QEvent* e)
return QMainWindow::event(e);
}
QString const text = QGuiApplication::clipboard()->text().trimmed();
if (text.endsWith(QStringLiteral(".torrent"), Qt::CaseInsensitive) ||
if (auto const text = QGuiApplication::clipboard()->text().trimmed();
text.endsWith(QStringLiteral(".torrent"), Qt::CaseInsensitive) ||
text.startsWith(QStringLiteral("magnet:"), Qt::CaseInsensitive))
{
for (QString const& entry : text.split(QLatin1Char('\n')))

View File

@ -70,8 +70,8 @@ private:
// normal closure, takes response and returns new future
template<
typename Func,
typename std::enable_if<
std::is_same_v<typename std::invoke_result_t<Func, RpcResponse const&>, RpcResponseFuture>>::type* = nullptr>
typename std::enable_if_t<std::is_same_v<typename std::invoke_result_t<Func, RpcResponse const&>, RpcResponseFuture>>* =
nullptr>
QueuedFunction normalizeFunc(Func const& func) const
{
return [func](RpcResponseFuture const& r)
@ -83,7 +83,7 @@ private:
// closure without argument (first step), takes nothing and returns new future
template<
typename Func,
typename std::enable_if<std::is_same_v<typename std::invoke_result_t<Func>, RpcResponseFuture>>::type* = nullptr>
typename std::enable_if_t<std::is_same_v<typename std::invoke_result_t<Func>, RpcResponseFuture>>* = nullptr>
QueuedFunction normalizeFunc(Func const& func) const
{
return [func](RpcResponseFuture const&)
@ -95,7 +95,7 @@ private:
// closure without return value ("auxiliary"), takes response and returns nothing
template<
typename Func,
typename std::enable_if<std::is_same_v<typename std::invoke_result_t<Func, RpcResponse const&>, void>>::type* = nullptr>
typename std::enable_if_t<std::is_same_v<typename std::invoke_result_t<Func, RpcResponse const&>, void>>* = nullptr>
QueuedFunction normalizeFunc(Func const& func) const
{
return [func](RpcResponseFuture const& r)
@ -106,7 +106,7 @@ private:
}
// closure without argument and return value, takes nothing and returns nothing -- next function will also get nothing
template<typename Func, typename std::enable_if<std::is_same_v<typename std::invoke_result_t<Func>, void>>::type* = nullptr>
template<typename Func, typename std::enable_if_t<std::is_same_v<typename std::invoke_result_t<Func>, void>>* = nullptr>
QueuedFunction normalizeFunc(Func const& func) const
{
return [func](RpcResponseFuture const&)
@ -119,7 +119,7 @@ private:
// normal error handler, takes last response
template<
typename Func,
typename std::enable_if<std::is_same_v<typename std::invoke_result_t<Func, RpcResponse const&>, void>>::type* = nullptr>
typename std::enable_if_t<std::is_same_v<typename std::invoke_result_t<Func, RpcResponse const&>, void>>* = nullptr>
ErrorHandlerFunction normalizeErrorHandler(Func const& func) const
{
return [func](RpcResponseFuture const& r)
@ -129,7 +129,7 @@ private:
}
// error handler without an argument, takes nothing
template<typename Func, typename std::enable_if<std::is_same_v<typename std::invoke_result_t<Func>, void>>::type* = nullptr>
template<typename Func, typename std::enable_if_t<std::is_same_v<typename std::invoke_result_t<Func>, void>>* = nullptr>
ErrorHandlerFunction normalizeErrorHandler(Func const& func) const
{
return [func](RpcResponseFuture const&)

View File

@ -1115,9 +1115,9 @@ void Session::onDuplicatesTimer()
duplicates.swap(duplicates_);
QStringList lines;
for (auto it : duplicates)
for (auto [dupe, original] : duplicates)
{
lines.push_back(tr("%1 (copy of %2)").arg(it.first).arg(it.second.left(7)));
lines.push_back(tr("%1 (copy of %2)").arg(dupe).arg(original.left(7)));
}
if (!lines.empty())

View File

@ -342,10 +342,10 @@ std::optional<int> TorrentModel::getRow(int id) const
{
std::optional<int> row;
auto const it = std::equal_range(torrents_.begin(), torrents_.end(), id, TorrentIdLessThan());
if (it.first != it.second)
auto const [begin, end] = std::equal_range(torrents_.begin(), torrents_.end(), id, TorrentIdLessThan());
if (begin != end)
{
row = std::distance(torrents_.begin(), it.first);
row = std::distance(torrents_.begin(), begin);
assert(torrents_[*row]->id() == id);
}
@ -425,9 +425,9 @@ std::vector<TorrentModel::span_t> TorrentModel::getSpans(torrent_ids_t const& id
void TorrentModel::rowsEmitChanged(torrent_ids_t const& ids)
{
for (auto const& span : getSpans(ids))
for (auto const& [first, last] : getSpans(ids))
{
emit dataChanged(index(span.first), index(span.second));
emit dataChanged(index(first), index(last));
}
}
@ -462,10 +462,10 @@ void TorrentModel::rowsRemove(torrents_t const& torrents)
auto const& spans = getSpans(getIds(torrents.begin(), torrents.end()));
for (auto it = spans.rbegin(), end = spans.rend(); it != end; ++it)
{
auto const& span = *it;
auto const& [first, last] = *it;
beginRemoveRows(QModelIndex(), span.first, span.second);
torrents_.erase(torrents_.begin() + span.first, torrents_.begin() + span.second + 1);
beginRemoveRows(QModelIndex(), first, last);
torrents_.erase(torrents_.begin() + first, torrents_.begin() + last + 1);
endRemoveRows();
}

View File

@ -28,7 +28,7 @@ namespace trqt
namespace variant_helpers
{
template<typename T, typename std::enable_if<std::is_same_v<T, bool>>::type* = nullptr>
template<typename T, typename std::enable_if_t<std::is_same_v<T, bool>>* = nullptr>
auto getValue(tr_variant const* variant)
{
std::optional<T> ret;
@ -43,9 +43,9 @@ auto getValue(tr_variant const* variant)
template<
typename T,
typename std::enable_if<
std::is_same_v<T, int64_t> || std::is_same_v<T, uint64_t> || std::is_same_v<T, int> ||
std::is_same_v<T, time_t>>::type* = nullptr>
typename std::enable_if_t<
std::is_same_v<T, int64_t> || std::is_same_v<T, uint64_t> || std::is_same_v<T, int> || std::is_same_v<T, time_t>>* =
nullptr>
auto getValue(tr_variant const* variant)
{
std::optional<T> ret;
@ -58,7 +58,7 @@ auto getValue(tr_variant const* variant)
return ret;
}
template<typename T, typename std::enable_if<std::is_same_v<T, double>>::type* = nullptr>
template<typename T, typename std::enable_if_t<std::is_same_v<T, double>>* = nullptr>
auto getValue(tr_variant const* variant)
{
std::optional<T> ret;
@ -71,7 +71,7 @@ auto getValue(tr_variant const* variant)
return ret;
}
template<typename T, typename std::enable_if<std::is_same_v<T, QString>>::type* = nullptr>
template<typename T, typename std::enable_if_t<std::is_same_v<T, QString>>* = nullptr>
auto getValue(tr_variant const* variant)
{
std::optional<T> ret;
@ -84,7 +84,7 @@ auto getValue(tr_variant const* variant)
return ret;
}
template<typename T, typename std::enable_if<std::is_same_v<T, std::string_view>>::type* = nullptr>
template<typename T, typename std::enable_if_t<std::is_same_v<T, std::string_view>>* = nullptr>
auto getValue(tr_variant const* variant)
{
std::optional<T> ret;
@ -100,8 +100,8 @@ auto getValue(tr_variant const* variant)
template<
typename C,
typename T = typename C::value_type,
typename std::enable_if<
std::is_same_v<C, QStringList> || std::is_same_v<C, QList<T>> || std::is_same_v<C, std::vector<T>>>::type* = nullptr>
typename std::enable_if_t<
std::is_same_v<C, QStringList> || std::is_same_v<C, QList<T>> || std::is_same_v<C, std::vector<T>>>* = nullptr>
auto getValue(tr_variant const* variant)
{
std::optional<C> ret;

View File

@ -156,8 +156,7 @@ void showInfo(app_opts const& opts, tr_torrent_metainfo const& metainfo)
***
**/
auto const n_webseeds = metainfo.webseedCount();
if (n_webseeds > 0)
if (auto const n_webseeds = metainfo.webseedCount(); n_webseeds > 0)
{
printf("\nWEBSEEDS\n\n");
@ -242,8 +241,7 @@ void doScrape(tr_torrent_metainfo const& metainfo)
// execute the http scrape
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_TIMEOUT, TimeoutSecs);
auto const res = curl_easy_perform(curl);
if (res != CURLE_OK)
if (auto const res = curl_easy_perform(curl); res != CURLE_OK)
{
printf("error: %s\n", curl_easy_strerror(res));
continue;