clang-tools 22.0.0git
RedundantStringInitCheck.cpp
Go to the documentation of this file.
1//===- RedundantStringInitCheck.cpp - clang-tidy ----------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
11#include "clang/ASTMatchers/ASTMatchers.h"
12#include <optional>
13
14using namespace clang::ast_matchers;
15
17
18const char DefaultStringNames[] =
19 "::std::basic_string_view;::std::basic_string";
20
21static std::vector<StringRef> removeNamespaces(ArrayRef<StringRef> Names) {
22 std::vector<StringRef> Result;
23 Result.reserve(Names.size());
24 for (const StringRef Name : Names) {
25 const StringRef::size_type ColonPos = Name.rfind(':');
26 Result.push_back(
27 Name.drop_front(ColonPos == StringRef::npos ? 0 : ColonPos + 1));
28 }
29 return Result;
30}
31
32static const CXXConstructExpr *
33getConstructExpr(const CXXCtorInitializer &CtorInit) {
34 const Expr *InitExpr = CtorInit.getInit();
35 if (const auto *CleanUpExpr = dyn_cast<ExprWithCleanups>(InitExpr))
36 InitExpr = CleanUpExpr->getSubExpr();
37 return dyn_cast<CXXConstructExpr>(InitExpr);
38}
39
40static std::optional<SourceRange>
41getConstructExprArgRange(const CXXConstructExpr &Construct) {
42 SourceLocation B, E;
43 for (const Expr *Arg : Construct.arguments()) {
44 if (B.isInvalid())
45 B = Arg->getBeginLoc();
46 if (Arg->getEndLoc().isValid())
47 E = Arg->getEndLoc();
48 }
49 if (B.isInvalid() || E.isInvalid())
50 return std::nullopt;
51 return SourceRange(B, E);
52}
53
55 ClangTidyContext *Context)
56 : ClangTidyCheck(Name, Context),
57 StringNames(utils::options::parseStringList(
58 Options.get("StringNames", DefaultStringNames))) {}
59
61 Options.store(Opts, "StringNames", DefaultStringNames);
62}
63
65 const auto HasStringTypeName = hasAnyName(StringNames);
66 const auto HasStringCtorName = hasAnyName(removeNamespaces(StringNames));
67
68 // Match string constructor.
69 const auto StringConstructorExpr = expr(
70 anyOf(cxxConstructExpr(argumentCountIs(1),
71 hasDeclaration(cxxMethodDecl(HasStringCtorName))),
72 // If present, the second argument is the alloc object which must
73 // not be present explicitly.
74 cxxConstructExpr(argumentCountIs(2),
75 hasDeclaration(cxxMethodDecl(HasStringCtorName)),
76 hasArgument(1, cxxDefaultArgExpr()))));
77
78 // Match a string constructor expression with an empty string literal.
79 const auto EmptyStringCtorExpr = cxxConstructExpr(
80 StringConstructorExpr,
81 hasArgument(0, ignoringParenImpCasts(stringLiteral(hasSize(0)))));
82
83 const auto EmptyStringCtorExprWithTemporaries =
84 cxxConstructExpr(StringConstructorExpr,
85 hasArgument(0, ignoringImplicit(EmptyStringCtorExpr)));
86
87 const auto StringType = hasType(hasUnqualifiedDesugaredType(
88 recordType(hasDeclaration(cxxRecordDecl(HasStringTypeName)))));
89 const auto EmptyStringInit = traverse(
90 TK_AsIs, expr(ignoringImplicit(anyOf(
91 EmptyStringCtorExpr, EmptyStringCtorExprWithTemporaries))));
92
93 // Match a variable declaration with an empty string literal as initializer.
94 // Examples:
95 // string foo = "";
96 // string bar("");
97 Finder->addMatcher(
98 traverse(TK_AsIs,
99 namedDecl(varDecl(StringType, hasInitializer(EmptyStringInit))
100 .bind("vardecl"),
101 unless(parmVarDecl()))),
102 this);
103 // Match a field declaration with an empty string literal as initializer.
104 Finder->addMatcher(
105 namedDecl(fieldDecl(StringType, hasInClassInitializer(EmptyStringInit))
106 .bind("fieldDecl")),
107 this);
108 // Matches Constructor Initializers with an empty string literal as
109 // initializer.
110 // Examples:
111 // Foo() : SomeString("") {}
112 Finder->addMatcher(
113 cxxCtorInitializer(
114 isWritten(),
115 forField(allOf(StringType, optionally(hasInClassInitializer(
116 EmptyStringInit.bind("empty_init"))))),
117 withInitializer(EmptyStringInit))
118 .bind("ctorInit"),
119 this);
120}
121
122void RedundantStringInitCheck::check(const MatchFinder::MatchResult &Result) {
123 if (const auto *VDecl = Result.Nodes.getNodeAs<VarDecl>("vardecl")) {
124 // VarDecl's getSourceRange() spans 'string foo = ""' or 'string bar("")'.
125 // So start at getLocation() to span just 'foo = ""' or 'bar("")'.
126 const SourceRange ReplaceRange(VDecl->getLocation(), VDecl->getEndLoc());
127 diag(VDecl->getLocation(), "redundant string initialization")
128 << FixItHint::CreateReplacement(ReplaceRange, VDecl->getName());
129 }
130 if (const auto *FDecl = Result.Nodes.getNodeAs<FieldDecl>("fieldDecl")) {
131 // FieldDecl's getSourceRange() spans 'string foo = ""'.
132 // So start at getLocation() to span just 'foo = ""'.
133 const SourceRange ReplaceRange(FDecl->getLocation(), FDecl->getEndLoc());
134 diag(FDecl->getLocation(), "redundant string initialization")
135 << FixItHint::CreateReplacement(ReplaceRange, FDecl->getName());
136 }
137 if (const auto *CtorInit =
138 Result.Nodes.getNodeAs<CXXCtorInitializer>("ctorInit")) {
139 if (const FieldDecl *Member = CtorInit->getMember()) {
140 if (!Member->hasInClassInitializer() ||
141 Result.Nodes.getNodeAs<Expr>("empty_init")) {
142 // The String isn't declared in the class with an initializer or its
143 // declared with a redundant initializer, which will be removed. Either
144 // way the string will be default initialized, therefore we can remove
145 // the constructor initializer entirely.
146 diag(CtorInit->getMemberLocation(), "redundant string initialization")
147 << FixItHint::CreateRemoval(CtorInit->getSourceRange());
148 return;
149 }
150 }
151 const CXXConstructExpr *Construct = getConstructExpr(*CtorInit);
152 if (!Construct)
153 return;
154 if (std::optional<SourceRange> RemovalRange =
155 getConstructExprArgRange(*Construct))
156 diag(CtorInit->getMemberLocation(), "redundant string initialization")
157 << FixItHint::CreateRemoval(*RemovalRange);
158 }
159}
160
161} // namespace clang::tidy::readability
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
RedundantStringInitCheck(StringRef Name, ClangTidyContext *Context)
void registerMatchers(ast_matchers::MatchFinder *Finder) override
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
static const CXXConstructExpr * getConstructExpr(const CXXCtorInitializer &CtorInit)
static std::vector< StringRef > removeNamespaces(ArrayRef< StringRef > Names)
static std::optional< SourceRange > getConstructExprArgRange(const CXXConstructExpr &Construct)
llvm::StringMap< ClangTidyValue > OptionMap