diff --git a/gtk/Application.cc b/gtk/Application.cc index 4dff344a9..703a48ff0 100644 --- a/gtk/Application.cc +++ b/gtk/Application.cc @@ -813,9 +813,7 @@ void Application::Impl::on_drag_data_received( guint /*info*/, guint time_) { - auto const uris = selection_data.get_uris(); - - if (!uris.empty()) + if (auto const uris = selection_data.get_uris(); !uris.empty()) { auto files = std::vector>(); files.reserve(uris.size()); diff --git a/libtransmission/completion.cc b/libtransmission/completion.cc index 9f51e33c4..e98837ee7 100644 --- a/libtransmission/completion.cc +++ b/libtransmission/completion.cc @@ -7,6 +7,7 @@ */ #include +#include #include #include "transmission.h" @@ -134,13 +135,12 @@ std::vector tr_completion::createPieceBitfield() const size_t const n = block_info_->n_pieces; auto pieces = tr_bitfield{ n }; - bool* const flags = new bool[n]; + auto flags = std::make_unique(n); for (tr_piece_index_t piece = 0; piece < n; ++piece) { flags[piece] = hasPiece(piece); } - pieces.setFromBools(flags, n); - delete[] flags; + pieces.setFromBools(flags.get(), n); return pieces.raw(); } diff --git a/libtransmission/completion.h b/libtransmission/completion.h index 33eaad175..ebdbd3827 100644 --- a/libtransmission/completion.h +++ b/libtransmission/completion.h @@ -30,6 +30,8 @@ struct tr_completion struct torrent_view { virtual bool pieceIsDnd(tr_piece_index_t piece) const = 0; + + virtual ~torrent_view() = default; }; explicit tr_completion(torrent_view const* tor, tr_block_info const* block_info) diff --git a/libtransmission/peer-io.cc b/libtransmission/peer-io.cc index 00bd40708..880b7c1d4 100644 --- a/libtransmission/peer-io.cc +++ b/libtransmission/peer-io.cc @@ -195,7 +195,7 @@ static void canReadWrapper(tr_peerIo* io) tr_peerIoRef(io); - tr_session* const session = io->session; + tr_session const* const session = io->session; /* try to consume the input buffer */ if (io->canRead != nullptr) diff --git a/libtransmission/peer-mgr-wishlist.cc b/libtransmission/peer-mgr-wishlist.cc index bfbae912b..c661d751b 100644 --- a/libtransmission/peer-mgr-wishlist.cc +++ b/libtransmission/peer-mgr-wishlist.cc @@ -102,7 +102,7 @@ std::vector getCandidates(Wishlist::PeerInfo const& peer_info) return candidates; } -static std::vector makeSpans(tr_block_index_t const* sorted_blocks, size_t n_blocks) +std::vector makeSpans(tr_block_index_t const* sorted_blocks, size_t n_blocks) { if (n_blocks == 0) { @@ -130,7 +130,7 @@ static std::vector makeSpans(tr_block_index_t const* sorted_blo } // namespace -std::vector Wishlist::next(Wishlist::PeerInfo const& peer_info, size_t n_wanted_blocks) +std::vector Wishlist::next(Wishlist::PeerInfo const& peer_info, size_t n_wanted_blocks) const { size_t n_blocks = 0; auto spans = std::vector{}; @@ -167,8 +167,7 @@ std::vector Wishlist::next(Wishlist::PeerInfo const& peer_info, // don't request from too many peers size_t const n_peers = peer_info.countActiveRequests(block); - size_t const max_peers = peer_info.isEndgame() ? 2 : 1; - if (n_peers >= max_peers) + if (size_t const max_peers = peer_info.isEndgame() ? 2 : 1; n_peers >= max_peers) { continue; } diff --git a/libtransmission/peer-mgr-wishlist.h b/libtransmission/peer-mgr-wishlist.h index b3f47b162..e1cf8da38 100644 --- a/libtransmission/peer-mgr-wishlist.h +++ b/libtransmission/peer-mgr-wishlist.h @@ -31,8 +31,9 @@ public: virtual tr_block_span_t blockSpan(tr_piece_index_t) const = 0; virtual tr_piece_index_t countAllPieces() const = 0; virtual tr_priority_t priority(tr_piece_index_t) const = 0; + virtual ~PeerInfo() = default; }; // get a list of the next blocks that we should request from a peer - std::vector next(PeerInfo const& peer_info, size_t n_wanted_blocks); + std::vector next(PeerInfo const& peer_info, size_t n_wanted_blocks) const; }; diff --git a/libtransmission/peer-mgr.cc b/libtransmission/peer-mgr.cc index 4bedbd7b9..a3fb85005 100644 --- a/libtransmission/peer-mgr.cc +++ b/libtransmission/peer-mgr.cc @@ -506,8 +506,8 @@ void tr_peerMgrSetUtpFailed(tr_torrent* tor, tr_address const* addr, bool failed *** *** 1. tr_swarm::active_requests, an opaque class that tracks what requests *** we currently have, i.e. which blocks and from which peers. -*** This is used for (a) cancelling requests that have been waiting -*** for too long and (b) avoiding duplicate requests. +*** This is used for cancelling requests that have been waiting +*** for too long and avoiding duplicate requests. *** *** 2. tr_swarm::pieces, an array of "struct weighted_piece" which lists the *** pieces that we want to request. It's used to decide which blocks to @@ -541,7 +541,6 @@ static int countActiveWebseeds(tr_swarm* s) // TODO: if we keep this, add equivalent API to ActiveRequest void tr_peerMgrClientSentRequests(tr_torrent* torrent, tr_peer* peer, tr_block_span_t span) { - // std::cout << __FILE__ << ':' << __LINE__ << " tr_peerMgrClientSentRequests [" << range.begin << "..." << range.end << ')' << std::endl; auto const now = tr_time(); for (tr_block_index_t block = span.begin; block < span.end; ++block) @@ -569,6 +568,8 @@ std::vector tr_peerMgrGetNextRequests(tr_torrent* torrent, tr_p { } + ~PeerInfoImpl() override = default; + bool clientCanRequestBlock(tr_block_index_t block) const override { return !torrent_->hasBlock(block) && !swarm_->active_requests.has(block, peer_); diff --git a/libtransmission/peer-msgs.cc b/libtransmission/peer-msgs.cc index 5979a0f9b..ce35e8cea 100644 --- a/libtransmission/peer-msgs.cc +++ b/libtransmission/peer-msgs.cc @@ -2085,7 +2085,6 @@ static void updateBlockRequests(tr_peerMsgsImpl* msgs) TR_ASSERT(msgs->is_client_interested()); TR_ASSERT(!msgs->is_client_choked()); - // std::cout << __FILE__ << ':' << __LINE__ << " wants " << n_wanted << " blocks to request" << std::endl; for (auto const span : tr_peerMgrGetNextRequests(msgs->torrent, msgs, n_wanted)) { for (tr_block_index_t block = span.begin; block < span.end; ++block) @@ -2093,7 +2092,6 @@ static void updateBlockRequests(tr_peerMsgsImpl* msgs) protocolSendRequest(msgs, blockToReq(msgs->torrent, block)); } - // std::cout << __FILE__ << ':' << __LINE__ << " peer " << (void*)msgs << " requested " << span.end - span.begin << " blocks" << std::endl; tr_peerMgrClientSentRequests(msgs->torrent, msgs, span); } } diff --git a/libtransmission/torrent-ctor.cc b/libtransmission/torrent-ctor.cc index c4abc47c3..25f1e1180 100644 --- a/libtransmission/torrent-ctor.cc +++ b/libtransmission/torrent-ctor.cc @@ -131,8 +131,7 @@ int tr_ctorSetMetainfoFromFile(tr_ctor* ctor, char const* filename) return EILSEQ; } - int const err = parseMetainfoContents(ctor); - if (err) + if (int const err = parseMetainfoContents(ctor); err != 0) { clearMetainfo(ctor); return err; @@ -141,9 +140,7 @@ int tr_ctorSetMetainfoFromFile(tr_ctor* ctor, char const* filename) setSourceFile(ctor, filename); /* if no `name' field was set, then set it from the filename */ - tr_variant* info = nullptr; - - if (tr_variantDictFindDict(&ctor->metainfo, TR_KEY_info, &info)) + if (tr_variant* info = nullptr; tr_variantDictFindDict(&ctor->metainfo, TR_KEY_info, &info)) { auto name = std::string_view{}; diff --git a/libtransmission/torrent.cc b/libtransmission/torrent.cc index 2e64b7551..8559db08c 100644 --- a/libtransmission/torrent.cc +++ b/libtransmission/torrent.cc @@ -7,6 +7,7 @@ */ #include /* EINVAL */ +#include #include /* EINVAL */ #include /* INT_MAX */ #include @@ -1932,7 +1933,7 @@ static void torrentCallScript(tr_torrent const* tor, char const* script) char* const torrent_dir = tr_sys_path_native_separators(tr_strdup(tor->currentDir)); - char const* const cmd[] = { + auto const cmd = std::array{ script, nullptr, }; @@ -1952,7 +1953,7 @@ static void torrentCallScript(tr_torrent const* tor, char const* script) tr_error* error = nullptr; - if (!tr_spawn_async(cmd, env, TR_IF_WIN32("\\", "/"), &error)) + if (!tr_spawn_async(std::data(cmd), env, TR_IF_WIN32("\\", "/"), &error)) { tr_logAddTorErr(tor, "Error executing script \"%s\" (%d): %s", script, error->code, error->message); tr_error_free(error); diff --git a/libtransmission/torrent.h b/libtransmission/torrent.h index b0f3ba10c..0cad3ea29 100644 --- a/libtransmission/torrent.h +++ b/libtransmission/torrent.h @@ -134,7 +134,7 @@ public: { } - virtual ~tr_torrent() = default; + virtual ~tr_torrent() override = default; void setLocation( std::string_view location, @@ -161,7 +161,6 @@ public: // private: void swapMetainfo(tr_metainfo_parsed& parsed); -public: auto unique_lock() const { return session->unique_lock(); @@ -337,7 +336,6 @@ public: std::optional findFile(std::string& filename, tr_file_index_t i) const; -public: tr_info info = {}; tr_bitfield dnd_pieces_ = tr_bitfield{ 0 }; diff --git a/libtransmission/tr-dht.cc b/libtransmission/tr-dht.cc index 853532a32..92ffa2482 100644 --- a/libtransmission/tr-dht.cc +++ b/libtransmission/tr-dht.cc @@ -365,8 +365,7 @@ int tr_dhtInit(tr_session* ss) tr_rand_buffer(myid, 20); } - int rc = dht_init(ss->udp_socket, ss->udp6_socket, myid, nullptr); - if (rc < 0) + if (int rc = dht_init(ss->udp_socket, ss->udp6_socket, myid, nullptr); rc < 0) { tr_free(nodes6); tr_free(nodes); diff --git a/libtransmission/variant-benc.cc b/libtransmission/variant-benc.cc index 123e4fbe1..01abe1045 100644 --- a/libtransmission/variant-benc.cc +++ b/libtransmission/variant-benc.cc @@ -186,50 +186,43 @@ int tr_variantParseBenc(tr_variant& top, int parse_opts, std::string_view benc, break; } - tr_variant* const v = get_node(stack, key, &top, &err); - if (v != nullptr) + if (tr_variant* const v = get_node(stack, key, &top, &err); v != nullptr) { tr_variantInitInt(v, *value); } break; } case 'l': // list - { - benc.remove_prefix(1); + benc.remove_prefix(1); - tr_variant* const v = get_node(stack, key, &top, &err); - if (v != nullptr) - { - tr_variantInitList(v, 0); - stack.push_back(v); - } - break; + if (tr_variant* const v = get_node(stack, key, &top, &err); v != nullptr) + { + tr_variantInitList(v, 0); + stack.push_back(v); } + break; + case 'd': // dict - { - benc.remove_prefix(1); + benc.remove_prefix(1); - tr_variant* const v = get_node(stack, key, &top, &err); - if (v != nullptr) - { - tr_variantInitDict(v, 0); - stack.push_back(v); - } - break; + if (tr_variant* const v = get_node(stack, key, &top, &err); v != nullptr) + { + tr_variantInitDict(v, 0); + stack.push_back(v); } + break; case 'e': // end of list or dict + benc.remove_prefix(1); + + if (std::empty(stack) || key) { - benc.remove_prefix(1); - - if (std::empty(stack) || key) - { - err = EILSEQ; - break; - } - - stack.pop_back(); + err = EILSEQ; break; } + + stack.pop_back(); + break; + case '0': case '1': case '2': diff --git a/qt/FileTreeModel.cc b/qt/FileTreeModel.cc index 54cb1afba..cf8f1aa47 100644 --- a/qt/FileTreeModel.cc +++ b/qt/FileTreeModel.cc @@ -92,7 +92,7 @@ public: FileTreeModel::FileTreeModel(QObject* parent, bool is_editable) : QAbstractItemModel{ parent } - , root_item_{ new FileTreeItem } + , root_item_{ std::make_unique() } , is_editable_{ is_editable } { } @@ -100,8 +100,6 @@ FileTreeModel::FileTreeModel(QObject* parent, bool is_editable) FileTreeModel::~FileTreeModel() { clear(); - - delete root_item_; } void FileTreeModel::setEditable(bool is_editable) @@ -233,7 +231,7 @@ QModelIndex FileTreeModel::index(int row, int column, QModelIndex const& parent) if (!parent.isValid()) { - parent_item = root_item_; + parent_item = root_item_.get(); } else { @@ -270,7 +268,7 @@ QModelIndex FileTreeModel::parent(QModelIndex const& child, int column) const int FileTreeModel::rowCount(QModelIndex const& parent) const { - FileTreeItem const* parent_item = parent.isValid() ? itemFromIndex(parent) : root_item_; + FileTreeItem const* parent_item = parent.isValid() ? itemFromIndex(parent) : root_item_.get(); return parent_item->childCount(); } @@ -284,7 +282,7 @@ int FileTreeModel::columnCount(QModelIndex const& parent) const QModelIndex FileTreeModel::indexOf(FileTreeItem* item, int column) const { - if (item == nullptr || item == root_item_) + if (item == nullptr || item == root_item_.get()) { return QModelIndex(); } @@ -320,8 +318,7 @@ void FileTreeModel::clear() { beginResetModel(); clearSubtree(QModelIndex()); - delete root_item_; - root_item_ = new FileTreeItem{}; + root_item_ = std::make_unique(); endResetModel(); assert(index_cache_.isEmpty()); @@ -368,7 +365,7 @@ void FileTreeModel::addFile( item = item->parent(); } - assert(item == root_item_); + assert(item == root_item_.get()); if (index_with_changed_parents.isValid()) { @@ -379,7 +376,7 @@ void FileTreeModel::addFile( { bool added = false; - item = root_item_; + item = root_item_.get(); BackwardPathIterator filename_it(filename); while (filename_it.hasNext()) @@ -411,7 +408,7 @@ void FileTreeModel::addFile( item = child; } - if (item != root_item_) + if (item != root_item_.get()) { assert(item->fileIndex() == file_index); assert(item->totalSize() == total_size); diff --git a/qt/FileTreeModel.h b/qt/FileTreeModel.h index 8a7390c6f..5c214049e 100644 --- a/qt/FileTreeModel.h +++ b/qt/FileTreeModel.h @@ -9,6 +9,7 @@ #pragma once #include // uint64_t +#include #include #include @@ -98,6 +99,6 @@ private: QModelIndexList getOrphanIndices(QModelIndexList const& indices) const; QMap index_cache_; - FileTreeItem* root_item_ = {}; + std::unique_ptr root_item_; bool is_editable_ = {}; }; diff --git a/qt/VariantHelpers.h b/qt/VariantHelpers.h index 6f37e5783..bad7876e3 100644 --- a/qt/VariantHelpers.h +++ b/qt/VariantHelpers.h @@ -77,8 +77,8 @@ template>::type* auto getValue(tr_variant const* variant) { std::optional ret; - auto sv = std::string_view{}; - if (tr_variantGetStrView(variant, &sv)) + + if (auto sv = std::string_view{}; tr_variantGetStrView(variant, &sv)) { ret = QString::fromUtf8(std::data(sv), std::size(sv)); } @@ -90,8 +90,8 @@ template auto getValue(tr_variant const* variant) { std::optional ret; - auto sv = std::string_view{}; - if (tr_variantGetStrView(variant, &sv)) + + if (auto sv = std::string_view{}; tr_variantGetStrView(variant, &sv)) { ret = std::string_view(std::data(sv), std::size(sv)); } @@ -181,8 +181,8 @@ template auto dictFind(tr_variant* dict, tr_quark key) { std::optional ret; - auto const* child = tr_variantDictFind(dict, key); - if (child != nullptr) + + if (auto const* child = tr_variantDictFind(dict, key); child != nullptr) { ret = getValue(child); }