namedDecl(
varDecl(
hasType(hasUnqualifiedDesugaredType(recordType(
- hasDeclaration(cxxRecordDecl(hasStringTypeName))))),
+ hasDeclaration(cxxRecordDecl(hasName("basic_string")))))),
hasInitializer(expr(ignoringImplicit(anyOf(
EmptyStringCtorExpr, EmptyStringCtorExprWithTemporaries)))))
.bind("vardecl"),
}
void RedundantStringInitCheck::check(const MatchFinder::MatchResult &Result) {
- const auto *VDecl = Result.Nodes.getNodeAs<VarDecl>("vardecl");
- // VarDecl's getSourceRange() spans 'string foo = ""' or 'string bar("")'.
- // So start at getLocation() to span just 'foo = ""' or 'bar("")'.
- SourceRange ReplaceRange(VDecl->getLocation(), VDecl->getEndLoc());
- diag(VDecl->getLocation(), "redundant string initialization")
- << FixItHint::CreateReplacement(ReplaceRange, VDecl->getName());
+ const auto *CtorExpr = Result.Nodes.getNodeAs<Expr>("expr");
+ const auto *Decl = Result.Nodes.getNodeAs<NamedDecl>("decl");
+ diag(CtorExpr->getExprLoc(), "redundant string initialization")
+ << FixItHint::CreateReplacement(CtorExpr->getSourceRange(),
+ Decl->getName());
}
} // namespace readability
-// RUN: %check_clang_tidy -std=c++11,c++14 %s readability-redundant-string-init %t \
-// RUN: -config="{CheckOptions: \
-// RUN: [{key: readability-redundant-string-init.StringNames, \
-// RUN: value: '::std::basic_string;our::TestString'}] \
-// RUN: }"
+// RUN: %check_clang_tidy -std=c++11,c++14 %s readability-redundant-string-init %t
// FIXME: Fix the checker to work in C++17 mode.
namespace std {
// CHECK-FIXES: std::string a, b, c;
std::string d = "u", e = "u", f = "u";
-
- std::string g = "u", h = "", i = "uuu", j = "", k;
- // CHECK-MESSAGES: [[@LINE-1]]:24: warning: redundant string initialization
- // CHECK-MESSAGES: [[@LINE-2]]:43: warning: redundant string initialization
- // CHECK-FIXES: std::string g = "u", h, i = "uuu", j, k;
}
// These cases should not generate warnings.