Author: Piotr Zegar Date: 2023-08-06T09:23:21Z New Revision: 2553e2403a33824bea7ca280c9328b0b0acf2836
URL: https://github.com/llvm/llvm-project/commit/2553e2403a33824bea7ca280c9328b0b0acf2836 DIFF: https://github.com/llvm/llvm-project/commit/2553e2403a33824bea7ca280c9328b0b0acf2836.diff LOG: [clang-tidy] Fix false-positives in performanc-noexcept-swap Enforce a stricter match with the swap function signature, eliminating false-positives. Fixes: #64303 Reviewed By: carlosgalvezp Differential Revision: https://reviews.llvm.org/D157185 Added: Modified: clang-tools-extra/clang-tidy/performance/NoexceptSwapCheck.cpp clang-tools-extra/docs/ReleaseNotes.rst clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-swap.cpp Removed: ################################################################################ diff --git a/clang-tools-extra/clang-tidy/performance/NoexceptSwapCheck.cpp b/clang-tools-extra/clang-tidy/performance/NoexceptSwapCheck.cpp index 65baebd808f6cf..25a58af74f7ee8 100644 --- a/clang-tools-extra/clang-tidy/performance/NoexceptSwapCheck.cpp +++ b/clang-tools-extra/clang-tidy/performance/NoexceptSwapCheck.cpp @@ -17,9 +17,32 @@ using namespace clang::ast_matchers; namespace clang::tidy::performance { void NoexceptSwapCheck::registerMatchers(MatchFinder *Finder) { - Finder->addMatcher( - functionDecl(unless(isDeleted()), hasName("swap")).bind(BindFuncDeclName), - this); + + // Match non-const method with single argument that is non-const reference to + // a class type that owns method and return void. + // Matches: void Class::swap(Class&) + auto MethodMatcher = cxxMethodDecl( + parameterCountIs(1U), unless(isConst()), returns(voidType()), + hasParameter(0, hasType(qualType(hasCanonicalType( + qualType(unless(isConstQualified()), + references(namedDecl().bind("class"))))))), + ofClass(equalsBoundNode("class"))); + + // Match function with 2 arguments, both are non-const references to same type + // and return void. + // Matches: void swap(Type&, Type&) + auto FunctionMatcher = allOf( + unless(cxxMethodDecl()), parameterCountIs(2U), returns(voidType()), + hasParameter( + 0, hasType(qualType(hasCanonicalType( + qualType(unless(isConstQualified()), references(qualType())) + .bind("type"))))), + hasParameter(1, hasType(qualType(hasCanonicalType( + qualType(equalsBoundNode("type"))))))); + Finder->addMatcher(functionDecl(unless(isDeleted()), hasName("swap"), + anyOf(MethodMatcher, FunctionMatcher)) + .bind(BindFuncDeclName), + this); } DiagnosticBuilder diff --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst index 1d006ae3fa16f5..284399afc11352 100644 --- a/clang-tools-extra/docs/ReleaseNotes.rst +++ b/clang-tools-extra/docs/ReleaseNotes.rst @@ -180,6 +180,10 @@ Changes in existing checks <clang-tidy/checks/modernize/loop-convert>` to support for-loops with iterators initialized by free functions like ``begin``, ``end``, or ``size``. +- Improved :doc:`performanc-noexcept-swap + <clang-tidy/checks/performance/noexcept-swap>` check to enforce a stricter + match with the swap function signature, eliminating false-positives. + - Improved the :doc:`readability-identifier-naming <clang-tidy/checks/readability/identifier-naming>` check to emit proper warnings when a type forward declaration precedes its definition. diff --git a/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-swap.cpp b/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-swap.cpp index 11c65906e89682..392b5d17d456e7 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-swap.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-swap.cpp @@ -201,3 +201,20 @@ struct OK21 { template <typename T> void swap(OK21<T> &, OK21<T> &) noexcept(noexcept(TemplateNoexceptWithInt<int>::f())); void swap(OK21<int> &, OK21<int> &) noexcept(noexcept(TemplateNoexceptWithInt<int>::f())); + +namespace PR64303 { + void swap(); + void swap(int&, bool&); + void swap(int&, int&, int&); + void swap(int&); + + struct Test { + void swap(); + void swap(Test&, Test&); + void swap(int&); + static void swap(int&, int&); + + friend void swap(Test&, Test&); + // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: swap functions should be marked noexcept [performance-noexcept-swap] + }; +} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits