Skip to content

Commit 517c677

Browse files
authored
[clang-tidy] Add check on constexpr & static values in modernize-use-default-member-init (#129425)
This aims to fix a portion of #122480. Simply added a check to look for either compile time initialized variables or static stored variables.
1 parent 4f60f45 commit 517c677

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ void UseDefaultMemberInitCheck::storeOptions(
194194
}
195195

196196
void 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)),

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff 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

clang-tools-extra/test/clang-tidy/checkers/modernize/use-default-member-init.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)