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

Alexander Shaposhnikov via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 26 15:01:25 PDT 2022


alexander-shaposhnikov created this revision.
alexander-shaposhnikov added reviewers: gribozavr2, ymandel, LegalizeAdulthood, alexfh.
alexander-shaposhnikov created this object with visibility "All Users".
Herald added subscribers: carlosgalvezp, xazax.hun.
Herald added a project: All.
alexander-shaposhnikov requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D136797

Files:
  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


Index: clang-tools-extra/test/clang-tidy/checkers/modernize/use-equals-default.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/modernize/use-equals-default.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/modernize/use-equals-default.cpp
@@ -58,6 +58,15 @@
   VA(...) {}
 };
 
+// Skip template constructors.
+struct TA {
+  template <unsigned U>
+  TA() {}
+
+  template <unsigned U>
+  TA(const TA &) {}
+};
+
 // Initializer or arguments.
 class IA {
 public:
Index: clang-tools-extra/docs/ReleaseNotes.rst
===================================================================
--- clang-tools-extra/docs/ReleaseNotes.rst
+++ clang-tools-extra/docs/ReleaseNotes.rst
@@ -158,9 +158,9 @@
   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
Index: clang-tools-extra/clang-tidy/modernize/UseEqualsDefaultCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/modernize/UseEqualsDefaultCheck.cpp
+++ clang-tools-extra/clang-tidy/modernize/UseEqualsDefaultCheck.cpp
@@ -246,9 +246,11 @@
               // Default constructor.
               allOf(unless(hasAnyConstructorInitializer(isWritten())),
                     unless(isVariadic()), parameterCountIs(0),
+                    unless(hasParent(functionTemplateDecl())),
                     IsPublicOrOutOfLineUntilCPP20),
               // Copy constructor.
               allOf(isCopyConstructor(),
+                    unless(hasParent(functionTemplateDecl())),
                     // Discard constructors that can be used as a copy
                     // constructor because all the other arguments have
                     // default values.
@@ -257,7 +259,8 @@
       this);
   // Copy-assignment operator.
   Finder->addMatcher(
-      cxxMethodDecl(unless(hasParent(IsUnionLikeClass)), isDefinition(),
+      cxxMethodDecl(unless(hasParent(IsUnionLikeClass)),
+                    unless(hasParent(functionTemplateDecl())), isDefinition(),
                     isCopyAssignmentOperator(),
                     // isCopyAssignmentOperator() allows the parameter to be
                     // passed by value, and in this case it cannot be


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D136797.470945.patch
Type: text/x-patch
Size: 3069 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221026/d11d78b0/attachment.bin>


More information about the cfe-commits mailing list