From d8adecbfb50ca7e93484a29dde52ee005abd2e65 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 23 Oct 2024 20:48:01 -0500 Subject: [PATCH] fix: clang-tidy-20 warnings in transmission-qt (#7192) * fix: modernize-min-max-use-initializer-list warning * fix: readability-container-contains warning * fix: readability-avoid-return-with-void-value warning * fix: readability-math-missing-parentheses warnings * chore: clang-format * fixup! fix: readability-math-missing-parentheses warnings * fix: bugprone-suspicious-stringview-data-usage warning --- qt/FileTreeModel.cc | 2 +- qt/FileTreeView.cc | 2 +- qt/PathButton.cc | 15 +++++++++++++-- qt/SqueezeLabel.cc | 3 ++- qt/Torrent.h | 23 ++++------------------- qt/TorrentDelegate.cc | 10 ++++++---- qt/TorrentDelegateMin.cc | 4 ++-- qt/TrackerDelegate.cc | 6 +++++- qt/VariantHelpers.cc | 2 +- 9 files changed, 35 insertions(+), 32 deletions(-) diff --git a/qt/FileTreeModel.cc b/qt/FileTreeModel.cc index bd31a290f..25962b71a 100644 --- a/qt/FileTreeModel.cc +++ b/qt/FileTreeModel.cc @@ -516,7 +516,7 @@ void FileTreeModel::twiddlePriority(QModelIndexList const& indices) for (int i = TR_PRI_LOW; i <= TR_PRI_HIGH; ++i) { - if (priority_indices.count(i) != 0U) + if (priority_indices.contains(i)) { setPriority(priority_indices[i], i); } diff --git a/qt/FileTreeView.cc b/qt/FileTreeView.cc index 24881d501..8bfce0ab3 100644 --- a/qt/FileTreeView.cc +++ b/qt/FileTreeView.cc @@ -125,7 +125,7 @@ void FileTreeView::resizeEvent(QResizeEvent* event) QString const header_text = model_->headerData(column, Qt::Horizontal).toString(); int const header_width = Utils::measureHeaderItem(this->header(), header_text); - int const width = std::max(min_width, std::max(item_width, header_width)); + int const width = std::max({ min_width, item_width, header_width }); setColumnWidth(column, width); left -= width; diff --git a/qt/PathButton.cc b/qt/PathButton.cc index a4a3a04fa..5cb8b0ccd 100644 --- a/qt/PathButton.cc +++ b/qt/PathButton.cc @@ -85,8 +85,19 @@ void PathButton::paintEvent(QPaintEvent* /*event*/) text_width -= style()->pixelMetric(QStyle::PM_MenuButtonIndicator, &option, this); } - QFileInfo const path_info(path_); - option.text = path_.isEmpty() ? tr("(None)") : (path_info.fileName().isEmpty() ? path_ : path_info.fileName()); + if (path_.isEmpty()) + { + option.text = tr("(None)"); + } + else if (auto const info = QFileInfo{ path_ }; !info.fileName().isEmpty()) + { + option.text = info.fileName(); + } + else + { + option.text = path_; + } + option.text = fontMetrics().elidedText(option.text, Qt::ElideMiddle, text_width); painter.drawComplexControl(QStyle::CC_ToolButton, option); diff --git a/qt/SqueezeLabel.cc b/qt/SqueezeLabel.cc index a68985550..35e50f260 100644 --- a/qt/SqueezeLabel.cc +++ b/qt/SqueezeLabel.cc @@ -69,7 +69,8 @@ void SqueezeLabel::paintEvent(QPaintEvent* paint_event) if (hasFocus() && (textInteractionFlags() & (Qt::TextSelectableByKeyboard | Qt::TextSelectableByMouse)) != 0) { - return QLabel::paintEvent(paint_event); + QLabel::paintEvent(paint_event); + return; } auto painter = QPainter{ this }; diff --git a/qt/Torrent.h b/qt/Torrent.h index 05692e81f..562e8598e 100644 --- a/qt/Torrent.h +++ b/qt/Torrent.h @@ -114,28 +114,13 @@ public: explicit TorrentHash(tr_sha1_digest_t const& data) : data_{ data } { + auto const hashstr = tr_sha1_to_string(data_); + data_str_ = QString::fromUtf8(std::data(hashstr), std::size(hashstr)); } - explicit TorrentHash(char const* str) + explicit TorrentHash(std::string_view const str) + : TorrentHash{ tr_sha1_from_string(str).value_or(tr_sha1_digest_t{}) } { - if (auto const hash = tr_sha1_from_string(str != nullptr ? str : ""); hash) - { - data_ = *hash; - - auto const tmpstr = tr_sha1_to_string(data_); - data_str_ = QString::fromUtf8(std::data(tmpstr), std::size(tmpstr)); - } - } - - explicit TorrentHash(QString const& str) - { - if (auto const hash = tr_sha1_from_string(str.toStdString()); hash) - { - data_ = *hash; - - auto const tmpstr = tr_sha1_to_string(data_); - data_str_ = QString::fromUtf8(std::data(tmpstr), std::size(tmpstr)); - } } [[nodiscard]] TR_CONSTEXPR20 auto operator==(TorrentHash const& that) const diff --git a/qt/TorrentDelegate.cc b/qt/TorrentDelegate.cc index b98e1f0d8..af169795a 100644 --- a/qt/TorrentDelegate.cc +++ b/qt/TorrentDelegate.cc @@ -419,8 +419,10 @@ QString TorrentDelegate::statusString(Torrent const& tor) QSize TorrentDelegate::sizeHint(QStyleOptionViewItem const& option, Torrent const& tor) const { auto const m = margin(*QApplication::style()); - auto const layout = ItemLayout{ tor.name(), progressString(tor), statusString(tor), QIcon{}, - option.font, option.direction, QPoint{ 0, 0 }, option.rect.width() - m.width() * 2 }; + auto const layout = ItemLayout{ + tor.name(), progressString(tor), statusString(tor), QIcon{}, + option.font, option.direction, QPoint{ 0, 0 }, option.rect.width() - (m.width() * 2) + }; return layout.size() + m * 2; } @@ -481,8 +483,8 @@ void TorrentDelegate::setProgressBarPercentDone(QStyleOptionViewItem const& opti progress_bar_style_.direction = option.direction; progress_bar_style_.progress = static_cast( progress_bar_style_.minimum + - (is_magnet ? tor.metadataPercentDone() : tor.percentDone()) * - (progress_bar_style_.maximum - progress_bar_style_.minimum)); + ((is_magnet ? tor.metadataPercentDone() : tor.percentDone()) * + (progress_bar_style_.maximum - progress_bar_style_.minimum))); } } diff --git a/qt/TorrentDelegateMin.cc b/qt/TorrentDelegateMin.cc index 91ece2f2b..b42f4f200 100644 --- a/qt/TorrentDelegateMin.cc +++ b/qt/TorrentDelegateMin.cc @@ -119,7 +119,7 @@ ItemLayout::ItemLayout( bar_style.progress = 100; bar_style.textVisible = true; QSize const bar_size( - bar_style.rect.width() * 2 - style->subElementRect(QStyle::SE_ProgressBarGroove, &bar_style).width(), + (bar_style.rect.width() * 2) - style->subElementRect(QStyle::SE_ProgressBarGroove, &bar_style).width(), bar_style.rect.height()); QRect base_rect{ top_left, @@ -153,7 +153,7 @@ QSize TorrentDelegateMin::sizeHint(QStyleOptionViewItem const& option, Torrent c option.font, option.direction, QPoint{}, - option.rect.width() - m.width() * 2 }; + option.rect.width() - (m.width() * 2) }; return layout.size() + m * 2; } diff --git a/qt/TrackerDelegate.cc b/qt/TrackerDelegate.cc index c2e771f12..75d46da58 100644 --- a/qt/TrackerDelegate.cc +++ b/qt/TrackerDelegate.cc @@ -90,7 +90,11 @@ ItemLayout::ItemLayout( QSize TrackerDelegate::sizeHint(QStyleOptionViewItem const& option, TrackerInfo const& info) const { - ItemLayout const layout{ getText(info), true, option.direction, QPoint{ 0, 0 }, option.rect.width() - Margin.width() * 2 }; + ItemLayout const layout{ getText(info), + true, + option.direction, + QPoint{ 0, 0 }, + option.rect.width() - (Margin.width() * 2) }; return layout.size() + Margin * 2; } diff --git a/qt/VariantHelpers.cc b/qt/VariantHelpers.cc index 240ff6f6c..1df874f18 100644 --- a/qt/VariantHelpers.cc +++ b/qt/VariantHelpers.cc @@ -40,7 +40,7 @@ bool change(Speed& setme, tr_variant const* value) bool change(TorrentHash& setme, tr_variant const* value) { auto const hash_string = getValue(value); - return hash_string && change(setme, TorrentHash(hash_string->data())); + return hash_string && change(setme, TorrentHash{ *hash_string }); } bool change(Peer& setme, tr_variant const* value)