From 293f4f6759f647a935f9b41d1a41b218d0edcac0 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 16 Aug 2022 09:30:05 -0500 Subject: [PATCH] refactor: fix sonarcloud warnings (#3649) * refactor: help sonarcloud to see that tr_bandwidth dtor does not throw * refactor: use std::map instead of QMap in qt FileTreeModel --- libtransmission/bandwidth.cc | 17 ++++++++++++----- libtransmission/bandwidth.h | 6 ++++-- qt/FileTreeModel.cc | 12 ++++++++---- qt/FileTreeModel.h | 4 ++-- 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/libtransmission/bandwidth.cc b/libtransmission/bandwidth.cc index 0b93332d2..7ee7f1774 100644 --- a/libtransmission/bandwidth.cc +++ b/libtransmission/bandwidth.cc @@ -101,15 +101,22 @@ static void remove_child(std::vector& v, tr_bandwidth* remove_me) } } +void tr_bandwidth::deparent() noexcept +{ + if (parent_ == nullptr) + { + return; + } + + remove_child(parent_->children_, this); + parent_ = nullptr; +} + void tr_bandwidth::setParent(tr_bandwidth* new_parent) { TR_ASSERT(this != new_parent); - if (this->parent_ != nullptr) - { - remove_child(this->parent_->children_, this); - this->parent_ = nullptr; - } + deparent(); if (new_parent != nullptr) { diff --git a/libtransmission/bandwidth.h b/libtransmission/bandwidth.h index 17f578677..2ef7f489c 100644 --- a/libtransmission/bandwidth.h +++ b/libtransmission/bandwidth.h @@ -88,9 +88,9 @@ public: { } - ~tr_bandwidth() + ~tr_bandwidth() noexcept { - this->setParent(nullptr); + deparent(); } tr_bandwidth& operator=(tr_bandwidth&&) = delete; @@ -119,6 +119,8 @@ public: void setParent(tr_bandwidth* new_parent); + void deparent() noexcept; + [[nodiscard]] constexpr tr_priority_t getPriority() const noexcept { return this->priority_; diff --git a/qt/FileTreeModel.cc b/qt/FileTreeModel.cc index b248a7dc4..787bb5405 100644 --- a/qt/FileTreeModel.cc +++ b/qt/FileTreeModel.cc @@ -286,9 +286,12 @@ void FileTreeModel::clearSubtree(QModelIndex const& top) return; } - if (item->fileIndex() != -1) + if (auto const idx = item->fileIndex(); idx != -1) { - index_cache_.remove(item->fileIndex()); + if (auto const iter = index_cache_.find(idx); iter != std::end(index_cache_)) + { + index_cache_.erase(iter); + } } delete item; @@ -301,12 +304,13 @@ void FileTreeModel::clear() root_item_ = std::make_unique(); endResetModel(); - assert(index_cache_.isEmpty()); + assert(std::empty(index_cache_)); } FileTreeItem* FileTreeModel::findItemForFileIndex(int file_index) const { - return index_cache_.value(file_index, nullptr); + auto iter = index_cache_.find(file_index); + return iter == std::end(index_cache_) ? nullptr : iter->second; } void FileTreeModel::addFile( diff --git a/qt/FileTreeModel.h b/qt/FileTreeModel.h index 2a31d0b70..a7077935a 100644 --- a/qt/FileTreeModel.h +++ b/qt/FileTreeModel.h @@ -6,10 +6,10 @@ #pragma once #include // uint64_t +#include #include #include -#include #include #include @@ -95,7 +95,7 @@ private: FileTreeItem* itemFromIndex(QModelIndex const&) const; QModelIndexList getOrphanIndices(QModelIndexList const& indices) const; - QMap index_cache_; + std::map index_cache_; std::unique_ptr root_item_; bool is_editable_ = {}; };