PiotrZSL created this revision. PiotrZSL added reviewers: njames93, carlosgalvezp, AMS21. Herald added a subscriber: xazax.hun. Herald added a project: All. PiotrZSL requested review of this revision. Herald added a project: clang-tools-extra. Herald added a subscriber: cfe-commits.
Enforce a stricter match with the swap function signature, eliminating false-positives. Fixes: #64303 Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D157185 Files: 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 Index: clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-swap.cpp =================================================================== --- clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-swap.cpp +++ clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-swap.cpp @@ -201,3 +201,17 @@ 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&); + }; +} Index: clang-tools-extra/docs/ReleaseNotes.rst =================================================================== --- clang-tools-extra/docs/ReleaseNotes.rst +++ clang-tools-extra/docs/ReleaseNotes.rst @@ -163,6 +163,10 @@ <clang-tidy/checks/bugprone/reserved-identifier>`, so that it does not warn on macros starting with underscore and lowercase letter. +- 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. + Removed checks ^^^^^^^^^^^^^^ Index: clang-tools-extra/clang-tidy/performance/NoexceptSwapCheck.cpp =================================================================== --- clang-tools-extra/clang-tidy/performance/NoexceptSwapCheck.cpp +++ clang-tools-extra/clang-tidy/performance/NoexceptSwapCheck.cpp @@ -17,8 +17,26 @@ namespace clang::tidy::performance { void NoexceptSwapCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher( - functionDecl(unless(isDeleted()), hasName("swap")).bind(BindFuncDeclName), + functionDecl( + unless(isDeleted()), hasName("swap"), + anyOf( + cxxMethodDecl( + parameterCountIs(1U), + hasParameter(0, + hasType(qualType(hasCanonicalType(qualType( + unless(isConstQualified()), + references(namedDecl().bind("class"))))))), + ofClass(equalsBoundNode("class"))), + allOf(unless(cxxMethodDecl()), parameterCountIs(2U), + hasParameter(0, hasType(qualType(hasCanonicalType( + qualType(unless(isConstQualified()), + references(qualType())) + .bind("type"))))), + hasParameter(1, hasType(qualType(hasCanonicalType( + qualType(equalsBoundNode("type"))))))))) + .bind(BindFuncDeclName), this); }
Index: clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-swap.cpp =================================================================== --- clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-swap.cpp +++ clang-tools-extra/test/clang-tidy/checkers/performance/noexcept-swap.cpp @@ -201,3 +201,17 @@ 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&); + }; +} Index: clang-tools-extra/docs/ReleaseNotes.rst =================================================================== --- clang-tools-extra/docs/ReleaseNotes.rst +++ clang-tools-extra/docs/ReleaseNotes.rst @@ -163,6 +163,10 @@ <clang-tidy/checks/bugprone/reserved-identifier>`, so that it does not warn on macros starting with underscore and lowercase letter. +- 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. + Removed checks ^^^^^^^^^^^^^^ Index: clang-tools-extra/clang-tidy/performance/NoexceptSwapCheck.cpp =================================================================== --- clang-tools-extra/clang-tidy/performance/NoexceptSwapCheck.cpp +++ clang-tools-extra/clang-tidy/performance/NoexceptSwapCheck.cpp @@ -17,8 +17,26 @@ namespace clang::tidy::performance { void NoexceptSwapCheck::registerMatchers(MatchFinder *Finder) { + Finder->addMatcher( - functionDecl(unless(isDeleted()), hasName("swap")).bind(BindFuncDeclName), + functionDecl( + unless(isDeleted()), hasName("swap"), + anyOf( + cxxMethodDecl( + parameterCountIs(1U), + hasParameter(0, + hasType(qualType(hasCanonicalType(qualType( + unless(isConstQualified()), + references(namedDecl().bind("class"))))))), + ofClass(equalsBoundNode("class"))), + allOf(unless(cxxMethodDecl()), parameterCountIs(2U), + hasParameter(0, hasType(qualType(hasCanonicalType( + qualType(unless(isConstQualified()), + references(qualType())) + .bind("type"))))), + hasParameter(1, hasType(qualType(hasCanonicalType( + qualType(equalsBoundNode("type"))))))))) + .bind(BindFuncDeclName), this); }
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits