Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@ static bool sameValue(const Expr *E1, const Expr *E2) {
cast<StringLiteral>(E2)->getString();
case Stmt::DeclRefExprClass:
return cast<DeclRefExpr>(E1)->getDecl() == cast<DeclRefExpr>(E2)->getDecl();
case Stmt::CStyleCastExprClass:
case Stmt::CXXStaticCastExprClass:
case Stmt::CXXFunctionalCastExprClass:
return sameValue(cast<ExplicitCastExpr>(E1)->getSubExpr(),
cast<ExplicitCastExpr>(E2)->getSubExpr());
default:
return false;
}
Expand Down Expand Up @@ -206,10 +211,13 @@ void UseDefaultMemberInitCheck::registerMatchers(MatchFinder *Finder) {
cxxBoolLiteral(), cxxNullPtrLiteralExpr(), implicitValueInitExpr(),
declRefExpr(to(anyOf(enumConstantDecl(), ConstExpRef))));

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

auto Init =
anyOf(initListExpr(anyOf(allOf(initCountIs(1), hasInit(0, InitBase)),
anyOf(initListExpr(anyOf(allOf(initCountIs(1), hasInit(0, InitMatcher)),
initCountIs(0), hasType(arrayType()))),
InitBase);
InitBase, ExplicitCastExpr);

Finder->addMatcher(
cxxConstructorDecl(forEachConstructorInitializer(
Expand Down
3 changes: 2 additions & 1 deletion clang-tools-extra/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ Changes in existing checks

- Improved :doc:`modernize-use-default-member-init
<clang-tidy/checks/modernize/use-default-member-init>` check by matching
``constexpr`` and ``static`` values on member initialization.
``constexpr`` and ``static``` values on member initialization and by detecting
explicit casting of built-in types within member list initialization.

- Improved :doc:`performance/unnecessary-value-param
<clang-tidy/checks/performance/unnecessary-value-param>` check performance by
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -536,4 +536,40 @@ namespace PR122480 {
// CHECK-FIXES: int b{STATIC_VAL};
};

class CStyleCastInit {
CStyleCastInit() : a{(int)1.23}, b{(float)42}, c{(double)'C'} {}
// CHECK-MESSAGES: :[[@LINE-1]]:50: warning: member initializer for 'c' is redundant [modernize-use-default-member-init]
// CHECK-FIXES: CStyleCastInit() {}

int a;
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'a' [modernize-use-default-member-init]
// CHECK-FIXES: int a{(int)1.23};
float b;
// CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use default member initializer for 'b' [modernize-use-default-member-init]
// CHECK-FIXES: float b{(float)42};
double c{(double)'C'};
};

class StaticCastInit {
StaticCastInit() : m(static_cast<int>(9.99)), n(static_cast<char>(65)) {}
// CHECK-MESSAGES: :[[@LINE-1]]:49: warning: member initializer for 'n' is redundant [modernize-use-default-member-init]
// CHECK-FIXES: StaticCastInit() {}
int m;
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'm' [modernize-use-default-member-init]
// CHECK-FIXES: int m{static_cast<int>(9.99)};
char n{static_cast<char>(65)};
};

class FunctionalCastInit {
FunctionalCastInit() : a(int(5.67)), b(float(2)), c(double('C')) {}
// CHECK-MESSAGES: :[[@LINE-1]]:40: warning: member initializer for 'b' is redundant [modernize-use-default-member-init]
int a;
// CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'a' [modernize-use-default-member-init]
// CHECK-FIXES: int a{int(5.67)};
float b{float(2)};
double c;
// CHECK-MESSAGES: :[[@LINE-1]]:10: warning: use default member initializer for 'c' [modernize-use-default-member-init]
// CHECK-FIXES: double c{double('C')};
};

} //namespace PR122480
Loading