1//===--- ContainerContainsCheck.cpp - clang-tidy --------------------------===//
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
9#include "ContainerContainsCheck.h"
10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
12
13using namespace clang::ast_matchers;
14
15namespace clang::tidy::readability {
16
17void ContainerContainsCheck::registerMatchers(MatchFinder *Finder) {
18 const auto SupportedContainers = hasType(
19 InnerMatcher: hasUnqualifiedDesugaredType(InnerMatcher: recordType(hasDeclaration(InnerMatcher: cxxRecordDecl(
20 hasAnyName("::std::set", "::std::unordered_set", "::std::map",
21 "::std::unordered_map", "::std::multiset",
22 "::std::unordered_multiset", "::std::multimap",
23 "::std::unordered_multimap"))))));
24
25 const auto CountCall =
26 cxxMemberCallExpr(on(InnerMatcher: SupportedContainers),
27 callee(InnerMatcher: cxxMethodDecl(hasName(Name: "count"))),
28 argumentCountIs(N: 1))
29 .bind(ID: "call");
30
31 const auto FindCall =
32 cxxMemberCallExpr(on(InnerMatcher: SupportedContainers),
33 callee(InnerMatcher: cxxMethodDecl(hasName(Name: "find"))),
34 argumentCountIs(N: 1))
35 .bind(ID: "call");
36
37 const auto EndCall = cxxMemberCallExpr(on(InnerMatcher: SupportedContainers),
38 callee(InnerMatcher: cxxMethodDecl(hasName(Name: "end"))),
39 argumentCountIs(N: 0));
40
41 const auto Literal0 = integerLiteral(equals(Value: 0));
42 const auto Literal1 = integerLiteral(equals(Value: 1));
43
44 auto AddSimpleMatcher = [&](auto Matcher) {
45 Finder->addMatcher(
46 traverse(TK_IgnoreUnlessSpelledInSource, std::move(Matcher)), this);
47 };
48
49 // Find membership tests which use `count()`.
50 Finder->addMatcher(NodeMatch: implicitCastExpr(hasImplicitDestinationType(InnerMatcher: booleanType()),
51 hasSourceExpression(InnerMatcher: CountCall))
52 .bind(ID: "positiveComparison"),
53 Action: this);
54 AddSimpleMatcher(
55 binaryOperator(hasLHS(InnerMatcher: CountCall), hasOperatorName(Name: "!="), hasRHS(InnerMatcher: Literal0))
56 .bind(ID: "positiveComparison"));
57 AddSimpleMatcher(
58 binaryOperator(hasLHS(InnerMatcher: Literal0), hasOperatorName(Name: "!="), hasRHS(InnerMatcher: CountCall))
59 .bind(ID: "positiveComparison"));
60 AddSimpleMatcher(
61 binaryOperator(hasLHS(InnerMatcher: CountCall), hasOperatorName(Name: ">"), hasRHS(InnerMatcher: Literal0))
62 .bind(ID: "positiveComparison"));
63 AddSimpleMatcher(
64 binaryOperator(hasLHS(InnerMatcher: Literal0), hasOperatorName(Name: "<"), hasRHS(InnerMatcher: CountCall))
65 .bind(ID: "positiveComparison"));
66 AddSimpleMatcher(
67 binaryOperator(hasLHS(InnerMatcher: CountCall), hasOperatorName(Name: ">="), hasRHS(InnerMatcher: Literal1))
68 .bind(ID: "positiveComparison"));
69 AddSimpleMatcher(
70 binaryOperator(hasLHS(InnerMatcher: Literal1), hasOperatorName(Name: "<="), hasRHS(InnerMatcher: CountCall))
71 .bind(ID: "positiveComparison"));
72
73 // Find inverted membership tests which use `count()`.
74 AddSimpleMatcher(
75 binaryOperator(hasLHS(InnerMatcher: CountCall), hasOperatorName(Name: "=="), hasRHS(InnerMatcher: Literal0))
76 .bind(ID: "negativeComparison"));
77 AddSimpleMatcher(
78 binaryOperator(hasLHS(InnerMatcher: Literal0), hasOperatorName(Name: "=="), hasRHS(InnerMatcher: CountCall))
79 .bind(ID: "negativeComparison"));
80 AddSimpleMatcher(
81 binaryOperator(hasLHS(InnerMatcher: CountCall), hasOperatorName(Name: "<="), hasRHS(InnerMatcher: Literal0))
82 .bind(ID: "negativeComparison"));
83 AddSimpleMatcher(
84 binaryOperator(hasLHS(InnerMatcher: Literal0), hasOperatorName(Name: ">="), hasRHS(InnerMatcher: CountCall))
85 .bind(ID: "negativeComparison"));
86 AddSimpleMatcher(
87 binaryOperator(hasLHS(InnerMatcher: CountCall), hasOperatorName(Name: "<"), hasRHS(InnerMatcher: Literal1))
88 .bind(ID: "negativeComparison"));
89 AddSimpleMatcher(
90 binaryOperator(hasLHS(InnerMatcher: Literal1), hasOperatorName(Name: ">"), hasRHS(InnerMatcher: CountCall))
91 .bind(ID: "negativeComparison"));
92
93 // Find membership tests based on `find() == end()`.
94 AddSimpleMatcher(
95 binaryOperator(hasLHS(InnerMatcher: FindCall), hasOperatorName(Name: "!="), hasRHS(InnerMatcher: EndCall))
96 .bind(ID: "positiveComparison"));
97 AddSimpleMatcher(
98 binaryOperator(hasLHS(InnerMatcher: FindCall), hasOperatorName(Name: "=="), hasRHS(InnerMatcher: EndCall))
99 .bind(ID: "negativeComparison"));
100}
101
102void ContainerContainsCheck::check(const MatchFinder::MatchResult &Result) {
103 // Extract the information about the match
104 const auto *Call = Result.Nodes.getNodeAs<CXXMemberCallExpr>(ID: "call");
105 const auto *PositiveComparison =
106 Result.Nodes.getNodeAs<Expr>(ID: "positiveComparison");
107 const auto *NegativeComparison =
108 Result.Nodes.getNodeAs<Expr>(ID: "negativeComparison");
109 assert((!PositiveComparison || !NegativeComparison) &&
110 "only one of PositiveComparison or NegativeComparison should be set");
111 bool Negated = NegativeComparison != nullptr;
112 const auto *Comparison = Negated ? NegativeComparison : PositiveComparison;
113
114 // Diagnose the issue.
115 auto Diag =
116 diag(Loc: Call->getExprLoc(), Description: "use 'contains' to check for membership");
117
118 // Don't fix it if it's in a macro invocation. Leave fixing it to the user.
119 SourceLocation FuncCallLoc = Comparison->getEndLoc();
120 if (!FuncCallLoc.isValid() || FuncCallLoc.isMacroID())
121 return;
122
123 // Create the fix it.
124 const auto *Member = cast<MemberExpr>(Call->getCallee());
125 Diag << FixItHint::CreateReplacement(
126 Member->getMemberNameInfo().getSourceRange(), "contains");
127 SourceLocation ComparisonBegin = Comparison->getSourceRange().getBegin();
128 SourceLocation ComparisonEnd = Comparison->getSourceRange().getEnd();
129 SourceLocation CallBegin = Call->getSourceRange().getBegin();
130 SourceLocation CallEnd = Call->getSourceRange().getEnd();
131 Diag << FixItHint::CreateReplacement(
132 RemoveRange: CharSourceRange::getCharRange(B: ComparisonBegin, E: CallBegin),
133 Code: Negated ? "!" : "");
134 Diag << FixItHint::CreateRemoval(RemoveRange: CharSourceRange::getTokenRange(
135 B: CallEnd.getLocWithOffset(Offset: 1), E: ComparisonEnd));
136}
137
138} // namespace clang::tidy::readability
139

source code of clang-tools-extra/clang-tidy/readability/ContainerContainsCheck.cpp