[PATCH] D143375: clang-tidy: Count template constructors in modernize-use-default-member-init
Marco Falke via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Mon Feb 6 02:00:35 PST 2023
MarcoFalke created this revision.
MarcoFalke added a project: clang-tools-extra.
Herald added a subscriber: carlosgalvezp.
Herald added a reviewer: njames93.
Herald added a project: All.
MarcoFalke requested review of this revision.
Herald added a subscriber: cfe-commits.
The check should bomb out when there are multiple constructors. However, it didn't count template constructors.
Add a negative test case `TwoConstructorsTpl` and make it pass.
Also, add more test cases with templates that were already passing on `main`.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D143375
Files:
clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp
clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp
Index: clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp
===================================================================
--- clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp
+++ clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp
@@ -60,6 +60,20 @@
int i;
};
+struct TwoConstructorsTpl {
+ TwoConstructorsTpl() : i{7} {}
+ template <typename T> TwoConstructorsTpl(T, int) : i(8) {}
+ int i;
+};
+
+struct PositiveConstructorTpl {
+ template <typename T> PositiveConstructorTpl(T, int) : i(8) {}
+ // CHECK-FIXES: PositiveConstructorTpl(T, int) {}
+ int i;
+ // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'i'
+ // CHECK-FIXES: int i{8};
+};
+
struct PositiveNotDefaultOOLInt {
PositiveNotDefaultOOLInt(int);
int i;
@@ -237,6 +251,14 @@
NegativeTemplate<int> nti;
NegativeTemplate<double> ntd;
+template <typename T> struct PositiveTemplate {
+ PositiveTemplate() : i(9) {}
+ // CHECK-FIXES: PositiveTemplate() {}
+ int i;
+ // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'i'
+ // CHECK-FIXES: int i{9};
+};
+
struct NegativeDefaultMember {
NegativeDefaultMember() {}
int i = 2;
Index: clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp
+++ clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp
@@ -246,8 +246,12 @@
// 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 *Ctor = dyn_cast<CXXConstructorDecl>(D))
+ return !Ctor->isCopyOrMoveConstructor();
+ if (const auto *FTD = dyn_cast<FunctionTemplateDecl>(D))
+ return isa<CXXConstructorDecl>(FTD->getTemplatedDecl());
+ return false;
}) > 1)
return;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D143375.495034.patch
Type: text/x-patch
Size: 2298 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230206/5ede7830/attachment.bin>
More information about the cfe-commits
mailing list