Skip to content

Commit 1426567

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

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

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

Lines changed: 18 additions & 8 deletions
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();
@@ -194,15 +200,19 @@ void UseDefaultMemberInitCheck::storeOptions(
194200
}
195201

196202
void UseDefaultMemberInitCheck::registerMatchers(MatchFinder *Finder) {
203+
auto NumericLiteral = anyOf(integerLiteral(), floatLiteral());
204+
auto UnaryNumericLiteral = unaryOperator(hasAnyOperatorName("+", "-"),
205+
hasUnaryOperand(NumericLiteral));
206+
auto EnumRef = declRefExpr(to(enumConstantDecl()));
207+
208+
auto BinaryNumericExpr = binaryOperator(
209+
hasOperands(anyOf(NumericLiteral, EnumRef, binaryOperator()),
210+
anyOf(NumericLiteral, EnumRef, binaryOperator())));
211+
197212
auto InitBase =
198-
anyOf(stringLiteral(), characterLiteral(), integerLiteral(),
199-
unaryOperator(hasAnyOperatorName("+", "-"),
200-
hasUnaryOperand(integerLiteral())),
201-
floatLiteral(),
202-
unaryOperator(hasAnyOperatorName("+", "-"),
203-
hasUnaryOperand(floatLiteral())),
204-
cxxBoolLiteral(), cxxNullPtrLiteralExpr(), implicitValueInitExpr(),
205-
declRefExpr(to(enumConstantDecl())));
213+
anyOf(stringLiteral(), characterLiteral(), NumericLiteral,
214+
UnaryNumericLiteral, cxxBoolLiteral(), cxxNullPtrLiteralExpr(),
215+
implicitValueInitExpr(), EnumRef, BinaryNumericExpr);
206216

207217
auto Init =
208218
anyOf(initListExpr(anyOf(allOf(initCountIs(1), hasInit(0, InitBase)),

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,11 @@ Changes in existing checks
128128
<clang-tidy/checks/misc/redundant-expression>` check by providing additional
129129
examples and fixing some macro related false positives.
130130

131-
- Improved :doc:`performance/unnecessary-value-param
131+
- Improved :doc:`modernize-use-default-member-init
132+
<clang-tidy/checks/modernize/use-default-member-init>` check by matching
133+
arithmetic operations within member list initialization.
134+
135+
- Improved :doc:`performance-unnecessary-value-param
132136
<clang-tidy/checks/performance/unnecessary-value-param>` check performance by
133137
tolerating fix-it breaking compilation when functions is used as pointers
134138
to avoid matching usage of functions within the current compilation unit.

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)