clang-tools 22.0.0git
InaccurateEraseCheck.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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
10#include "clang/ASTMatchers/ASTMatchFinder.h"
11#include "clang/Lex/Lexer.h"
12
13using namespace clang::ast_matchers;
14
15namespace clang::tidy::bugprone {
16
17void InaccurateEraseCheck::registerMatchers(MatchFinder *Finder) {
18 const auto EndCall =
19 callExpr(
20 callee(functionDecl(hasAnyName("remove", "remove_if", "unique"))),
21 hasArgument(1, optionally(cxxMemberCallExpr(
22 callee(cxxMethodDecl(hasName("end"))))
23 .bind("end"))))
24 .bind("alg");
25
26 const auto DeclInStd = type(hasUnqualifiedDesugaredType(
27 tagType(hasDeclaration(decl(isInStdNamespace())))));
28 Finder->addMatcher(
29 cxxMemberCallExpr(
30 on(anyOf(hasType(DeclInStd), hasType(pointsTo(DeclInStd)))),
31 callee(cxxMethodDecl(hasName("erase"))), argumentCountIs(1),
32 hasArgument(0, EndCall))
33 .bind("erase"),
34 this);
35}
36
37void InaccurateEraseCheck::check(const MatchFinder::MatchResult &Result) {
38 const auto *MemberCall = Result.Nodes.getNodeAs<CXXMemberCallExpr>("erase");
39 const auto *EndExpr = Result.Nodes.getNodeAs<CXXMemberCallExpr>("end");
40 const SourceLocation Loc = MemberCall->getBeginLoc();
41
42 FixItHint Hint;
43
44 if (!Loc.isMacroID() && EndExpr) {
45 const auto *AlgCall = Result.Nodes.getNodeAs<CallExpr>("alg");
46 std::string ReplacementText = std::string(Lexer::getSourceText(
47 CharSourceRange::getTokenRange(EndExpr->getSourceRange()),
48 *Result.SourceManager, getLangOpts()));
49 const SourceLocation EndLoc = Lexer::getLocForEndOfToken(
50 AlgCall->getEndLoc(), 0, *Result.SourceManager, getLangOpts());
51 Hint = FixItHint::CreateInsertion(EndLoc, ", " + ReplacementText);
52 }
53
54 diag(Loc, "this call will remove at most one item even when multiple items "
55 "should be removed")
56 << Hint;
57}
58
59} // namespace clang::tidy::bugprone
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
void registerMatchers(ast_matchers::MatchFinder *Finder) override