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

This commit is contained in:
Charles Kerr 2022-02-03 07:02:11 -06:00 committed by GitHub
parent 5565255d85
commit 4455673bed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 68 additions and 132 deletions

View File

@ -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_,

View File

@ -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;
}

View File

@ -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());

View File

@ -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);
}

View File

@ -76,8 +76,7 @@ void FreeSpaceLabel::onTimer()
[this](RpcResponse const& r)
{
// update the label
auto const bytes = dictFind<int64_t>(r.args.get(), TR_KEY_size_bytes);
if (bytes && *bytes > 1)
if (auto const bytes = dictFind<int64_t>(r.args.get(), TR_KEY_size_bytes); bytes && *bytes > 1)
{
setText(tr("%1 free").arg(Formatter::get().sizeToString(*bytes)));
}

View File

@ -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);
}

View File

@ -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<QGridLayout*>(d->layout());
if (l != nullptr)
if (auto* const l = qobject_cast<QGridLayout*>(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<QFileDialog const*>(sender());
if (file_dialog != nullptr)
if (auto const* const file_dialog = qobject_cast<QFileDialog const*>(sender()); file_dialog != nullptr)
{
auto const* const b = file_dialog->findChild<QCheckBox const*>(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();
}

View File

@ -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()));
}

View File

@ -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())
{

View File

@ -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()));
}

View File

@ -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<QCheckBox*>(widget);
if (check_box != nullptr)
if (auto const* check_box = qobject_cast<QCheckBox*>(widget); check_box != nullptr)
{
connect(check_box, &QAbstractButton::toggled, this, &PrefsDialog::checkBoxToggled);
return;
}
auto const* time_edit = qobject_cast<QTimeEdit*>(widget);
if (time_edit != nullptr)
if (auto const* time_edit = qobject_cast<QTimeEdit*>(widget); time_edit != nullptr)
{
connect(time_edit, &QAbstractSpinBox::editingFinished, this, &PrefsDialog::timeEditingFinished);
return;
}
auto const* line_edit = qobject_cast<QLineEdit*>(widget);
if (line_edit != nullptr)
if (auto const* line_edit = qobject_cast<QLineEdit*>(widget); line_edit != nullptr)
{
connect(line_edit, &QLineEdit::editingFinished, this, &PrefsDialog::lineEditingFinished);
return;
}
auto const* path_button = qobject_cast<PathButton*>(widget);
if (path_button != nullptr)
if (auto const* path_button = qobject_cast<PathButton*>(widget); path_button != nullptr)
{
connect(path_button, &PathButton::pathChanged, this, &PrefsDialog::pathChanged);
return;
}
auto const* spin_box = qobject_cast<QAbstractSpinBox*>(widget);
if (spin_box != nullptr)
if (auto const* spin_box = qobject_cast<QAbstractSpinBox*>(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();

View File

@ -301,16 +301,13 @@ RpcResponse RpcClient::parseResponseData(tr_variant& json) const
{
RpcResponse ret;
auto const result = dictFind<QString>(&json, TR_KEY_result);
if (result)
if (auto const result = dictFind<QString>(&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;

View File

@ -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<int>(r.args.get(), TR_KEY_blocklist_size);
if (size)
if (auto const size = dictFind<int>(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<QString>(b);
if (str)
if (auto const str = getValue<QString>(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<int>(b); value)
{
auto const value = getValue<int>(b);
if (value)
{
prefs_.set(i, *value);
}
break;
prefs_.set(i, *value);
}
break;
case QVariant::Double:
if (auto const value = getValue<double>(b); value)
{
auto const value = getValue<double>(b);
if (value)
{
prefs_.set(i, *value);
}
break;
prefs_.set(i, *value);
}
break;
case QVariant::Bool:
if (auto const value = getValue<bool>(b); value)
{
auto const value = getValue<bool>(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<QString>(b); value)
{
auto const value = getValue<QString>(b);
if (value)
{
prefs_.set(i, *value);
}
break;
prefs_.set(i, *value);
}
break;
default:
break;
}
}
auto const b = dictFind<bool>(d, TR_KEY_seedRatioLimited);
if (b)
if (auto const b = dictFind<bool>(d, TR_KEY_seedRatioLimited); b)
{
prefs_.set(Prefs::RATIO_ENABLED, *b);
}
auto const x = dictFind<double>(d, TR_KEY_seedRatioLimit);
if (x)
if (auto const x = dictFind<double>(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<int>(d, TR_KEY_blocklist_size);
if (size && *size != blocklistSize())
if (auto const size = dictFind<int>(d, TR_KEY_blocklist_size); size && *size != blocklistSize())
{
setBlocklistSize(*size);
}
auto str = dictFind<QString>(d, TR_KEY_version);
if (str)
if (auto const str = dictFind<QString>(d, TR_KEY_version); str)
{
session_version_ = *str;
}
str = dictFind<QString>(d, TR_KEY_session_id);
if (str)
if (auto const str = dictFind<QString>(d, TR_KEY_session_id); str)
{
session_id_ = *str;
is_definitely_local_session_ = tr_session_id_is_local(session_id_.toUtf8().constData());

View File

@ -32,8 +32,8 @@ template<typename T, typename std::enable_if_t<std::is_same_v<T, bool>>* = nullp
auto getValue(tr_variant const* variant)
{
std::optional<T> 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<T> 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<typename T, typename std::enable_if_t<std::is_same_v<T, double>>* = nul
auto getValue(tr_variant const* variant)
{
std::optional<T> ret;
auto value = T{};
if (tr_variantGetReal(variant, &value))
if (auto value = T{}; tr_variantGetReal(variant, &value))
{
ret = value;
}

View File

@ -49,9 +49,8 @@ int WatchDir::metainfoTest(QString const& filename) const
void WatchDir::onTimeout()
{
auto* t = qobject_cast<QTimer*>(sender());
QString const filename = t->objectName();
if (metainfoTest(filename) == OK)
if (auto const filename = t->objectName(); metainfoTest(filename) == OK)
{
emit torrentFileAdded(filename);
}