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