Skip to content

Commit 60856b9

Browse files
committed
[clang-tidy] detect arithmetic operations within member list initialization in modernize-use-default-member-init
1 parent 83d2c68 commit 60856b9

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,12 @@ static bool sameValue(const Expr *E1, const Expr *E2) {
159159
case Stmt::UnaryOperatorClass:
160160
return sameValue(cast<UnaryOperator>(E1)->getSubExpr(),
161161
cast<UnaryOperator>(E2)->getSubExpr());
162+
case Stmt::BinaryOperatorClass: {
163+
const auto *BinOp1 = cast<BinaryOperator>(E1);
164+
const auto *BinOp2 = cast<BinaryOperator>(E2);
165+
return sameValue(BinOp1->getLHS(), BinOp2->getLHS()) &&
166+
sameValue(BinOp1->getRHS(), BinOp2->getRHS());
167+
}
162168
case Stmt::CharacterLiteralClass:
163169
return cast<CharacterLiteral>(E1)->getValue() ==
164170
cast<CharacterLiteral>(E2)->getValue();
@@ -202,7 +208,12 @@ void UseDefaultMemberInitCheck::registerMatchers(MatchFinder *Finder) {
202208
unaryOperator(hasAnyOperatorName("+", "-"),
203209
hasUnaryOperand(floatLiteral())),
204210
cxxBoolLiteral(), cxxNullPtrLiteralExpr(), implicitValueInitExpr(),
205-
declRefExpr(to(enumConstantDecl())));
211+
declRefExpr(to(enumConstantDecl())),
212+
binaryOperator(
213+
hasLHS(anyOf(integerLiteral(), floatLiteral(),
214+
declRefExpr(to(enumConstantDecl())), binaryOperator())),
215+
hasRHS(anyOf(integerLiteral(), floatLiteral(),
216+
declRefExpr(to(enumConstantDecl())), binaryOperator()))));
206217

207218
auto Init =
208219
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
@@ -137,6 +137,10 @@ Changes in existing checks
137137
<clang-tidy/checks/performance/move-const-arg>` check by fixing false negatives
138138
on ternary operators calling ``std::move``.
139139

140+
- Improved :doc:`modernize-use-default-member-init
141+
<clang-tidy/checks/modernize/use-default-member-init>` check by matching arithmetic
142+
operations within member list initialization.
143+
140144
Removed checks
141145
^^^^^^^^^^^^^^
142146

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,3 +518,29 @@ class ArrayBraceInitMultipleValues {
518518
};
519519

520520
} // namespace PR63285
521+
522+
namespace PR122480 {
523+
524+
#define ARITHMETIC_MACRO (44 - 2)
525+
526+
class DefaultMemberInitWithArithmetic {
527+
DefaultMemberInitWithArithmetic() : a{1 + 1}, b{1 + 11 + 123 + 1234}, c{2 + (4 / 2) + 3 + (7 / 11)}, d{ARITHMETIC_MACRO * 2}, e{1.2 + 3.4} {}
528+
// CHECK-MESSAGES: :[[@LINE-1]]:39: warning: member initializer for 'a' is redundant [modernize-use-default-member-init]
529+
// CHECK-FIXES: DefaultMemberInitWithArithmetic() {}
530+
531+
int a{1 + 1};
532+
int b;
533+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'b' [modernize-use-default-member-init]
534+
// CHECK-FIXES: int b{1 + 11 + 123 + 1234};
535+
int c;
536+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'c' [modernize-use-default-member-init]
537+
// CHECK-FIXES: int c{2 + (4 / 2) + 3 + (7 / 11)}
538+
int d;
539+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'd' [modernize-use-default-member-init]
540+
// CHECK-FIXES: int d{ARITHMETIC_MACRO * 2};
541+
double e;
542+
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use default member initializer for 'e' [modernize-use-default-member-init]
543+
// CHECK-FIXES: double e{1.2 + 3.4};
544+
545+
};
546+
} // namespace PR122480

0 commit comments

Comments
 (0)