|
14 | 14 | using namespace clang::ast_matchers; |
15 | 15 |
|
16 | 16 | namespace clang::tidy::readability { |
| 17 | + |
17 | 18 | void ContainerContainsCheck::registerMatchers(MatchFinder *Finder) { |
18 | 19 | const auto Literal0 = integerLiteral(equals(0)); |
19 | 20 | const auto Literal1 = integerLiteral(equals(1)); |
@@ -47,57 +48,36 @@ void ContainerContainsCheck::registerMatchers(MatchFinder *Finder) { |
47 | 48 | const auto StringNpos = anyOf(declRefExpr(to(varDecl(hasName("npos")))), |
48 | 49 | memberExpr(member(hasName("npos")))); |
49 | 50 |
|
50 | | - auto AddSimpleMatcher = [&](const auto &Matcher) { |
51 | | - Finder->addMatcher(traverse(TK_IgnoreUnlessSpelledInSource, Matcher), this); |
52 | | - }; |
53 | | - |
54 | | - // Find membership tests which use `count()`. |
55 | | - Finder->addMatcher(implicitCastExpr(hasImplicitDestinationType(booleanType()), |
56 | | - hasSourceExpression(CountCall)) |
57 | | - .bind("positiveComparison"), |
58 | | - this); |
59 | | - AddSimpleMatcher( |
60 | | - binaryOperation(hasOperatorName("!="), hasOperands(CountCall, Literal0)) |
61 | | - .bind("positiveComparison")); |
62 | | - AddSimpleMatcher( |
63 | | - binaryOperation(hasLHS(CountCall), hasOperatorName(">"), hasRHS(Literal0)) |
64 | | - .bind("positiveComparison")); |
65 | | - AddSimpleMatcher( |
66 | | - binaryOperation(hasLHS(Literal0), hasOperatorName("<"), hasRHS(CountCall)) |
67 | | - .bind("positiveComparison")); |
68 | | - AddSimpleMatcher(binaryOperation(hasLHS(CountCall), hasOperatorName(">="), |
69 | | - hasRHS(Literal1)) |
70 | | - .bind("positiveComparison")); |
71 | | - AddSimpleMatcher(binaryOperation(hasLHS(Literal1), hasOperatorName("<="), |
72 | | - hasRHS(CountCall)) |
73 | | - .bind("positiveComparison")); |
74 | | - |
75 | | - // Find inverted membership tests which use `count()`. |
76 | | - AddSimpleMatcher( |
77 | | - binaryOperation(hasOperatorName("=="), hasOperands(CountCall, Literal0)) |
78 | | - .bind("negativeComparison")); |
79 | | - AddSimpleMatcher(binaryOperation(hasLHS(CountCall), hasOperatorName("<="), |
80 | | - hasRHS(Literal0)) |
81 | | - .bind("negativeComparison")); |
82 | | - AddSimpleMatcher(binaryOperation(hasLHS(Literal0), hasOperatorName(">="), |
83 | | - hasRHS(CountCall)) |
84 | | - .bind("negativeComparison")); |
85 | | - AddSimpleMatcher( |
86 | | - binaryOperation(hasLHS(CountCall), hasOperatorName("<"), hasRHS(Literal1)) |
87 | | - .bind("negativeComparison")); |
88 | | - AddSimpleMatcher( |
89 | | - binaryOperation(hasLHS(Literal1), hasOperatorName(">"), hasRHS(CountCall)) |
90 | | - .bind("negativeComparison")); |
91 | | - |
92 | | - // Find membership tests based on `find() == end()` or `find() == npos`. |
93 | | - AddSimpleMatcher( |
94 | | - binaryOperation(hasOperatorName("!="), |
95 | | - hasOperands(FindCall, anyOf(EndCall, StringNpos))) |
96 | | - .bind("positiveComparison")); |
97 | | - AddSimpleMatcher( |
98 | | - binaryOperation(hasOperatorName("=="), |
99 | | - hasOperands(FindCall, anyOf(EndCall, StringNpos))) |
100 | | - .bind("negativeComparison")); |
| 51 | + Finder->addMatcher( |
| 52 | + traverse(TK_AsIs, |
| 53 | + implicitCastExpr(hasImplicitDestinationType(booleanType()), |
| 54 | + hasSourceExpression(CountCall)) |
| 55 | + .bind("positiveComparison")), |
| 56 | + this); |
| 57 | + |
| 58 | + const auto PositiveComparison = |
| 59 | + anyOf(allOf(hasOperatorName("!="), hasOperands(CountCall, Literal0)), |
| 60 | + allOf(hasLHS(CountCall), hasOperatorName(">"), hasRHS(Literal0)), |
| 61 | + allOf(hasLHS(Literal0), hasOperatorName("<"), hasRHS(CountCall)), |
| 62 | + allOf(hasLHS(CountCall), hasOperatorName(">="), hasRHS(Literal1)), |
| 63 | + allOf(hasLHS(Literal1), hasOperatorName("<="), hasRHS(CountCall)), |
| 64 | + allOf(hasOperatorName("!="), |
| 65 | + hasOperands(FindCall, anyOf(EndCall, StringNpos)))); |
| 66 | + |
| 67 | + const auto NegativeComparison = |
| 68 | + anyOf(allOf(hasOperatorName("=="), hasOperands(CountCall, Literal0)), |
| 69 | + allOf(hasLHS(CountCall), hasOperatorName("<="), hasRHS(Literal0)), |
| 70 | + allOf(hasLHS(Literal0), hasOperatorName(">="), hasRHS(CountCall)), |
| 71 | + allOf(hasLHS(CountCall), hasOperatorName("<"), hasRHS(Literal1)), |
| 72 | + allOf(hasLHS(Literal1), hasOperatorName(">"), hasRHS(CountCall)), |
| 73 | + allOf(hasOperatorName("=="), |
| 74 | + hasOperands(FindCall, anyOf(EndCall, StringNpos)))); |
| 75 | + |
| 76 | + Finder->addMatcher( |
| 77 | + binaryOperation( |
| 78 | + anyOf(allOf(PositiveComparison, expr().bind("positiveComparison")), |
| 79 | + allOf(NegativeComparison, expr().bind("negativeComparison")))), |
| 80 | + this); |
101 | 81 | } |
102 | 82 |
|
103 | 83 | void ContainerContainsCheck::check(const MatchFinder::MatchResult &Result) { |
|
0 commit comments