From 557c0b3c3e5f96c3d0d0ec035dab83be8712fd9b Mon Sep 17 00:00:00 2001 From: Siarhei Fedartsou Date: Sun, 4 Dec 2022 16:20:04 +0100 Subject: [PATCH] Configure clang-tidy CI check Signed-off-by: Siarhei Fedartsou --- .clang-tidy | 114 ++++++++++++++++++ .github/workflows/linux-check.yaml | 2 +- CMakeLists.txt | 5 + base/buffer_vector.hpp | 2 +- base/deferred_task.cpp | 2 +- cmake/OmimHelpers.cmake | 21 +++- generator/osm_o5m_source.hpp | 2 +- generator/tag_admixer.hpp | 6 +- indexer/validate_and_format_contacts.cpp | 2 +- map/transit/transit_display.cpp | 1 + openlr/openlr_model.cpp | 1 + platform/http_client.cpp | 4 +- .../test_results_matching.cpp | 1 + storage/country_decl.cpp | 2 +- transit/experimental/transit_data.cpp | 1 + transit/transit_graph_data.cpp | 1 + 16 files changed, 153 insertions(+), 14 deletions(-) create mode 100644 .clang-tidy diff --git a/.clang-tidy b/.clang-tidy new file mode 100644 index 0000000000..22f52fe564 --- /dev/null +++ b/.clang-tidy @@ -0,0 +1,114 @@ +--- +Checks: > + bugprone-*, + -bugprone-narrowing-conversions, + -bugprone-easily-swappable-parameters, + -bugprone-branch-clone, + -bugprone-misplaced-widening-cast, + -bugprone-exception-escape, + -bugprone-implicit-widening-of-multiplication-result, + -bugprone-integer-division, + -bugprone-reserved-identifier, + -bugprone-unhandled-self-assignment, + -bugprone-forward-declaration-namespace, + -bugprone-sizeof-expression, + -bugprone-throw-keyword-missing, + -bugprone-macro-parentheses, + -bugprone-forwarding-reference-overload, + -bugprone-move-forwarding-reference, + -bugprone-incorrect-roundings, + -bugprone-unchecked-optional-access, + -bugprone-assignment-in-if-condition, + -bugprone-signed-char-misuse, + -bugprone-suspicious-include, + -bugprone-sizeof-container, + -bugprone-bool-pointer-implicit-conversion, + -bugprone-lambda-function-name, + -bugprone-use-after-move, + -bugprone-suspicious-enum-usage, + -clang-analyzer-*, + -clang-diagnostic-deprecated-declarations, + -clang-diagnostic-constant-conversion, + -clang-diagnostic-unqualified-std-cast-call, + google-*, + -google-build-explicit-make-pair, + -google-build-using-namespace, + -google-explicit-constructor, + -google-default-arguments, + -google-readability-braces-around-statements, + -google-readability-casting, + -google-readability-namespace-comments, + -google-readability-function, + -google-readability-todo, + -google-runtime-int, + -google-build-namespaces, + -google-runtime-references, + -google-readability-function-size, + -google-global-names-in-headers, + llvm-*, + -llvm-namespace-comment, + -llvm-qualified-auto, + -llvm-include-order, + -llvm-else-after-return, + -llvm-header-guard, + -llvm-twine-local, + misc-*, + -misc-argument-comment, + -misc-const-correctness, + -misc-non-private-member-variables-in-classes, + -misc-unconventional-assign-operator, + -misc-no-recursion, + -misc-misplaced-const, + -misc-definitions-in-headers, + -misc-unused-parameters, + -misc-unused-using-decls, + -misc-static-assert, + -misc-confusable-identifiers, + -misc-redundant-expression, + performance-*, + -performance-noexcept-move-constructor, + -performance-no-int-to-ptr, + -performance-unnecessary-value-param, + -performance-unnecessary-copy-initialization, + -performance-move-const-arg, + -performance-inefficient-string-concatenation, + -performance-no-automatic-move, + -performance-type-promotion-in-math-fn, + -performance-for-range-copy, + readability-*, + -readability-avoid-const-params-in-decls, + -readability-braces-around-statements, + -readability-container-size-empty, + -readability-convert-member-functions-to-static, + -readability-container-data-pointer, + -readability-const-return-type, + -readability-function-cognitive-complexity, + -readability-function-size, + -readability-identifier-naming, + -readability-implicit-bool-conversion, + -readability-magic-numbers, + -readability-else-after-return, + -readability-inconsistent-declaration-parameter-name, + -readability-isolate-declaration, + -readability-identifier-length, + -readability-redundant-declaration, + -readability-uppercase-literal-suffix, + -readability-named-parameter, + -readability-qualified-auto, + -readability-suspicious-call-argument, + -readability-redundant-access-specifiers, + -readability-redundant-member-init, + -readability-static-definition-in-anonymous-namespace, + -readability-use-anyofallof, + -readability-simplify-boolean-expr, + -readability-make-member-function-const, + -readability-redundant-string-init, + -readability-non-const-parameter, + -readability-static-accessed-through-instance, + -readability-misleading-indentation, + -readability-redundant-string-cstr, + -readability-redundant-smartptr-get, + -readability-duplicate-include + +WarningsAsErrors: '*' +HeaderFilterRegex: '.*' \ No newline at end of file diff --git a/.github/workflows/linux-check.yaml b/.github/workflows/linux-check.yaml index af796a9ac2..cdf5ea779e 100644 --- a/.github/workflows/linux-check.yaml +++ b/.github/workflows/linux-check.yaml @@ -63,7 +63,7 @@ jobs: CXX: clang++-14 # -g1 should slightly reduce build time. run: | - cmake . -B build -G Ninja -DCMAKE_BUILD_TYPE=Debug -DCMAKE_CXX_FLAGS=-g1 -DUNITY_DISABLE=ON + cmake . -B build -G Ninja -DCMAKE_BUILD_TYPE=Debug -DCMAKE_CXX_FLAGS=-g1 -DUNITY_DISABLE=ON -DUSE_CLANG_TIDY=ON - name: Compile shell: bash diff --git a/CMakeLists.txt b/CMakeLists.txt index 807bf143ec..197442cd9e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -126,6 +126,7 @@ if (BUILD_DESIGNER) add_definitions(-DBUILD_DESIGNER) endif() +option(USE_CLANG_TIDY "Enable Clang Tidy" OFF) option(USE_ASAN "Enable Address Sanitizer" OFF) option(USE_TSAN "Enable Thread Sanitizer" OFF) option(USE_LIBFUZZER "Enable LibFuzzer" OFF) @@ -161,6 +162,10 @@ if (PLATFORM_LINUX) option(USE_PPROF "Enable Google Profiler" OFF) endif() +if (USE_CLANG_TIDY) + message(STATUS "Clang Tidy is enabled") +endif() + if (USE_ASAN) message(STATUS "Address Sanitizer is enabled") endif() diff --git a/base/buffer_vector.hpp b/base/buffer_vector.hpp index 7a9b331934..279ce4ff86 100644 --- a/base/buffer_vector.hpp +++ b/base/buffer_vector.hpp @@ -74,7 +74,7 @@ public: buffer_vector(buffer_vector const &) = default; - buffer_vector(buffer_vector && rhs) : m_size(rhs.m_size), m_dynamic(move(rhs.m_dynamic)) + buffer_vector(buffer_vector && rhs) : m_size(rhs.m_size), m_dynamic(std::move(rhs.m_dynamic)) { if (!IsDynamic()) MoveStatic(rhs); diff --git a/base/deferred_task.cpp b/base/deferred_task.cpp index 42cd788d69..ef5e6295db 100644 --- a/base/deferred_task.cpp +++ b/base/deferred_task.cpp @@ -18,7 +18,7 @@ DeferredTask::DeferredTask(Duration const & duration) : m_duration(duration) if (m_cv.wait_for(l, m_duration) != std::cv_status::timeout || !m_fn) continue; - auto fn = move(m_fn); + auto fn = std::move(m_fn); m_fn = nullptr; l.unlock(); diff --git a/cmake/OmimHelpers.cmake b/cmake/OmimHelpers.cmake index 2e1c7c23e2..86d8b736c9 100644 --- a/cmake/OmimHelpers.cmake +++ b/cmake/OmimHelpers.cmake @@ -33,7 +33,7 @@ function(omim_add_executable executable) # Enable warnings for all our binaries. target_compile_options(${executable} PRIVATE ${OMIM_WARNING_FLAGS}) - target_include_directories(${executable} PRIVATE ${OMIM_INCLUDE_DIRS}) + target_include_directories(${executable} SYSTEM PRIVATE ${OMIM_INCLUDE_DIRS}) if (USE_ASAN) target_link_libraries(${executable} -fsanitize=address @@ -74,12 +74,15 @@ function(omim_add_executable executable) endif() endfunction() + +# SYSTEM is needed to suppress warnings from 3party headers. + function(omim_add_library library) add_library(${library} ${ARGN}) # Enable warnings for all our libraries. target_compile_options(${library} PRIVATE ${OMIM_WARNING_FLAGS}) - target_include_directories(${library} PRIVATE ${OMIM_INCLUDE_DIRS}) + target_include_directories(${library} SYSTEM PRIVATE ${OMIM_INCLUDE_DIRS}) if (USE_PPROF AND PLATFORM_MAC) find_path(PPROF_INCLUDE_DIR NAMES gperftools/profiler.h) target_include_directories(${library} SYSTEM PUBLIC ${PPROF_INCLUDE_DIR}) @@ -87,6 +90,18 @@ function(omim_add_library library) if (USE_PCH) add_precompiled_headers_to_target(${library} ${OMIM_PCH_TARGET_NAME}) endif() + + + if (USE_CLANG_TIDY) + find_program(CLANG_TIDY_COMMAND NAMES clang-tidy) + if(NOT CLANG_TIDY_COMMAND) + message(FATAL_ERROR "USE_CLANG_TIDY is ON but clang-tidy is not found!") + else() + message(STATUS "Found clang-tidy at ${CLANG_TIDY_COMMAND}") + set_target_properties(${library} PROPERTIES CXX_CLANG_TIDY "${CLANG_TIDY_COMMAND}") + endif() + endif() + endfunction() function(omim_add_test_impl disable_platform_init executable) @@ -96,7 +111,7 @@ function(omim_add_test_impl disable_platform_init executable) ${OMIM_ROOT}/testing/testingmain.cpp ) target_compile_options(${executable} PRIVATE ${OMIM_WARNING_FLAGS}) - target_include_directories(${executable} PRIVATE ${OMIM_INCLUDE_DIRS}) + target_include_directories(${executable} SYSTEM PRIVATE ${OMIM_INCLUDE_DIRS}) if(disable_platform_init) target_compile_definitions(${PROJECT_NAME} PRIVATE OMIM_UNIT_TEST_DISABLE_PLATFORM_INIT) else() diff --git a/generator/osm_o5m_source.hpp b/generator/osm_o5m_source.hpp index 57115eb3b7..b37b6ceeba 100644 --- a/generator/osm_o5m_source.hpp +++ b/generator/osm_o5m_source.hpp @@ -227,7 +227,7 @@ public: SubElements(O5MSource * reader, TSubElementGetter const & func) : m_reader(reader), m_func(func) {} - void Skip() { while (m_reader && m_func(nullptr)); } + void Skip() { while (m_reader && m_func(nullptr)) { /* no-op */ } } Iterator const begin() const { return Iterator(m_reader, m_func); } Iterator const end() const { return Iterator(); } diff --git a/generator/tag_admixer.hpp b/generator/tag_admixer.hpp index bf0c8e94b8..5e2dd4e46c 100644 --- a/generator/tag_admixer.hpp +++ b/generator/tag_admixer.hpp @@ -52,16 +52,16 @@ public: { // String format: <>. // First ';'. - auto pos = oneLine.find(";"); + auto pos = oneLine.find(';'); if (pos != std::string::npos) { // Second ';'. - pos = oneLine.find(";", pos + 1); + pos = oneLine.find(';', pos + 1); if (pos != std::string::npos) { uint64_t nodeId; // Third ';'. - auto endPos = oneLine.find(";", pos + 1); + auto endPos = oneLine.find(';', pos + 1); if (endPos != std::string::npos) { if (strings::to_uint64(oneLine.substr(pos + 1, endPos - pos - 1), nodeId)) diff --git a/indexer/validate_and_format_contacts.cpp b/indexer/validate_and_format_contacts.cpp index 5bc74f50ed..fb112a243a 100644 --- a/indexer/validate_and_format_contacts.cpp +++ b/indexer/validate_and_format_contacts.cpp @@ -290,7 +290,7 @@ bool ValidateWebsite(string const & site) if ('.' == site[startPos] || '.' == site.back()) return false; - if (string::npos == site.find(".")) + if (string::npos == site.find('.')) return false; if (string::npos != site.find("..")) diff --git a/map/transit/transit_display.cpp b/map/transit/transit_display.cpp index 77e854cea7..2ec4e7f87c 100644 --- a/map/transit/transit_display.cpp +++ b/map/transit/transit_display.cpp @@ -807,6 +807,7 @@ void TransitRouteDisplay::CreateTransitMarks() GetTransitMarkerSizes(kStopMarkerScale, m_maxSubrouteWidth); vector transferArrowOffsets; + transferArrowOffsets.reserve(transferMarkerSizes.size()); for (auto const & size : transferMarkerSizes) transferArrowOffsets.emplace_back(0.0f, size.y * 0.5f); diff --git a/openlr/openlr_model.cpp b/openlr/openlr_model.cpp index 7aad417158..44b47f350a 100644 --- a/openlr/openlr_model.cpp +++ b/openlr/openlr_model.cpp @@ -12,6 +12,7 @@ namespace openlr vector LinearSegment::GetMercatorPoints() const { vector points; + points.reserve(m_locationReference.m_points.size()); for (auto const & point : m_locationReference.m_points) points.push_back(mercator::FromLatLon(point.m_latLon)); return points; diff --git a/platform/http_client.cpp b/platform/http_client.cpp index 9eb74e9b38..011c830770 100644 --- a/platform/http_client.cpp +++ b/platform/http_client.cpp @@ -175,7 +175,7 @@ string HttpClient::NormalizeServerCookies(string && cookies) // Split by ", ". Can have invalid tokens here, expires= can also contain a comma. while (getline(is, str, ',')) { - size_t const leading = str.find_first_not_of(" "); + size_t const leading = str.find_first_not_of(' '); if (leading != string::npos) str.substr(leading).swap(str); @@ -193,7 +193,7 @@ string HttpClient::NormalizeServerCookies(string && cookies) result.append("; "); // Read cookie itself. - result.append(str, 0, str.find(";")); + result.append(str, 0, str.find(';')); } return result; } diff --git a/search/search_tests_support/test_results_matching.cpp b/search/search_tests_support/test_results_matching.cpp index 541a661103..0f1496619c 100644 --- a/search/search_tests_support/test_results_matching.cpp +++ b/search/search_tests_support/test_results_matching.cpp @@ -67,6 +67,7 @@ bool MatchResults(DataSource const & dataSource, vector vector const & actual) { vector resultIds; + resultIds.reserve(actual.size()); for (auto const & a : actual) resultIds.push_back(a.GetFeatureID()); sort(resultIds.begin(), resultIds.end()); diff --git a/storage/country_decl.cpp b/storage/country_decl.cpp index 72edc18be7..9c23f94ebd 100644 --- a/storage/country_decl.cpp +++ b/storage/country_decl.cpp @@ -21,7 +21,7 @@ void CountryInfo::FileName2FullName(string & fName) // static void CountryInfo::FullName2GroupAndMap(string const & fName, string & group, string & map) { - size_t pos = fName.find(","); + size_t pos = fName.find(','); if (pos == string::npos) { map = fName; diff --git a/transit/experimental/transit_data.cpp b/transit/experimental/transit_data.cpp index a94c1ae53a..364f9c4ae4 100644 --- a/transit/experimental/transit_data.cpp +++ b/transit/experimental/transit_data.cpp @@ -182,6 +182,7 @@ TimeTable GetTimeTableFromJson(json_t * obj) std::vector timeIntervals; auto const & rawValues = GetVectorFromJson(item, "intervals"); + timeIntervals.reserve(rawValues.size()); for (auto const & rawValue : rawValues) timeIntervals.push_back(TimeInterval(rawValue)); diff --git a/transit/transit_graph_data.cpp b/transit/transit_graph_data.cpp index a965bc162a..853b92a16b 100644 --- a/transit/transit_graph_data.cpp +++ b/transit/transit_graph_data.cpp @@ -134,6 +134,7 @@ template void UpdateItems(set const & ids, vector & items) { vector itemsToFill; + itemsToFill.reserve(ids.size()); for (auto const id : ids) itemsToFill.push_back(FindById(items, id)); -- 2.45.3