[clang-tools-extra] 2c7ea1c - [clang-tidy] Address false positive in modernize-use-default-member-init
Nathan James via cfe-commits
cfe-commits at lists.llvm.org
Fri Apr 3 11:43:53 PDT 2020
Author: Nathan James
Date: 2020-04-03T19:43:46+01:00
New Revision: 2c7ea1c4c5f7dde562bac684a59ad67f1f062726
URL: https://github.com/llvm/llvm-project/commit/2c7ea1c4c5f7dde562bac684a59ad67f1f062726
DIFF: https://github.com/llvm/llvm-project/commit/2c7ea1c4c5f7dde562bac684a59ad67f1f062726.diff
LOG: [clang-tidy] Address false positive in modernize-use-default-member-init
Summary: Fixes [[ https://bugs.llvm.org/show_bug.cgi?id=45363 | incorrect warning emitted by "modernize-use-default-member-init" (new to 10.0.0) ]].
Reviewers: aaron.ballman, alexfh, gribozavr2
Reviewed By: aaron.ballman
Subscribers: xazax.hun, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D77199
Added:
Modified:
clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp
clang-tools-extra/test/clang-tidy/checkers/modernize-use-default-member-init.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp
index cb275ab58f1b..04cc7aa9d449 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp
@@ -17,6 +17,12 @@ namespace clang {
namespace tidy {
namespace modernize {
+namespace {
+AST_MATCHER_P(InitListExpr, initCountIs, unsigned, N) {
+ return Node.getNumInits() == N;
+}
+} // namespace
+
static StringRef getValueOfValueInit(const QualType InitType) {
switch (InitType->getScalarTypeKind()) {
case Type::STK_CPointer:
@@ -190,7 +196,7 @@ void UseDefaultMemberInitCheck::storeOptions(
}
void UseDefaultMemberInitCheck::registerMatchers(MatchFinder *Finder) {
- auto Init =
+ auto InitBase =
anyOf(stringLiteral(), characterLiteral(), integerLiteral(),
unaryOperator(hasAnyOperatorName("+", "-"),
hasUnaryOperand(integerLiteral())),
@@ -198,7 +204,13 @@ void UseDefaultMemberInitCheck::registerMatchers(MatchFinder *Finder) {
unaryOperator(hasAnyOperatorName("+", "-"),
hasUnaryOperand(floatLiteral())),
cxxBoolLiteral(), cxxNullPtrLiteralExpr(), implicitValueInitExpr(),
- initListExpr(), declRefExpr(to(enumConstantDecl())));
+ declRefExpr(to(enumConstantDecl())));
+
+ auto Init =
+ anyOf(initListExpr(anyOf(
+ allOf(initCountIs(1), hasInit(0, ignoringImplicit(InitBase))),
+ initCountIs(0))),
+ InitBase);
Finder->addMatcher(
cxxConstructorDecl(
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize-use-default-member-init.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize-use-default-member-init.cpp
index 3570fcff4a5c..60344e06dc31 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize-use-default-member-init.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize-use-default-member-init.cpp
@@ -432,3 +432,17 @@ class FunctionTryBlock {
// CHECK-MESSAGES: :[[@LINE-2]]:10: warning: use default member initializer for 'k' [modernize-use-default-member-init]
// CHECK-FIXES: int i{5}, k{8};
};
+
+struct PR45363 {
+ // Ensure no warning is emitted here
+ PR45363(int i = 0) : m_i{i} {}
+ int m_i;
+};
+
+struct EmptyBracedIntDefault {
+ EmptyBracedIntDefault() : m_i{} {}
+ int m_i;
+ // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: use default member initializer for 'm_i' [modernize-use-default-member-init]
+ // CHECK-FIXES: {{^ }}EmptyBracedIntDefault() {}
+ // CHECK-FIXES-NEXT: {{^ }}int m_i{};
+};
More information about the cfe-commits
mailing list