[clang-tools-extra] fa491fe - clang-tidy: Count template constructors in modernize-use-default-member-init

via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 6 02:05:36 PST 2023


Author: MarcoFalke
Date: 2023-03-06T11:04:38+01:00
New Revision: fa491fefb0f862b653b196eb61f0e690b0b71460

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

LOG: clang-tidy: Count template constructors in modernize-use-default-member-init

Differential Revision: https://reviews.llvm.org/D143375

Added: 
    

Modified: 
    clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp
    clang-tools-extra/docs/ReleaseNotes.rst
    clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp
index d3e7bd4b8032c..673c475db630f 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp
@@ -246,8 +246,12 @@ void UseDefaultMemberInitCheck::checkDefaultInit(
   // Check whether we have multiple hand-written constructors and bomb out, as
   // it is hard to reconcile their sets of member initializers.
   const auto *ClassDecl = cast<CXXRecordDecl>(Field->getParent());
-  if (llvm::count_if(ClassDecl->ctors(), [](const CXXConstructorDecl *Ctor) {
-        return !Ctor->isCopyOrMoveConstructor();
+  if (llvm::count_if(ClassDecl->decls(), [](const Decl *D) {
+        if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(D))
+          D = FTD->getTemplatedDecl();
+        if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(D))
+          return !Ctor->isCopyOrMoveConstructor();
+        return false;
       }) > 1)
     return;
 

diff  --git a/clang-tools-extra/docs/ReleaseNotes.rst b/clang-tools-extra/docs/ReleaseNotes.rst
index e5c46de76cfcc..bd38cf9d6a62e 100644
--- a/clang-tools-extra/docs/ReleaseNotes.rst
+++ b/clang-tools-extra/docs/ReleaseNotes.rst
@@ -169,6 +169,11 @@ Changes in existing checks
   <clang-tidy/checks/misc/unused-using-decls>` check.
   Global options of the same name should be used instead.
 
+- In :doc:`modernize-use-default-member-init
+  <clang-tidy/checks/modernize/use-default-member-init>` count template
+  constructors toward hand written constructors so that they are skipped if more
+  than one exists.
+
 - Fixed reading `HungarianNotation.CString.*` options in
   :doc:`readability-identifier-naming
   <clang-tidy/checks/readability/identifier-naming>` check.

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp
index 464cfbdeacb88..3eb6331d0a9f2 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp
@@ -60,6 +60,12 @@ struct TwoConstructors {
     int i;
 };
 
+struct TwoConstructorsTpl {
+  TwoConstructorsTpl() : i{7} {}
+  template <typename T> TwoConstructorsTpl(T, int) : i(8) {}
+  int i;
+};
+
 struct PositiveNotDefaultOOLInt {
   PositiveNotDefaultOOLInt(int);
   int i;


        


More information about the cfe-commits mailing list