From 74c65164a00412654c9ea5c6d47a42c6b8d723ce Mon Sep 17 00:00:00 2001 From: KRM7 <70973547+KRM7@users.noreply.github.com> Date: Tue, 5 Sep 2023 21:13:25 +0200 Subject: [PATCH] clean up clang tidy warnings --- .clang-tidy | 7 +++++-- CMakeLists.txt | 3 ++- CMakeSettings.json | 34 +++++++++++++++++++++++++++++++++- src/core/ga_base.impl.hpp | 2 +- test/misc/rcu.cpp | 4 ++-- test/unit/algorithm.cpp | 4 ++-- test/unit/functional.cpp | 12 +++--------- test/unit/math.cpp | 2 +- test/unit/test_utils.hpp | 2 +- 9 files changed, 50 insertions(+), 20 deletions(-) diff --git a/.clang-tidy b/.clang-tidy index 23a628bd..61f9546c 100644 --- a/.clang-tidy +++ b/.clang-tidy @@ -35,7 +35,9 @@ -cppcoreguidelines-special-member-functions, -misc-unconventional-assign-operator, -misc-non-private-member-variables-in-classes, - -misc-no-recursion + -misc-no-recursion, + -misc-const-correctness, + -misc-use-anonymous-namespace # Also check the header files @@ -44,7 +46,6 @@ HeaderFilterRegex: ".*" # List of warnings to treat as errors during the build WarningsAsErrors: > - *, clang-analyzer-*, portability-*, bugprone-assignment-in-if-condition, @@ -76,3 +77,5 @@ CheckOptions: - { key: bugprone-narrowing-conversions.IgnoreConversionFromTypes, value: difference_type;ptrdiff_t;size_type;size_t;bool } - { key: readability-braces-around-statements.ShortStatementLines, value: 2 } - { key: bugprone-easily-swappable-parameters.ModelImplicitConversions, value: false } + - { key: performance-unnecessary-value-param.AllowedTypes, value: iterator* } + - { key: readability-simplify-boolean-expr.SimplifyDeMorgan, value: false } diff --git a/CMakeLists.txt b/CMakeLists.txt index 46e625c8..758f993e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,7 +40,7 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # Compiler / linker setup if(MSVC) # MSVC style compiler interface - set(GAPP_WARN_FLAGS "-permissive- -W4 -w44263 -w44265 -w44388 -w44471 -w44820 -w44986 -w45024 -w45025 -w45038 -w45204 -w45233 -w45258 -w45262 -w45263 -w45264 -external:W0") + set(GAPP_WARN_FLAGS "-permissive- -W4 -w44263 -w44265 -w44388 -w44471 -w44986 -w45024 -w45025 -w45038 -w45204 -w45233 -w45258 -w45262 -w45263 -w45264 -external:W0") if(GAPP_USE_WERROR) set(GAPP_WARN_FLAGS "${GAPP_WARN_FLAGS} -WX") endif() @@ -184,6 +184,7 @@ if(GAPP_BUILD_TESTS AND BUILD_TESTING AND PROJECT_IS_TOP_LEVEL) enable_testing() add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/test/unit") add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/test/integration") + add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/test/misc") endif() if(GAPP_BUILD_BENCHMARKS AND BUILD_TESTING AND PROJECT_IS_TOP_LEVEL) diff --git a/CMakeSettings.json b/CMakeSettings.json index 96283bc3..d018972e 100644 --- a/CMakeSettings.json +++ b/CMakeSettings.json @@ -9,7 +9,7 @@ "cmakeCommandArgs": "-DGAPP_CXX_FLAGS=/analyze:WX- -DGAPP_BUILD_TESTS=ON -DGAPP_BUILD_BENCHMARKS=ON -DGAPP_BUILD_EXAMPLES=ON", "ctestCommandArgs": "--output-on-failure --schedule-random", "codeAnalysisRuleset": "${projectDir}\\core-guidelines.ruleset", - "enableMicrosoftCodeAnalysis": true, + "enableMicrosoftCodeAnalysis": false, "inheritEnvironments": [ "msvc_x64_x64" ] }, { @@ -44,6 +44,7 @@ "installRoot": "${projectDir}\\out\\install\\${name}", "cmakeCommandArgs": "-DGAPP_BUILD_TESTS=ON -DGAPP_BUILD_BENCHMARKS=ON -DGAPP_BUILD_EXAMPLES=ON", "ctestCommandArgs": "--output-on-failure --schedule-random", + "enableClangTidyCodeAnalysis": false, "inheritEnvironments": [ "clang_cl_x64_x64" ] }, { @@ -77,6 +78,37 @@ "codeAnalysisRuleset": "${projectDir}\\core-guidelines.ruleset", "enableMicrosoftCodeAnalysis": false, "inheritEnvironments": [ "msvc_x64_x64" ] + }, + { + "name": "clang-tidy", + "generator": "Ninja", + "configurationType": "Debug", + "buildRoot": "${projectDir}\\out\\build\\${name}", + "installRoot": "${projectDir}\\out\\install\\${name}", + "cmakeCommandArgs": "-DGAPP_BUILD_TESTS=ON", + "ctestCommandArgs": "--output-on-failure --schedule-random", + "disableExternalAnalysis": true, + "inheritEnvironments": [ "clang_cl_x64_x64" ], + "variables": [ + { + "name": "CMAKE_CXX_CLANG_TIDY", + "value": "clang-tidy" + } + ] + }, + { + "name": "core-guidelines-check", + "generator": "Ninja", + "configurationType": "Debug", + "buildRoot": "${projectDir}\\out\\build\\${name}", + "installRoot": "${projectDir}\\out\\install\\${name}", + "cmakeCommandArgs": "-DGAPP_CXX_FLAGS=/analyze:WX- -DGAPP_BUILD_TESTS=ON", + "ctestCommandArgs": "--output-on-failure --schedule-random", + "codeAnalysisRuleset": "${projectDir}\\core-guidelines.ruleset", + "enableMicrosoftCodeAnalysis": true, + "disableExternalAnalysis": true, + "inheritEnvironments": [ "msvc_x64_x64" ], + "environments": [ { "CAExcludePath": "${projectDir}\\src\\algorithm\\reference_lines.cpp" } ] } ] } \ No newline at end of file diff --git a/src/core/ga_base.impl.hpp b/src/core/ga_base.impl.hpp index 325c9b50..2ea0dc53 100644 --- a/src/core/ga_base.impl.hpp +++ b/src/core/ga_base.impl.hpp @@ -311,7 +311,7 @@ namespace gapp inline bool GA::fitnessMatrixIsSynced() const { return std::equal(fitness_matrix_.begin(), fitness_matrix_.end(), population_.begin(), population_.end(), - [this](const auto& fvec, const auto& sol) + [](const auto& fvec, const auto& sol) { return fvec == sol.fitness; }); diff --git a/test/misc/rcu.cpp b/test/misc/rcu.cpp index 3f98440f..13c3d6ef 100644 --- a/test/misc/rcu.cpp +++ b/test/misc/rcu.cpp @@ -10,14 +10,14 @@ using namespace gapp::detail; using namespace std::chrono_literals; -rcu_obj number = 0; +rcu_obj number = 0; static const auto reader_func = [] { while (true) { std::scoped_lock _{ number }; - const size_t& n = number.get(); + [[maybe_unused]] const int& n = number.get(); std::this_thread::sleep_for(2ms); assert(0 <= n && n <= 100); } diff --git a/test/unit/algorithm.cpp b/test/unit/algorithm.cpp index 8648ed54..5d4ca93e 100644 --- a/test/unit/algorithm.cpp +++ b/test/unit/algorithm.cpp @@ -195,8 +195,8 @@ TEST_CASE("find_all", "[algorithm]") const auto big_nums = detail::find_all(nums.begin(), nums.end(), is_big); REQUIRE(big_nums.empty()); - REQUIRE(detail::find_all(nums.begin(), nums.end(), always_true).size() == nums.size()); - REQUIRE(detail::find_all(nums.begin(), nums.end(), always_false).size() == 0); + REQUIRE(detail::find_all(nums.begin(), nums.end(), always_true).size() == nums.size()); + REQUIRE(detail::find_all(nums.begin(), nums.end(), always_false).empty()); } TEST_CASE("find_indices", "[algorithm]") diff --git a/test/unit/functional.cpp b/test/unit/functional.cpp index 9197a646..91eedc71 100644 --- a/test/unit/functional.cpp +++ b/test/unit/functional.cpp @@ -23,21 +23,15 @@ TEST_CASE("map", "[functional]") const auto res = map(nums, [](double n) { return n + 1.5; }); REQUIRE(res == std::vector{ 1.5, 2.7, 6.5, 4.0 }); - REQUIRE(map(std::vector{}, std::identity{}) == std::vector{}); + REQUIRE(map(std::vector{}, std::identity{}).empty()); } TEST_CASE("flatten", "[functional]") { std::vector> num_pairs = { { 0, 1 }, { 1, 3 }, { 5, 2 }}; - SECTION("lvalue") - { - REQUIRE(flatten(num_pairs) == std::vector{ 0, 1, 1, 3, 5, 2 }); - } - SECTION("rvalue") - { - REQUIRE(flatten(std::move(num_pairs)) == std::vector{ 0, 1, 1, 3, 5, 2 }); - } + REQUIRE(flatten(num_pairs) == std::vector{ 0, 1, 1, 3, 5, 2 }); + REQUIRE(flatten(std::move(num_pairs)) == std::vector{ 0, 1, 1, 3, 5, 2 }); } TEST_CASE("arithmetic_funcs", "[functional]") diff --git a/test/unit/math.cpp b/test/unit/math.cpp index e02189de..568caf0f 100644 --- a/test/unit/math.cpp +++ b/test/unit/math.cpp @@ -330,7 +330,7 @@ TEST_CASE("normalize", "[math]") REQUIRE_THAT(normalizeVector(vec), Matchers::Approx(normalized_vec).margin(0.01)); REQUIRE_THAT(normalizeVector(normalized_vec), Matchers::Approx(normalized_vec).margin(0.01)); - REQUIRE(normalizeVector({}) == std::vector{}); + REQUIRE(normalizeVector({}).empty()); } TEST_CASE("euclidean_distance", "[math]") diff --git a/test/unit/test_utils.hpp b/test/unit/test_utils.hpp index b04fe456..83673496 100644 --- a/test/unit/test_utils.hpp +++ b/test/unit/test_utils.hpp @@ -17,7 +17,7 @@ class DummyFitnessFunction final : public FitnessFunctionBase explicit DummyFitnessFunction(size_t chrom_len, size_t nobj = 1, bool var_len = false, bool dynamic = false) : FitnessFunctionBase(chrom_len, var_len, dynamic), nobj_(nobj) {} private: - FitnessVector invoke(const Chromosome&) const override { return std::vector(nobj_, 0.0); } + FitnessVector invoke(const Chromosome&) const override { return FitnessVector(nobj_, 0.0); } size_t nobj_; };