From: Joel Rosdahl Date: Fri, 19 Jun 2020 17:48:19 +0000 (+0200) Subject: Enable more clang-tidy rules and implement suggestions X-Git-Tag: v4.0~375 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=62a4755c2d9b6b2a7cc5dbc3836287c4be23ca0b;p=thirdparty%2Fccache.git Enable more clang-tidy rules and implement suggestions --- diff --git a/src/.clang-tidy b/src/.clang-tidy index 60fc47ee5..e0a0ace9e 100644 --- a/src/.clang-tidy +++ b/src/.clang-tidy @@ -13,23 +13,14 @@ Checks: '-*, -readability-implicit-bool-conversion, -readability-magic-numbers, -readability-else-after-return, - -readability-named-parameter, -readability-qualified-auto, -readability-magic-numbers, - -readability-isolate-declaration, - -readability-inconsistent-declaration-parameter-name, - -readability-simplify-boolean-expr, performance-*, -performance-unnecessary-value-param, - -performance-faster-string-find, modernize-*, -modernize-avoid-c-arrays, - -modernize-deprecated-headers, - -modernize-loop-convert, -modernize-pass-by-value, -modernize-use-auto, - -modernize-use-bool-literals, - -modernize-use-using, -modernize-use-trailing-return-type, cppcoreguidelines-*, -cppcoreguidelines-pro-bounds-array-to-pointer-decay, @@ -41,8 +32,6 @@ Checks: '-*, -cppcoreguidelines-init-variables, -cppcoreguidelines-avoid-c-arrays, -cppcoreguidelines-pro-bounds-constant-array-index, - -cppcoreguidelines-pro-type-cstyle-cast, - -cppcoreguidelines-avoid-goto, -cppcoreguidelines-pro-type-member-init, -cppcoreguidelines-macro-usage, -cppcoreguidelines-pro-type-const-cast, diff --git a/src/Config.cpp b/src/Config.cpp index 575dec567..cab2aa71a 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -380,7 +380,7 @@ parse_line(const std::string& line, if (stripped_line.empty() || stripped_line[0] == '#') { return true; } - size_t equal_pos = stripped_line.find("="); + size_t equal_pos = stripped_line.find('='); if (equal_pos == std::string::npos) { *error_message = "missing equal sign"; return false; diff --git a/src/InodeCache.cpp b/src/InodeCache.cpp index 2b1d99299..e73c35b46 100644 --- a/src/InodeCache.cpp +++ b/src/InodeCache.cpp @@ -294,8 +294,8 @@ InodeCache::create_new_file(const std::string& filename) # ifdef PTHREAD_MUTEX_ROBUST pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST); # endif - for (uint32_t i = 0; i < k_num_buckets; ++i) { - pthread_mutex_init(&sr->buckets[i].mt, &mattr); + for (auto& bucket : sr->buckets) { + pthread_mutex_init(&bucket.mt, &mattr); } munmap(sr, sizeof(SharedRegion)); diff --git a/src/Util.cpp b/src/Util.cpp index bfac8ebbe..488631fc0 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -196,7 +196,8 @@ edit_ansi_csi_seqs(string_view string, const SubstringEditor& editor) { static const std::regex csi_regex( "\x1B\\[[\x30-\x3F]*[\x20-\x2F]*[\x40-\x7E]"); - std::string ret, substr; + std::string ret; + std::string substr; ret.reserve(string.size()); for (std::cregex_token_iterator itr( string.begin(), string.end(), csi_regex, {-1, 0}); @@ -709,15 +710,15 @@ remove_extension(string_view path) } std::vector -split_into_views(string_view s, const char* separators) +split_into_views(string_view input, const char* separators) { - return split_at(s, separators); + return split_at(input, separators); } std::vector -split_into_strings(string_view s, const char* separators) +split_into_strings(string_view input, const char* separators) { - return split_at(s, separators); + return split_at(input, separators); } bool @@ -730,7 +731,7 @@ std::string strip_ansi_csi_seqs(string_view string, string_view strip_actions) { return edit_ansi_csi_seqs( - string, [strip_actions](string_view::size_type, std::string& substr) { + string, [=](string_view::size_type /*pos*/, std::string& substr) { if (strip_actions.find(substr.back()) != string_view::npos) { substr.clear(); } diff --git a/src/Util.hpp b/src/Util.hpp index 8661a7107..a879080f5 100644 --- a/src/Util.hpp +++ b/src/Util.hpp @@ -33,7 +33,7 @@ namespace Util { -using ProgressReceiver = std::function; +using ProgressReceiver = std::function; using CacheFileVisitor = std::function)>; using SubdirVisitor = std::function -#include +#include static void delete_file(const std::string& path, diff --git a/src/compopt.cpp b/src/compopt.cpp index f1b29aeff..67748c1ad 100644 --- a/src/compopt.cpp +++ b/src/compopt.cpp @@ -134,16 +134,16 @@ static const struct compopt compopts[] = { static int compare_compopts(const void* key1, const void* key2) { - const struct compopt* opt1 = (const struct compopt*)key1; - const struct compopt* opt2 = (const struct compopt*)key2; + const struct compopt* opt1 = static_cast(key1); + const struct compopt* opt2 = static_cast(key2); return strcmp(opt1->name, opt2->name); } static int compare_prefix_compopts(const void* key1, const void* key2) { - const struct compopt* opt1 = (const struct compopt*)key1; - const struct compopt* opt2 = (const struct compopt*)key2; + const struct compopt* opt1 = static_cast(key1); + const struct compopt* opt2 = static_cast(key2); return strncmp(opt1->name, opt2->name, strlen(opt2->name)); } diff --git a/src/hash.cpp b/src/hash.cpp index c843b685c..4d8ba65b4 100644 --- a/src/hash.cpp +++ b/src/hash.cpp @@ -160,7 +160,7 @@ hash_string_buffer(struct hash* hash, const char* s, size_t length) void hash_int(struct hash* hash, int x) { - do_hash_buffer(hash, (char*)&x, sizeof(x)); + do_hash_buffer(hash, reinterpret_cast(&x), sizeof(x)); char buf[16]; snprintf(buf, sizeof(buf), "%d", x); diff --git a/src/legacy_util.cpp b/src/legacy_util.cpp index d4c88652d..0f08c5bd3 100644 --- a/src/legacy_util.cpp +++ b/src/legacy_util.cpp @@ -458,13 +458,13 @@ get_extension(const char* path) // Format a size as a human-readable string. Caller frees. char* -format_human_readable_size(uint64_t v) +format_human_readable_size(uint64_t size) { char* s; - if (v >= 1000 * 1000 * 1000) { - s = format("%.1f GB", v / ((double)(1000 * 1000 * 1000))); + if (size >= 1000 * 1000 * 1000) { + s = format("%.1f GB", size / ((double)(1000 * 1000 * 1000))); } else { - s = format("%.1f MB", v / ((double)(1000 * 1000))); + s = format("%.1f MB", size / ((double)(1000 * 1000))); } return s; } @@ -636,15 +636,12 @@ same_executable_name(const char* s1, const char* s2) bool is_full_path(const char* path) { - if (strchr(path, '/')) { - return true; - } #ifdef _WIN32 if (strchr(path, '\\')) { return true; } #endif - return false; + return strchr(path, '/'); } // Update the modification time of a file in the cache to save it from LRU diff --git a/src/manifest.cpp b/src/manifest.cpp index e4294cd63..87abeb938 100644 --- a/src/manifest.cpp +++ b/src/manifest.cpp @@ -380,8 +380,8 @@ write_manifest(const Config& config, writer.write(mf.results.size()); for (const auto& result : mf.results) { writer.write(result.file_info_indexes.size()); - for (uint32_t j = 0; j < result.file_info_indexes.size(); ++j) { - writer.write(result.file_info_indexes[j]); + for (auto index : result.file_info_indexes) { + writer.write(index); } writer.write(result.name.bytes(), Digest::size()); } diff --git a/src/result.cpp b/src/result.cpp index 366405a00..ad858a873 100644 --- a/src/result.cpp +++ b/src/result.cpp @@ -138,7 +138,7 @@ UnderlyingFileTypeIntToString(UnderlyingFileTypeInt underlying_type) } static void -read_embedded_file_entry(const Context&, +read_embedded_file_entry(const Context& /*ctx*/, CacheEntryReader& reader, const std::string& /*result_path_in_cache*/, uint32_t entry_number, @@ -358,7 +358,7 @@ read_result(const Context& ctx, } static void -write_embedded_file_entry(Context&, +write_embedded_file_entry(Context& /*ctx*/, CacheEntryWriter& writer, const std::string& /*result_path_in_cache*/, uint32_t entry_number, diff --git a/src/stats.cpp b/src/stats.cpp index 6ffb0bc39..6676f5520 100644 --- a/src/stats.cpp +++ b/src/stats.cpp @@ -195,7 +195,7 @@ format_timestamp(uint64_t timestamp) { if (timestamp > 0) { struct tm tm; - localtime_r((time_t*)×tamp, &tm); + localtime_r(reinterpret_cast(×tamp), &tm); char buffer[100]; strftime(buffer, sizeof(buffer), "%c", &tm); return format(" %s", buffer); @@ -295,9 +295,9 @@ stats_update_size(Counters& counters, int64_t size, int files) // Read in the stats from one directory and add to the counters. void -stats_read(const std::string& sfile, Counters& counters) +stats_read(const std::string& path, Counters& counters) { - char* data = read_text_file(sfile.c_str(), 1024); + char* data = read_text_file(path.c_str(), 1024); if (data) { parse_stats(counters, data); } @@ -371,7 +371,7 @@ stats_flush_to_file(const Config& config, double factor = config.limit_multiple() / 16; uint64_t max_size = round(config.max_size() * factor); uint32_t max_files = round(config.max_files() * factor); - clean_up_dir(subdir, max_size, max_files, [](double) {}); + clean_up_dir(subdir, max_size, max_files, [](double /*progress*/) {}); } }