[clang-tools-extra] 29e4606 - [clang-tidy] Skip template ctors in modernize-use-equals-default

Alexander Shaposhnikov via cfe-commits cfe-commits at lists.llvm.org
Thu Oct 27 22:36:53 PDT 2022


Author: Alexander Shaposhnikov
Date: 2022-10-28T05:30:19Z
New Revision: 29e4606ced7284c87a88fdcd34a6d179a1350fe9

URL: https://github.com/llvm/llvm-project/commit/29e4606ced7284c87a88fdcd34a6d179a1350fe9
DIFF: https://github.com/llvm/llvm-project/commit/29e4606ced7284c87a88fdcd34a6d179a1350fe9.diff

LOG: [clang-tidy] Skip template ctors in modernize-use-equals-default

Skip template ctors in modernize-use-equals-default,
such constructors may be enabled/disabled via SFINAE,
it is not safe to make them "= default".

Test plan: ninja check-all

Differential revision: https://reviews.llvm.org/D136797

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/modernize/UseEqualsDefaultCheck.cpp
    clang-tools-extra/docs/ReleaseNotes.rst
    clang-tools-extra/test/clang-tidy/checkers/modernize/use-equals-default.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/modernize/UseEqualsDefaultCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseEqualsDefaultCheck.cpp
index 60e0f80da2878..a545fae26a3bb 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseEqualsDefaultCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseEqualsDefaultCheck.cpp
@@ -241,7 +241,9 @@ void UseEqualsDefaultCheck::registerMatchers(MatchFinder *Finder) {
       this);
   Finder->addMatcher(
       cxxConstructorDecl(
-          unless(hasParent(IsUnionLikeClass)), isDefinition(),
+          unless(
+              hasParent(decl(anyOf(IsUnionLikeClass, functionTemplateDecl())))),
+          isDefinition(),
           anyOf(
               // Default constructor.
               allOf(unless(hasAnyConstructorInitializer(isWritten())),
@@ -257,8 +259,9 @@ void UseEqualsDefaultCheck::registerMatchers(MatchFinder *Finder) {
       this);
   // Copy-assignment operator.
   Finder->addMatcher(
-      cxxMethodDecl(unless(hasParent(IsUnionLikeClass)), isDefinition(),
-                    isCopyAssignmentOperator(),
+      cxxMethodDecl(unless(hasParent(
+                        decl(anyOf(IsUnionLikeClass, functionTemplateDecl())))),
+                    isDefinition(), isCopyAssignmentOperator(),
                     // isCopyAssignmentOperator() allows the parameter to be
                     // passed by value, and in this case it cannot be
                     // defaulted.

diff  --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index d7ae8dfdcf364..c5cc967259afe 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -158,9 +158,9 @@ Changes in existing checks
   The check now skips unions/union-like classes since in this case a default constructor
   with empty body is not equivalent to the explicitly defaulted one, variadic constructors
   since they cannot be explicitly defaulted. The check also skips copy assignment operators
-  with nonstandard return types, private/protected default constructors for C++17 or earlier.
-  The automatic fixit has been adjusted to avoid adding superfluous semicolon.
-  The check is restricted to C++11 or later.
+  with nonstandard return types, template constructors, private/protected default constructors
+  for C++17 or earlier. The automatic fixit has been adjusted to avoid adding superfluous
+  semicolon. The check is restricted to C++11 or later.
 
 - Change the default behavior of :doc:`readability-avoid-const-params-in-decls
   <clang-tidy/checks/readability/avoid-const-params-in-decls>` to not

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-equals-default.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-equals-default.cpp
index 7a21ebe439cc1..cc5d379b3c3de 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-equals-default.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-equals-default.cpp
@@ -58,6 +58,18 @@ struct VA {
   VA(...) {}
 };
 
+// Skip template constructors.
+struct TC {
+  template <unsigned U>
+  TC() {}
+
+  template <unsigned U>
+  TC(const TC &) {}
+
+  template <unsigned U>
+  TC& operator = (const TC &) { return *this; }
+};
+
 // Initializer or arguments.
 class IA {
 public:


        


More information about the cfe-commits mailing list