Skip to content

Commit

Permalink
[clang-tidy] detect arithmetic operations within member list initiali…
Browse files Browse the repository at this point in the history
…zation in modernize-use-default-member-init
  • Loading branch information
RiverDave committed Mar 1, 2025
1 parent 83d2c68 commit 60856b9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ static bool sameValue(const Expr *E1, const Expr *E2) {
case Stmt::UnaryOperatorClass:
return sameValue(cast<UnaryOperator>(E1)->getSubExpr(),
cast<UnaryOperator>(E2)->getSubExpr());
case Stmt::BinaryOperatorClass: {
const auto *BinOp1 = cast<BinaryOperator>(E1);
const auto *BinOp2 = cast<BinaryOperator>(E2);
return sameValue(BinOp1->getLHS(), BinOp2->getLHS()) &&
sameValue(BinOp1->getRHS(), BinOp2->getRHS());
}
case Stmt::CharacterLiteralClass:
return cast<CharacterLiteral>(E1)->getValue() ==
cast<CharacterLiteral>(E2)->getValue();
Expand Down Expand Up @@ -202,7 +208,12 @@ void UseDefaultMemberInitCheck::registerMatchers(MatchFinder *Finder) {
unaryOperator(hasAnyOperatorName("+", "-"),
hasUnaryOperand(floatLiteral())),
cxxBoolLiteral(), cxxNullPtrLiteralExpr(), implicitValueInitExpr(),
declRefExpr(to(enumConstantDecl())));
declRefExpr(to(enumConstantDecl())),
binaryOperator(
hasLHS(anyOf(integerLiteral(), floatLiteral(),
declRefExpr(to(enumConstantDecl())), binaryOperator())),
hasRHS(anyOf(integerLiteral(), floatLiteral(),
declRefExpr(to(enumConstantDecl())), binaryOperator()))));

auto Init =
anyOf(initListExpr(anyOf(allOf(initCountIs(1), hasInit(0, InitBase)),
Expand Down
4 changes: 4 additions & 0 deletions clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ Changes in existing checks
<clang-tidy/checks/performance/move-const-arg>` check by fixing false negatives
on ternary operators calling ``std::move``.

- Improved :doc:`modernize-use-default-member-init
<clang-tidy/checks/modernize/use-default-member-init>` check by matching arithmetic
operations within member list initialization.

Removed checks
^^^^^^^^^^^^^^

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -518,3 +518,29 @@ class ArrayBraceInitMultipleValues {
};

} // namespace PR63285

namespace PR122480 {

#define ARITHMETIC_MACRO (44 - 2)

class DefaultMemberInitWithArithmetic {
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} {}
// CHECK-MESSAGES: :[[@LINE-1]]:39: warning: member initializer for 'a' is redundant [modernize-use-default-member-init]
// CHECK-FIXES: DefaultMemberInitWithArithmetic() {}

int a{1 + 1};
int b;
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'b' [modernize-use-default-member-init]
// CHECK-FIXES: int b{1 + 11 + 123 + 1234};
int c;
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'c' [modernize-use-default-member-init]
// CHECK-FIXES: int c{2 + (4 / 2) + 3 + (7 / 11)}
int d;
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'd' [modernize-use-default-member-init]
// CHECK-FIXES: int d{ARITHMETIC_MACRO * 2};
double e;
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use default member initializer for 'e' [modernize-use-default-member-init]
// CHECK-FIXES: double e{1.2 + 3.4};

};
} // namespace PR122480

0 comments on commit 60856b9

Please sign in to comment.