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 = cxxConstructExpr(
37 StringConstructorExpr,
38 hasArgument(0, ignoringParenImpCasts(stringLiteral(hasSize(0)))));
40 const auto EmptyStringCtorExprWithTemporaries =
41 cxxConstructExpr(StringConstructorExpr,
42 hasArgument(0, ignoringImplicit(EmptyStringCtorExpr)));
50 varDecl(hasType(cxxRecordDecl(hasName(
"basic_string"))),
51 hasInitializer(expr(ignoringImplicit(anyOf(
53 EmptyStringCtorExprWithTemporaries)))
55 unless(parmVarDecl()))
60 void RedundantStringInitCheck::check(
const MatchFinder::MatchResult &Result) {
61 const auto *CtorExpr = Result.Nodes.getNodeAs<Expr>(
"expr");
62 const auto *Decl = Result.Nodes.getNodeAs<NamedDecl>(
"decl");
63 diag(CtorExpr->getExprLoc(),
"redundant string initialization")
64 << FixItHint::CreateReplacement(CtorExpr->getSourceRange(),
std::unique_ptr< ast_matchers::MatchFinder > Finder