[PATCH] D157185: [clang-tidy] Fix false-positives in performanc-noexcept-swap
Piotr Zegar via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Sun Aug 6 02:26:47 PDT 2023
This revision was automatically updated to reflect the committed changes.
Closed by commit rG2553e2403a33: [clang-tidy] Fix false-positives in performanc-noexcept-swap (authored by PiotrZSL).
Changed prior to commit:
https://reviews.llvm.org/D157185?vs=547528&id=547537#toc
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D157185/new/
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,20 @@
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]
+ };
+}
Index: clang-tools-extra/docs/ReleaseNotes.rst
===================================================================
--- clang-tools-extra/docs/ReleaseNotes.rst
+++ clang-tools-extra/docs/ReleaseNotes.rst
@@ -180,6 +180,10 @@
<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.
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,9 +17,32 @@
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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D157185.547537.patch
Type: text/x-patch
Size: 3570 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230806/21fd0616/attachment.bin>
More information about the cfe-commits
mailing list