24 llvm::raw_string_ostream OS(Result);
25 Literal->outputString(OS);
28 auto OpenPos = Result.find_first_of(
'"');
29 if (OpenPos == std::string::npos)
31 Result[OpenPos] =
'\'';
33 auto ClosePos = Result.find_last_of(
'"');
34 if (ClosePos == std::string::npos)
36 Result[ClosePos] =
'\'';
39 if ((ClosePos - OpenPos) == 2 && Result[OpenPos + 1] ==
'\'')
40 Result.replace(OpenPos + 1, 1,
"\\'");
68 const auto SingleChar =
69 expr(ignoringParenCasts(stringLiteral(hasSize(1)).bind(
"literal")));
70 const auto StringFindFunctions =
71 hasAnyName(
"find",
"rfind",
"find_first_of",
"find_first_not_of",
72 "find_last_of",
"find_last_not_of");
76 callee(functionDecl(StringFindFunctions).bind(
"func")),
77 anyOf(argumentCountIs(1), argumentCountIs(2)),
78 hasArgument(0, SingleChar),
79 on(expr(hasType(hasUnqualifiedDesugaredType(recordType(hasDeclaration(
80 recordDecl(hasAnyName(StringLikeClasses)))))),
81 unless(hasSubstitutedType())))),
86 const auto *Literal = Result.Nodes.getNodeAs<StringLiteral>(
"literal");
87 const auto *FindFunc = Result.Nodes.getNodeAs<FunctionDecl>(
"func");
93 diag(Literal->getBeginLoc(),
"%0 called with a string literal consisting of "
94 "a single character; consider using the more "
95 "effective overload accepting a character")
97 << FixItHint::CreateReplacement(
98 CharSourceRange::getTokenRange(Literal->getBeginLoc(),
99 Literal->getEndLoc()),
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.