10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12#include "clang/Lex/Lexer.h"
21 callee(functionDecl(hasAnyName(
"remove",
"remove_if",
"unique"))),
23 1, optionally(cxxMemberCallExpr(callee(cxxMethodDecl(hasName(
"end"))))
27 const auto DeclInStd = type(hasUnqualifiedDesugaredType(
28 tagType(hasDeclaration(decl(isInStdNamespace())))));
31 on(anyOf(hasType(DeclInStd), hasType(pointsTo(DeclInStd)))),
32 callee(cxxMethodDecl(hasName(
"erase"))), argumentCountIs(1),
33 hasArgument(0, EndCall))
39 const auto *MemberCall =
40 Result.Nodes.getNodeAs<CXXMemberCallExpr>(
"erase");
42 Result.Nodes.getNodeAs<CXXMemberCallExpr>(
"end");
43 const SourceLocation
Loc = MemberCall->getBeginLoc();
47 if (!
Loc.isMacroID() && EndExpr) {
48 const auto *AlgCall = Result.Nodes.getNodeAs<CallExpr>(
"alg");
49 std::string ReplacementText = std::string(Lexer::getSourceText(
50 CharSourceRange::getTokenRange(EndExpr->getSourceRange()),
52 const SourceLocation EndLoc = Lexer::getLocForEndOfToken(
53 AlgCall->getEndLoc(), 0, *Result.SourceManager,
getLangOpts());
54 Hint = FixItHint::CreateInsertion(EndLoc,
", " + ReplacementText);
57 diag(
Loc,
"this call will remove at most one item even when multiple items "
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
const LangOptions & getLangOpts() const
Returns the language options from the context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.