From 4455673bed0a169535dc5a039afb09f42f10d286 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 3 Feb 2022 07:02:11 -0600 Subject: [PATCH] fix: some use-init-statement sonarcloud warnings (#2566) --- qt/Application.cc | 4 +- qt/FaviconCache.cc | 9 ++-- qt/FileTreeItem.cc | 4 +- qt/FilterBarComboBox.cc | 6 +-- qt/FreeSpaceLabel.cc | 3 +- qt/InteropObject.cc | 4 +- qt/MainWindow.cc | 18 +++----- qt/MakeDialog.cc | 4 +- qt/PathButton.cc | 4 +- qt/Prefs.cc | 7 ++- qt/PrefsDialog.cc | 19 +++----- qt/RpcClient.cc | 7 +-- qt/Session.cc | 96 +++++++++++++++-------------------------- qt/VariantHelpers.h | 12 +++--- qt/WatchDir.cc | 3 +- 15 files changed, 68 insertions(+), 132 deletions(-) diff --git a/qt/Application.cc b/qt/Application.cc index 2bb8d0fcc..30f901cf3 100644 --- a/qt/Application.cc +++ b/qt/Application.cc @@ -605,9 +605,7 @@ void Application::raise() const bool Application::notifyApp(QString const& title, QString const& body, QStringList const& actions) const { #ifdef QT_DBUS_LIB - QDBusConnection bus = QDBusConnection::sessionBus(); - - if (bus.isConnected()) + if (auto bus = QDBusConnection::sessionBus(); bus.isConnected()) { QDBusMessage m = QDBusMessage::createMethodCall( fdo_notifications_service_name_, diff --git a/qt/FaviconCache.cc b/qt/FaviconCache.cc index 81232c7b1..fce88decc 100644 --- a/qt/FaviconCache.cc +++ b/qt/FaviconCache.cc @@ -108,8 +108,7 @@ void FaviconCache::ensureCacheDirHasBeenScanned() // remember which hosts we've asked for a favicon so that we // don't re-ask them every time we start a new session - auto skip_file = QFile(getScrapedFile()); - if (skip_file.open(QIODevice::ReadOnly | QIODevice::Text)) + if (auto skip_file = QFile(getScrapedFile()); skip_file.open(QIODevice::ReadOnly | QIODevice::Text)) { while (!skip_file.atEnd()) { @@ -123,8 +122,7 @@ void FaviconCache::ensureCacheDirHasBeenScanned() // load the cached favicons auto cache_dir = QDir(getCacheDir()); cache_dir.mkpath(cache_dir.absolutePath()); - QStringList const files = cache_dir.entryList(QDir::Files | QDir::Readable); - for (auto const& file : files) + for (auto const& file : cache_dir.entryList(QDir::Files | QDir::Readable)) { QPixmap pixmap(cache_dir.absoluteFilePath(file)); if (!pixmap.isNull()) @@ -183,8 +181,7 @@ FaviconCache::Key FaviconCache::add(QString const& url_str) ensureCacheDirHasBeenScanned(); // find or add this url's key - auto k_it = keys_.find(url_str); - if (k_it != keys_.end()) + if (auto k_it = keys_.find(url_str); k_it != keys_.end()) { return k_it->second; } diff --git a/qt/FileTreeItem.cc b/qt/FileTreeItem.cc index ce60434eb..140fd77e7 100644 --- a/qt/FileTreeItem.cc +++ b/qt/FileTreeItem.cc @@ -57,9 +57,7 @@ FileTreeItem* FileTreeItem::child(QString const& filename) { FileTreeItem* item(nullptr); - int const row = getMyChildRows().value(filename, -1); - - if (row != -1) + if (int const row = getMyChildRows().value(filename, -1); row != -1) { item = child(row); assert(filename == item->name()); diff --git a/qt/FilterBarComboBox.cc b/qt/FilterBarComboBox.cc index 3bcc7313d..842050548 100644 --- a/qt/FilterBarComboBox.cc +++ b/qt/FilterBarComboBox.cc @@ -94,11 +94,9 @@ void FilterBarComboBox::paintEvent(QPaintEvent* e) rect.adjust(2, 1, -2, -1); // draw the icon - QIcon const icon = Utils::getIconFromIndex(model_index); - - if (!icon.isNull()) + if (auto const icon = Utils::getIconFromIndex(model_index); !icon.isNull()) { - QRect const icon_rect = QStyle::alignedRect(opt.direction, Qt::AlignLeft | Qt::AlignVCenter, opt.iconSize, rect); + auto const icon_rect = QStyle::alignedRect(opt.direction, Qt::AlignLeft | Qt::AlignVCenter, opt.iconSize, rect); icon.paint(&painter, icon_rect, Qt::AlignCenter, StyleHelper::getIconMode(opt.state), QIcon::Off); Utils::narrowRect(rect, icon_rect.width() + hmargin, 0, opt.direction); } diff --git a/qt/FreeSpaceLabel.cc b/qt/FreeSpaceLabel.cc index 0a05e381f..354f00234 100644 --- a/qt/FreeSpaceLabel.cc +++ b/qt/FreeSpaceLabel.cc @@ -76,8 +76,7 @@ void FreeSpaceLabel::onTimer() [this](RpcResponse const& r) { // update the label - auto const bytes = dictFind(r.args.get(), TR_KEY_size_bytes); - if (bytes && *bytes > 1) + if (auto const bytes = dictFind(r.args.get(), TR_KEY_size_bytes); bytes && *bytes > 1) { setText(tr("%1 free").arg(Formatter::get().sizeToString(*bytes))); } diff --git a/qt/InteropObject.cc b/qt/InteropObject.cc index 90173dec4..3950ff244 100644 --- a/qt/InteropObject.cc +++ b/qt/InteropObject.cc @@ -22,9 +22,7 @@ bool InteropObject::PresentWindow() const // NOLINTNEXTLINE(readability-identifier-naming) bool InteropObject::AddMetainfo(QString const& metainfo) const { - AddData addme(metainfo); - - if (addme.type != addme.NONE) + if (auto addme = AddData(metainfo); addme.type != addme.NONE) { trApp->addTorrent(addme); } diff --git a/qt/MainWindow.cc b/qt/MainWindow.cc index b0042a1fd..11726e28d 100644 --- a/qt/MainWindow.cc +++ b/qt/MainWindow.cc @@ -658,9 +658,8 @@ void MainWindow::openFolder() } QString const first_file = files.at(0).filename; - int slash_index = first_file.indexOf(QLatin1Char('/')); - if (slash_index > -1) + if (int const slash_index = first_file.indexOf(QLatin1Char('/')); slash_index > -1) { path = path + QLatin1Char('/') + first_file.left(slash_index); } @@ -775,9 +774,8 @@ void MainWindow::onRefreshTimer() void MainWindow::refreshTitle() { QString title(QStringLiteral("Transmission")); - QUrl const url(session_.getRemoteUrl()); - if (!url.isEmpty()) + if (auto const url = QUrl(session_.getRemoteUrl()); !url.isEmpty()) { //: Second (optional) part of main window title "Transmission - host:port" (added when connected to remote session) //: notice that leading space (before the dash) is included here @@ -1271,9 +1269,7 @@ void MainWindow::openTorrent() d->setFileMode(QFileDialog::ExistingFiles); d->setAttribute(Qt::WA_DeleteOnClose); - auto* const l = qobject_cast(d->layout()); - - if (l != nullptr) + if (auto* const l = qobject_cast(d->layout()); l != nullptr) { auto* b = new QCheckBox(tr("Show &options dialog")); b->setChecked(prefs_.getBool(Prefs::OPTIONS_PROMPT)); @@ -1307,9 +1303,7 @@ void MainWindow::addTorrents(QStringList const& filenames) { bool show_options = prefs_.getBool(Prefs::OPTIONS_PROMPT); - auto const* const file_dialog = qobject_cast(sender()); - - if (file_dialog != nullptr) + if (auto const* const file_dialog = qobject_cast(sender()); file_dialog != nullptr) { auto const* const b = file_dialog->findChild(show_options_checkbox_name_); @@ -1586,9 +1580,7 @@ void MainWindow::dropEvent(QDropEvent* event) if (!key.isEmpty()) { - QUrl const url(key); - - if (url.isLocalFile()) + if (auto const url = QUrl(key); url.isLocalFile()) { key = url.toLocalFile(); } diff --git a/qt/MakeDialog.cc b/qt/MakeDialog.cc index ef101fab2..5cb84aea7 100644 --- a/qt/MakeDialog.cc +++ b/qt/MakeDialog.cc @@ -216,9 +216,7 @@ void MakeDialog::onSourceChanged() { builder_.reset(); - QString const filename = getSource(); - - if (!filename.isEmpty()) + if (auto const filename = getSource(); !filename.isEmpty()) { builder_.reset(tr_metaInfoBuilderCreate(filename.toUtf8().constData())); } diff --git a/qt/PathButton.cc b/qt/PathButton.cc index a842d1e98..847a57e23 100644 --- a/qt/PathButton.cc +++ b/qt/PathButton.cc @@ -112,9 +112,7 @@ void PathButton::onClicked() const dialog->setNameFilter(name_filter_); } - QFileInfo const path_info(path_); - - if (!path_.isEmpty() && path_info.exists()) + if (auto const path_info = QFileInfo(path_); !path_.isEmpty() && path_info.exists()) { if (path_info.isDir()) { diff --git a/qt/Prefs.cc b/qt/Prefs.cc index 40d0a8cc0..56ef3e08b 100644 --- a/qt/Prefs.cc +++ b/qt/Prefs.cc @@ -38,8 +38,8 @@ namespace void ensureSoundCommandIsAList(tr_variant* dict) { tr_quark key = TR_KEY_torrent_complete_sound_command; - tr_variant* list = nullptr; - if (tr_variantDictFindList(dict, key, &list)) + + if (tr_variant* list = nullptr; tr_variantDictFindList(dict, key, &list)) { return; } @@ -507,9 +507,8 @@ bool Prefs::getBool(int key) const QString Prefs::getString(int key) const { assert(Items[key].type == QVariant::String); - QByteArray const b = values_[key].toByteArray(); - if (isValidUtf8(b.constData())) + if (auto const b = values_[key].toByteArray(); isValidUtf8(b.constData())) { values_[key].setValue(QString::fromUtf8(b.constData())); } diff --git a/qt/PrefsDialog.cc b/qt/PrefsDialog.cc index dea2d3e24..91139ee4e 100644 --- a/qt/PrefsDialog.cc +++ b/qt/PrefsDialog.cc @@ -206,36 +206,31 @@ void PrefsDialog::linkWidgetToPref(QWidget* widget, int pref_key) updateWidgetValue(widget, pref_key); widgets_.insert(pref_key, widget); - auto const* check_box = qobject_cast(widget); - if (check_box != nullptr) + if (auto const* check_box = qobject_cast(widget); check_box != nullptr) { connect(check_box, &QAbstractButton::toggled, this, &PrefsDialog::checkBoxToggled); return; } - auto const* time_edit = qobject_cast(widget); - if (time_edit != nullptr) + if (auto const* time_edit = qobject_cast(widget); time_edit != nullptr) { connect(time_edit, &QAbstractSpinBox::editingFinished, this, &PrefsDialog::timeEditingFinished); return; } - auto const* line_edit = qobject_cast(widget); - if (line_edit != nullptr) + if (auto const* line_edit = qobject_cast(widget); line_edit != nullptr) { connect(line_edit, &QLineEdit::editingFinished, this, &PrefsDialog::lineEditingFinished); return; } - auto const* path_button = qobject_cast(widget); - if (path_button != nullptr) + if (auto const* path_button = qobject_cast(widget); path_button != nullptr) { connect(path_button, &PathButton::pathChanged, this, &PrefsDialog::pathChanged); return; } - auto const* spin_box = qobject_cast(widget); - if (spin_box != nullptr) + if (auto const* spin_box = qobject_cast(widget); spin_box != nullptr) { connect(spin_box, &QAbstractSpinBox::editingFinished, this, &PrefsDialog::spinBoxEditingFinished); } @@ -681,9 +676,7 @@ void PrefsDialog::setPref(int key, QVariant const& v) void PrefsDialog::sessionUpdated() { - bool const is_local = session_.isLocal(); - - if (is_local_ != is_local) + if (bool const is_local = session_.isLocal(); is_local_ != is_local) { is_local_ = is_local; updateDownloadingWidgetsLocality(); diff --git a/qt/RpcClient.cc b/qt/RpcClient.cc index af510ff51..8d6db4026 100644 --- a/qt/RpcClient.cc +++ b/qt/RpcClient.cc @@ -301,16 +301,13 @@ RpcResponse RpcClient::parseResponseData(tr_variant& json) const { RpcResponse ret; - auto const result = dictFind(&json, TR_KEY_result); - if (result) + if (auto const result = dictFind(&json, TR_KEY_result); result) { ret.result = *result; ret.success = *result == QStringLiteral("success"); } - tr_variant* args; - - if (tr_variantDictFindDict(&json, TR_KEY_arguments, &args)) + if (tr_variant* args = nullptr; tr_variantDictFindDict(&json, TR_KEY_arguments, &args)) { ret.args = createVariant(); *ret.args = *args; diff --git a/qt/Session.cc b/qt/Session.cc index 0c5944dfe..d1ad16b51 100644 --- a/qt/Session.cc +++ b/qt/Session.cc @@ -199,27 +199,23 @@ void Session::updatePref(int key) break; case Prefs::ENCRYPTION: + switch (int const i = prefs_.variant(key).toInt(); i) { - int const i = prefs_.variant(key).toInt(); + case 0: + sessionSet(prefs_.getKey(key), QStringLiteral("tolerated")); + break; - switch (i) - { - case 0: - sessionSet(prefs_.getKey(key), QStringLiteral("tolerated")); - break; - - case 1: - sessionSet(prefs_.getKey(key), QStringLiteral("preferred")); - break; - - case 2: - sessionSet(prefs_.getKey(key), QStringLiteral("required")); - break; - } + case 1: + sessionSet(prefs_.getKey(key), QStringLiteral("preferred")); + break; + case 2: + sessionSet(prefs_.getKey(key), QStringLiteral("required")); break; } + break; + case Prefs::RPC_AUTH_REQUIRED: if (session_ != nullptr) { @@ -796,8 +792,7 @@ void Session::updateBlocklist() q->add( [this](RpcResponse const& r) { - auto const size = dictFind(r.args.get(), TR_KEY_blocklist_size); - if (size) + if (auto const size = dictFind(r.args.get(), TR_KEY_blocklist_size); size) { setBlocklistSize(*size); } @@ -883,9 +878,7 @@ void Session::updateInfo(tr_variant* d) if (i == Prefs::ENCRYPTION) { - auto const str = getValue(b); - - if (str) + if (auto const str = getValue(b); str) { if (*str == QStringLiteral("required")) { @@ -907,68 +900,50 @@ void Session::updateInfo(tr_variant* d) switch (prefs_.type(i)) { case QVariant::Int: + if (auto const value = getValue(b); value) { - auto const value = getValue(b); - - if (value) - { - prefs_.set(i, *value); - } - - break; + prefs_.set(i, *value); } + break; + case QVariant::Double: + if (auto const value = getValue(b); value) { - auto const value = getValue(b); - - if (value) - { - prefs_.set(i, *value); - } - - break; + prefs_.set(i, *value); } + break; + case QVariant::Bool: + if (auto const value = getValue(b); value) { - auto const value = getValue(b); - - if (value) - { - prefs_.set(i, *value); - } - - break; + prefs_.set(i, *value); } + break; + case CustomVariantType::FilterModeType: case CustomVariantType::SortModeType: case QVariant::String: + if (auto const value = getValue(b); value) { - auto const value = getValue(b); - - if (value) - { - prefs_.set(i, *value); - } - - break; + prefs_.set(i, *value); } + break; + default: break; } } - auto const b = dictFind(d, TR_KEY_seedRatioLimited); - if (b) + if (auto const b = dictFind(d, TR_KEY_seedRatioLimited); b) { prefs_.set(Prefs::RATIO_ENABLED, *b); } - auto const x = dictFind(d, TR_KEY_seedRatioLimit); - if (x) + if (auto const x = dictFind(d, TR_KEY_seedRatioLimit); x) { prefs_.set(Prefs::RATIO, *x); } @@ -985,20 +960,17 @@ void Session::updateInfo(tr_variant* d) prefs_.set(Prefs::RPC_WHITELIST, QString::fromUtf8(tr_sessionGetRPCWhitelist(session_))); } - auto const size = dictFind(d, TR_KEY_blocklist_size); - if (size && *size != blocklistSize()) + if (auto const size = dictFind(d, TR_KEY_blocklist_size); size && *size != blocklistSize()) { setBlocklistSize(*size); } - auto str = dictFind(d, TR_KEY_version); - if (str) + if (auto const str = dictFind(d, TR_KEY_version); str) { session_version_ = *str; } - str = dictFind(d, TR_KEY_session_id); - if (str) + if (auto const str = dictFind(d, TR_KEY_session_id); str) { session_id_ = *str; is_definitely_local_session_ = tr_session_id_is_local(session_id_.toUtf8().constData()); diff --git a/qt/VariantHelpers.h b/qt/VariantHelpers.h index 099bc44da..8e1e33217 100644 --- a/qt/VariantHelpers.h +++ b/qt/VariantHelpers.h @@ -32,8 +32,8 @@ template>* = nullp auto getValue(tr_variant const* variant) { std::optional ret; - auto value = T{}; - if (tr_variantGetBool(variant, &value)) + + if (auto value = T{}; tr_variantGetBool(variant, &value)) { ret = value; } @@ -49,8 +49,8 @@ template< auto getValue(tr_variant const* variant) { std::optional ret; - auto value = int64_t{}; - if (tr_variantGetInt(variant, &value)) + + if (auto value = int64_t{}; tr_variantGetInt(variant, &value)) { ret = value; } @@ -62,8 +62,8 @@ template>* = nul auto getValue(tr_variant const* variant) { std::optional ret; - auto value = T{}; - if (tr_variantGetReal(variant, &value)) + + if (auto value = T{}; tr_variantGetReal(variant, &value)) { ret = value; } diff --git a/qt/WatchDir.cc b/qt/WatchDir.cc index dbb661bed..cc86f0702 100644 --- a/qt/WatchDir.cc +++ b/qt/WatchDir.cc @@ -49,9 +49,8 @@ int WatchDir::metainfoTest(QString const& filename) const void WatchDir::onTimeout() { auto* t = qobject_cast(sender()); - QString const filename = t->objectName(); - if (metainfoTest(filename) == OK) + if (auto const filename = t->objectName(); metainfoTest(filename) == OK) { emit torrentFileAdded(filename); }