11 #include "../utils/Matchers.h"
12 #include "clang/ASTMatchers/ASTMatchers.h"
14 using namespace clang::ast_matchers;
15 using namespace clang::tidy::matchers;
19 namespace readability {
21 void RedundantStringInitCheck::registerMatchers(MatchFinder *
Finder) {
22 if (!getLangOpts().CPlusPlus)
26 const auto StringConstructorExpr = expr(anyOf(
27 cxxConstructExpr(argumentCountIs(1),
28 hasDeclaration(cxxMethodDecl(hasName(
"basic_string")))),
31 cxxConstructExpr(argumentCountIs(2),
32 hasDeclaration(cxxMethodDecl(hasName(
"basic_string"))),
33 hasArgument(1, cxxDefaultArgExpr()))));
36 const auto EmptyStringCtorExpr =
37 cxxConstructExpr(StringConstructorExpr,
38 hasArgument(0, ignoringParenImpCasts(
39 stringLiteral(hasSize(0)))));
41 const auto EmptyStringCtorExprWithTemporaries =
42 cxxConstructExpr(StringConstructorExpr,
43 hasArgument(0, ignoringImplicit(EmptyStringCtorExpr)));
51 varDecl(hasType(cxxRecordDecl(hasName(
"basic_string"))),
52 hasInitializer(expr(ignoringImplicit(anyOf(
54 EmptyStringCtorExprWithTemporaries)))
56 unless(parmVarDecl()))
61 void RedundantStringInitCheck::check(
const MatchFinder::MatchResult &
Result) {
62 const auto *CtorExpr = Result.Nodes.getNodeAs<Expr>(
"expr");
63 const auto *Decl = Result.Nodes.getNodeAs<NamedDecl>(
"decl");
64 diag(CtorExpr->getExprLoc(),
"redundant string initialization")
65 << FixItHint::CreateReplacement(CtorExpr->getSourceRange(),
std::unique_ptr< ast_matchers::MatchFinder > Finder