33 llvm::raw_string_ostream OS(Result);
34 Literal->outputString(OS);
37 auto OpenPos = Result.find_first_of(
'"');
38 if (OpenPos == std::string::npos)
40 Result[OpenPos] =
'\'';
42 auto ClosePos = Result.find_last_of(
'"');
43 if (ClosePos == std::string::npos)
45 Result[ClosePos] =
'\'';
48 if ((ClosePos - OpenPos) == 2 && Result[OpenPos + 1] ==
'\'')
49 Result.replace(OpenPos + 1, 1,
"\\'");
72 const auto SingleChar =
73 ignoringParenCasts(stringLiteral(hasSize(1)).bind(
"literal"));
75 const auto StringExpr = expr(hasType(hasUnqualifiedDesugaredType(
76 recordType(hasDeclaration(recordDecl(hasAnyName(StringLikeClasses)))))));
78 const auto InterestingStringFunction = hasAnyName(
79 "find",
"rfind",
"find_first_of",
"find_first_not_of",
"find_last_of",
80 "find_last_not_of",
"starts_with",
"ends_with",
"contains",
"operator+=");
84 callee(functionDecl(InterestingStringFunction).bind(
"func")),
85 anyOf(argumentCountIs(1), argumentCountIs(2)),
86 hasArgument(0, SingleChar), on(StringExpr)),
89 Finder->addMatcher(cxxOperatorCallExpr(hasOperatorName(
"+="),
90 hasLHS(StringExpr), hasRHS(SingleChar),
91 callee(functionDecl().bind(
"func"))),
96 const MatchFinder::MatchResult &Result) {
97 const auto *Literal = Result.Nodes.getNodeAs<StringLiteral>(
"literal");
98 const auto *FindFunc = Result.Nodes.getNodeAs<FunctionDecl>(
"func");
104 diag(Literal->getBeginLoc(),
"%0 called with a string literal consisting of "
105 "a single character; consider using the more "
106 "efficient overload accepting a character")
108 << FixItHint::CreateReplacement(Literal->getSourceRange(), *Replacement);
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void diagDeprecatedCheckAlias(ClangTidyCheck &Check, const ClangTidyContext &Context, StringRef DeprecatedName, StringRef CanonicalName)
Emits a configuration diagnostic when a deprecated check alias is enabled and the canonical check nam...