34 llvm::raw_string_ostream OS(Result);
35 Literal->outputString(OS);
38 const auto OpenPos = Result.find_first_of(
'"');
39 if (OpenPos == std::string::npos)
41 Result[OpenPos] =
'\'';
43 const auto ClosePos = Result.find_last_of(
'"');
44 if (ClosePos == std::string::npos)
46 Result[ClosePos] =
'\'';
49 if ((ClosePos - OpenPos) == 2 && Result[OpenPos + 1] ==
'\'')
50 Result.replace(OpenPos + 1, 1,
"\\'");
73 const auto SingleChar =
74 ignoringParenCasts(stringLiteral(hasSize(1)).bind(
"literal"));
76 const auto StringExpr = expr(hasType(hasUnqualifiedDesugaredType(
77 recordType(hasDeclaration(recordDecl(hasAnyName(StringLikeClasses)))))));
79 const auto InterestingStringFunction = hasAnyName(
80 "find",
"rfind",
"find_first_of",
"find_first_not_of",
"find_last_of",
81 "find_last_not_of",
"starts_with",
"ends_with",
"contains",
"operator+=");
85 callee(functionDecl(InterestingStringFunction).bind(
"func")),
86 anyOf(argumentCountIs(1), argumentCountIs(2)),
87 hasArgument(0, SingleChar), on(StringExpr)),
90 Finder->addMatcher(cxxOperatorCallExpr(hasOperatorName(
"+="),
91 hasLHS(StringExpr), hasRHS(SingleChar),
92 callee(functionDecl().bind(
"func"))),
97 const MatchFinder::MatchResult &Result) {
98 const auto *Literal = Result.Nodes.getNodeAs<StringLiteral>(
"literal");
99 const auto *FindFunc = Result.Nodes.getNodeAs<FunctionDecl>(
"func");
105 const auto Diag = diag(Literal->getBeginLoc(),
106 "%0 called with a string literal consisting of "
107 "a single character; consider using the more "
108 "efficient overload accepting a character")
110 if (std::none_of(Literal->tokloc_begin(), Literal->tokloc_end(),
111 [](SourceLocation Loc) { return Loc.isMacroID(); }))
112 Diag << FixItHint::CreateReplacement(Literal->getSourceRange(),
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void diagDeprecatedCheckAlias(const 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...