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
This commit is contained in:
Charles Kerr 2022-08-16 09:30:05 -05:00 committed by GitHub
parent 99c21efecc
commit 293f4f6759
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 13 deletions

View File

@ -101,15 +101,22 @@ static void remove_child(std::vector<tr_bandwidth*>& 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)
{

View File

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

View File

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

View File

@ -6,10 +6,10 @@
#pragma once
#include <cstdint> // uint64_t
#include <map>
#include <memory>
#include <QAbstractItemModel>
#include <QMap>
#include <QSet>
#include <libtransmission/tr-macros.h>
@ -95,7 +95,7 @@ private:
FileTreeItem* itemFromIndex(QModelIndex const&) const;
QModelIndexList getOrphanIndices(QModelIndexList const& indices) const;
QMap<int, FileTreeItem*> index_cache_;
std::map<int, FileTreeItem*> index_cache_;
std::unique_ptr<FileTreeItem> root_item_;
bool is_editable_ = {};
};