Skip to content

Commit 8c35d9c

Browse files
committed
[clang-tidy] detect arithmetic operations within member list initialization in modernize-use-default-member-init
1 parent 79210fe commit 8c35d9c

File tree

3 files changed

+45
-10
lines changed

3 files changed

+45
-10
lines changed

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

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,13 @@ 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 BinOp1->getOpcode() == BinOp2->getOpcode() &&
166+
sameValue(BinOp1->getLHS(), BinOp2->getLHS()) &&
167+
sameValue(BinOp1->getRHS(), BinOp2->getRHS());
168+
}
162169
case Stmt::CharacterLiteralClass:
163170
return cast<CharacterLiteral>(E1)->getValue() ==
164171
cast<CharacterLiteral>(E2)->getValue();
@@ -199,17 +206,22 @@ void UseDefaultMemberInitCheck::storeOptions(
199206
}
200207

201208
void UseDefaultMemberInitCheck::registerMatchers(MatchFinder *Finder) {
202-
auto ConstExpRef = varDecl(anyOf(isConstexpr(), isStaticStorageClass()));
209+
auto NumericLiteral = anyOf(integerLiteral(), floatLiteral());
210+
auto UnaryNumericLiteral = unaryOperator(hasAnyOperatorName("+", "-"),
211+
hasUnaryOperand(NumericLiteral));
212+
213+
auto ConstExprRef = varDecl(anyOf(isConstexpr(), isStaticStorageClass()));
214+
auto ImmutableRef =
215+
declRefExpr(to(decl(anyOf(enumConstantDecl(), ConstExprRef))));
216+
217+
auto BinaryNumericExpr = binaryOperator(
218+
hasOperands(anyOf(NumericLiteral, ImmutableRef, binaryOperator()),
219+
anyOf(NumericLiteral, ImmutableRef, binaryOperator())));
203220

204221
auto InitBase =
205-
anyOf(stringLiteral(), characterLiteral(), integerLiteral(),
206-
unaryOperator(hasAnyOperatorName("+", "-"),
207-
hasUnaryOperand(integerLiteral())),
208-
floatLiteral(),
209-
unaryOperator(hasAnyOperatorName("+", "-"),
210-
hasUnaryOperand(floatLiteral())),
211-
cxxBoolLiteral(), cxxNullPtrLiteralExpr(), implicitValueInitExpr(),
212-
declRefExpr(to(anyOf(enumConstantDecl(), ConstExpRef))));
222+
anyOf(stringLiteral(), characterLiteral(), NumericLiteral,
223+
UnaryNumericLiteral, cxxBoolLiteral(), cxxNullPtrLiteralExpr(),
224+
implicitValueInitExpr(), ImmutableRef, BinaryNumericExpr);
213225

214226
auto ExplicitCastExpr = castExpr(hasSourceExpression(InitBase));
215227
auto InitMatcher = anyOf(InitBase, ExplicitCastExpr);

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ Changes in existing checks
190190

191191
- Improved :doc:`modernize-use-default-member-init
192192
<clang-tidy/checks/modernize/use-default-member-init>` check by matching
193-
``constexpr`` and ``static``` values on member initialization and by detecting
193+
arithmetic operations, ``constexpr`` and ``static`` values, and detecting
194194
explicit casting of built-in types within member list initialization.
195195

196196
- Improved :doc:`modernize-use-designated-initializers

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -572,4 +572,27 @@ class FunctionalCastInit {
572572
// CHECK-FIXES: double c{double('C')};
573573
};
574574

575+
#define ARITHMETIC_MACRO (44 - 2)
576+
577+
class DefaultMemberInitWithArithmetic {
578+
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} {}
579+
// CHECK-MESSAGES: :[[@LINE-1]]:39: warning: member initializer for 'a' is redundant [modernize-use-default-member-init]
580+
// CHECK-FIXES: DefaultMemberInitWithArithmetic() {}
581+
582+
int a{1 + 1};
583+
int b;
584+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'b' [modernize-use-default-member-init]
585+
// CHECK-FIXES: int b{1 + 11 + 123 + 1234};
586+
int c;
587+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'c' [modernize-use-default-member-init]
588+
// CHECK-FIXES: int c{2 + (4 / 2) + 3 + (7 / 11)};
589+
int d;
590+
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'd' [modernize-use-default-member-init]
591+
// CHECK-FIXES: int d{ARITHMETIC_MACRO * 2};
592+
double e;
593+
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use default member initializer for 'e' [modernize-use-default-member-init]
594+
// CHECK-FIXES: double e{1.2 + 3.4};
595+
596+
};
597+
575598
} //namespace PR122480

0 commit comments

Comments
 (0)