From 0a00247687889e996fef37b59b12be3e1215398d Mon Sep 17 00:00:00 2001 From: Michael Kazakov Date: Fri, 17 May 2024 23:08:43 +0100 Subject: [PATCH] Enabled more clang-tidy checks --- Source/.clang-tidy | 6 +++++- .../Core/ActionsShortcutsManager.mm | 18 ++++++++++++++---- .../Preferences/PreferencesWindowPanelsTab.mm | 1 + .../States/FilePanels/Actions/Duplicate.mm | 12 +++++++----- .../States/FilePanels/Actions/OpenFile.mm | 1 + .../FilePanels/NCPanelOpenWithMenuDelegate.mm | 2 ++ .../States/FilePanels/PanelHistory.mm | 1 + .../Views/GoToFolderSheetController.mm | 1 + .../AttrsChanging/AttrsChangingDialog.mm | 2 ++ .../source/Compression/CompressionJob.cpp | 2 +- .../Operations/source/Copying/CopyingJob.cpp | 3 ++- Source/Operations/source/Copying/Helpers.cpp | 5 +++-- .../Operations/source/FilenameTextControl.mm | 2 ++ 13 files changed, 42 insertions(+), 14 deletions(-) diff --git a/Source/.clang-tidy b/Source/.clang-tidy index 6abf5f5a8..33e8a7e3e 100644 --- a/Source/.clang-tidy +++ b/Source/.clang-tidy @@ -40,7 +40,11 @@ Checks: > performance-avoid-endl, performance-enum-size, performance-faster-string-find, - performance-for-range-copy + performance-for-range-copy, + performance-implicit-conversion-in-loop, + performance-inefficient-algorithm, + performance-inefficient-string-concatenation, + performance-inefficient-vector-operation CheckOptions: - key: modernize-loop-convert.MinConfidence diff --git a/Source/NimbleCommander/NimbleCommander/Core/ActionsShortcutsManager.mm b/Source/NimbleCommander/NimbleCommander/Core/ActionsShortcutsManager.mm index cd581e643..0b6c81cb2 100644 --- a/Source/NimbleCommander/NimbleCommander/Core/ActionsShortcutsManager.mm +++ b/Source/NimbleCommander/NimbleCommander/Core/ActionsShortcutsManager.mm @@ -414,17 +414,27 @@ }; // clang-format on +template +static constexpr auto make_array_n_impl(T &&value, std::index_sequence) +{ + return std::array, size>{(static_cast(indexes), value)..., std::forward(value)}; +} + +template +static constexpr auto make_array_n(T &&value) +{ + return make_array_n_impl(std::forward(value), std::make_index_sequence{}); +} + static constinit const auto g_ActionToTag = [] { - std::pair items[std::size(g_ActionsTags)] = { - [0 ... std::size(g_ActionsTags) - 1] = {frozen::string(""), 0}}; + auto items = make_array_n(std::pair(frozen::string(""), 0)); for( size_t i = 0; i < std::size(g_ActionsTags); ++i ) items[i] = std::pair(g_ActionsTags[i].first, g_ActionsTags[i].second); return frozen::make_unordered_map(items); }(); static constinit const auto g_TagToAction = [] { - std::pair items[std::size(g_ActionsTags)] = { - [0 ... std::size(g_ActionsTags) - 1] = {0, frozen::string("")}}; + auto items = make_array_n(std::pair(0, frozen::string(""))); for( size_t i = 0; i < std::size(g_ActionsTags); ++i ) items[i] = std::pair(g_ActionsTags[i].second, g_ActionsTags[i].first); return frozen::make_unordered_map(items); diff --git a/Source/NimbleCommander/NimbleCommander/Preferences/PreferencesWindowPanelsTab.mm b/Source/NimbleCommander/NimbleCommander/Preferences/PreferencesWindowPanelsTab.mm index b7374ce54..13512e3c9 100644 --- a/Source/NimbleCommander/NimbleCommander/Preferences/PreferencesWindowPanelsTab.mm +++ b/Source/NimbleCommander/NimbleCommander/Preferences/PreferencesWindowPanelsTab.mm @@ -902,6 +902,7 @@ - (void)acceptFSTags:(const std::vector &)_tags m_TagsStorage->Set(m_Tags); [self.tagsTable reloadData]; std::vector labels; + labels.reserve(added.size()); for( auto &tag : added ) labels.push_back(fmt::format("“{}”", tag.Label())); diff --git a/Source/NimbleCommander/NimbleCommander/States/FilePanels/Actions/Duplicate.mm b/Source/NimbleCommander/NimbleCommander/States/FilePanels/Actions/Duplicate.mm index 3b546a8fc..4d31c1585 100644 --- a/Source/NimbleCommander/NimbleCommander/States/FilePanels/Actions/Duplicate.mm +++ b/Source/NimbleCommander/NimbleCommander/States/FilePanels/Actions/Duplicate.mm @@ -1,4 +1,4 @@ -// Copyright (C) 2017-2023 Michael Kazakov. Subject to GNU General Public License version 3. +// Copyright (C) 2017-2024 Michael Kazakov. Subject to GNU General Public License version 3. #include "Duplicate.h" #include "../PanelController.h" #include @@ -14,6 +14,7 @@ #include #include "Helpers.h" #include +#include namespace nc::panel::actions { @@ -137,14 +138,15 @@ static void CommonPerform(PanelController *_target, const std::vector items; + items.reserve(_items.size()); for( auto &i : _items ) items.emplace_back(i.Path()); _file_opener.Open(items, _items.front().Host(), nil, _target); diff --git a/Source/NimbleCommander/NimbleCommander/States/FilePanels/NCPanelOpenWithMenuDelegate.mm b/Source/NimbleCommander/NimbleCommander/States/FilePanels/NCPanelOpenWithMenuDelegate.mm index 207770c1c..863a52133 100644 --- a/Source/NimbleCommander/NimbleCommander/States/FilePanels/NCPanelOpenWithMenuDelegate.mm +++ b/Source/NimbleCommander/NimbleCommander/States/FilePanels/NCPanelOpenWithMenuDelegate.mm @@ -51,6 +51,7 @@ static void SortAndPurgeDuplicateHandlers(std::vector &_ha static FetchResult FetchHandlers(const std::vector &_items, const UTIDB &_db) { std::vector per_item_handlers; + per_item_handlers.reserve(_items.size()); for( auto &i : _items ) per_item_handlers.emplace_back(i, _db); @@ -321,6 +322,7 @@ - (void)openItemsWithHandler:(const LaunchServiceHandler &)_handler }); if( same_host ) { std::vector items; + items.reserve(source_items.size()); for( auto &i : source_items ) items.emplace_back(i.Path()); m_FileOpener->Open(items, source_items.front().Host(), _handler.Identifier(), self.target); diff --git a/Source/NimbleCommander/NimbleCommander/States/FilePanels/PanelHistory.mm b/Source/NimbleCommander/NimbleCommander/States/FilePanels/PanelHistory.mm index 98019c6f8..914b9927d 100644 --- a/Source/NimbleCommander/NimbleCommander/States/FilePanels/PanelHistory.mm +++ b/Source/NimbleCommander/NimbleCommander/States/FilePanels/PanelHistory.mm @@ -119,6 +119,7 @@ std::vector> History::All() const { std::vector> res; + res.reserve(m_History.size()); for( auto &i : m_History ) res.emplace_back(std::cref(i)); return res; diff --git a/Source/NimbleCommander/NimbleCommander/States/FilePanels/Views/GoToFolderSheetController.mm b/Source/NimbleCommander/NimbleCommander/States/FilePanels/Views/GoToFolderSheetController.mm index a3772dcf1..eeb76bb1a 100644 --- a/Source/NimbleCommander/NimbleCommander/States/FilePanels/Views/GoToFolderSheetController.mm +++ b/Source/NimbleCommander/NimbleCommander/States/FilePanels/Views/GoToFolderSheetController.mm @@ -155,6 +155,7 @@ - (BOOL)control:(NSControl *) [[maybe_unused]] control - (NSMenu *)buildMenuWithElements:(const std::vector &)_inds ofListing:(const VFSListing &)_listing { std::vector filenames; + filenames.reserve(_inds.size()); for( auto i : _inds ) filenames.emplace_back(_listing.FilenameNS(i)); diff --git a/Source/Operations/source/AttrsChanging/AttrsChangingDialog.mm b/Source/Operations/source/AttrsChanging/AttrsChangingDialog.mm index 11751a6f8..8280fa63a 100644 --- a/Source/Operations/source/AttrsChanging/AttrsChangingDialog.mm +++ b/Source/Operations/source/AttrsChanging/AttrsChangingDialog.mm @@ -699,6 +699,7 @@ static auto optional_common_value(_InputIterator _first, _InputIterator _last, _ static AttrsChangingCommand::Permissions ExtractCommonPermissions(const std::vector &_items) { std::vector modes; + modes.reserve(_items.size()); for( const auto &i : _items ) modes.emplace_back(i.UnixMode()); @@ -732,6 +733,7 @@ static auto optional_common_value(_InputIterator _first, _InputIterator _last, _ static AttrsChangingCommand::Flags ExtractCommonFlags(const std::vector &_items) { std::vector flags; + flags.reserve(_items.size()); for( const auto &i : _items ) flags.emplace_back(i.UnixFlags()); diff --git a/Source/Operations/source/Compression/CompressionJob.cpp b/Source/Operations/source/Compression/CompressionJob.cpp index 5e8c9c84b..c653d6281 100644 --- a/Source/Operations/source/Compression/CompressionJob.cpp +++ b/Source/Operations/source/Compression/CompressionJob.cpp @@ -518,7 +518,7 @@ bool CompressionJob::ScanItem(const std::string &_full_path, const auto directory_node = &_ctx.filenames.back(); for( const std::string &filename : directory_entries ) - if( !ScanItem(_full_path + "/" + filename, + if( !ScanItem(fmt::format("{}/{}", _full_path, filename), filename, meta.base_vfs_indx, meta.base_path_indx, diff --git a/Source/Operations/source/Copying/CopyingJob.cpp b/Source/Operations/source/Copying/CopyingJob.cpp index e88b6eed1..3d10cd0db 100644 --- a/Source/Operations/source/Copying/CopyingJob.cpp +++ b/Source/Operations/source/Copying/CopyingJob.cpp @@ -16,6 +16,7 @@ #include #include #include +#include using namespace nc::ops::copying; @@ -670,7 +671,7 @@ std::tuple CopyingJob::ScanSourceItems() return StepResult::Stop; // go into recursion - scan_item(my_indx, _full_relative_path + '/' + entry, entry); + scan_item(my_indx, fmt::format("{}/{}", _full_relative_path, entry), entry); } } } diff --git a/Source/Operations/source/Copying/Helpers.cpp b/Source/Operations/source/Copying/Helpers.cpp index 0879f22af..0af4b0e59 100644 --- a/Source/Operations/source/Copying/Helpers.cpp +++ b/Source/Operations/source/Copying/Helpers.cpp @@ -1,5 +1,6 @@ -// Copyright (C) 2018-2023 Michael Kazakov. Subject to GNU General Public License version 3. +// Copyright (C) 2018-2024 Michael Kazakov. Subject to GNU General Public License version 3. #include "Helpers.h" +#include namespace nc::ops::copying { @@ -19,7 +20,7 @@ FindNonExistingItemPath(const std::string &_orig_existing_path, VFSHost &_host, for( int check_index = 2; /*noop*/; ++check_index ) { if( _cancel_checker && _cancel_checker() ) return ""; - auto path = epilog + std::to_string(check_index) + prologue; + auto path = fmt::format("{}{}{}", epilog, check_index, prologue); if( _host.Exists(path.c_str(), _cancel_checker) == false ) { if( _cancel_checker && _cancel_checker() ) return ""; diff --git a/Source/Operations/source/FilenameTextControl.mm b/Source/Operations/source/FilenameTextControl.mm index a15bd464d..28f0d7b61 100644 --- a/Source/Operations/source/FilenameTextControl.mm +++ b/Source/Operations/source/FilenameTextControl.mm @@ -135,6 +135,7 @@ - (NSMenu *)buildMenuWithSuggestions:(const std::vector &)_suggesti forCurrentPath:(const std::string &)_current_path { std::vector directories; + directories.reserve(_suggestions.size()); for( const auto &suggestion : _suggestions ) directories.emplace_back([NSString stringWithUTF8StdString:suggestion]); @@ -211,6 +212,7 @@ - (void)updateTextView:(NSTextView *)_text_view withAutocompetion:(const std::st const auto indices = ListDirsWithPrefix(*listing, filename); std::vector directories; + directories.reserve(indices.size()); for( auto index : indices ) directories.emplace_back(listing->Filename(index));