File tree Expand file tree Collapse file tree 3 files changed +26
-1
lines changed Expand file tree Collapse file tree 3 files changed +26
-1
lines changed Original file line number Diff line number Diff line change @@ -194,6 +194,8 @@ void UseDefaultMemberInitCheck::storeOptions(
194194}
195195
196196void UseDefaultMemberInitCheck::registerMatchers (MatchFinder *Finder) {
197+ auto ConstExpRef = varDecl (anyOf (isConstexpr (), isStaticStorageClass ()));
198+
197199 auto InitBase =
198200 anyOf (stringLiteral (), characterLiteral (), integerLiteral (),
199201 unaryOperator (hasAnyOperatorName (" +" , " -" ),
@@ -202,7 +204,7 @@ void UseDefaultMemberInitCheck::registerMatchers(MatchFinder *Finder) {
202204 unaryOperator (hasAnyOperatorName (" +" , " -" ),
203205 hasUnaryOperand (floatLiteral ())),
204206 cxxBoolLiteral (), cxxNullPtrLiteralExpr (), implicitValueInitExpr (),
205- declRefExpr (to (enumConstantDecl ())));
207+ declRefExpr (to (anyOf ( enumConstantDecl (), ConstExpRef ))));
206208
207209 auto Init =
208210 anyOf (initListExpr (anyOf (allOf (initCountIs (1 ), hasInit (0 , InitBase)),
Original file line number Diff line number Diff line change @@ -146,6 +146,10 @@ Changes in existing checks
146146 <clang-tidy/checks/misc/use-internal-linkage>` check by fix false positives
147147 for function or variable in header file which contains macro expansion.
148148
149+ - Improved :doc: `modernize-use-default-member-init
150+ <clang-tidy/checks/modernize/use-default-member-init>` check by matching
151+ ``constexpr `` and ``static `` values on member initialization.
152+
149153- Improved :doc: `performance/unnecessary-value-param
150154 <clang-tidy/checks/performance/unnecessary-value-param>` check performance by
151155 tolerating fix-it breaking compilation when functions is used as pointers
Original file line number Diff line number Diff line change @@ -518,3 +518,22 @@ class ArrayBraceInitMultipleValues {
518518};
519519
520520} // namespace PR63285
521+
522+ namespace PR122480 {
523+
524+ static int STATIC_VAL = 23 ;
525+ constexpr const char * CONSTEXPR_REF = " Const" ;
526+
527+ class StaticConstExprInit {
528+
529+ StaticConstExprInit () : a{CONSTEXPR_REF}, b{STATIC_VAL}{}
530+ // CHECK-FIXES: StaticConstExprInit() {}
531+ const char * a;
532+ // CHECK-MESSAGES: :[[@LINE-1]]:17: warning: use default member initializer for 'a' [modernize-use-default-member-init]
533+ // CHECK-FIXES: const char* a{CONSTEXPR_REF};
534+ int b;
535+ // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use default member initializer for 'b' [modernize-use-default-member-init]
536+ // CHECK-FIXES: int b{STATIC_VAL};
537+ };
538+
539+ } // namespace PR122480
You can’t perform that action at this time.
0 commit comments