Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 31 additions & 51 deletions clang-tools-extra/clang-tidy/readability/ContainerContainsCheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using namespace clang::ast_matchers;

namespace clang::tidy::readability {

void ContainerContainsCheck::registerMatchers(MatchFinder *Finder) {
const auto Literal0 = integerLiteral(equals(0));
const auto Literal1 = integerLiteral(equals(1));
Expand Down Expand Up @@ -47,57 +48,36 @@ void ContainerContainsCheck::registerMatchers(MatchFinder *Finder) {
const auto StringNpos = anyOf(declRefExpr(to(varDecl(hasName("npos")))),
memberExpr(member(hasName("npos"))));

auto AddSimpleMatcher = [&](const auto &Matcher) {
Finder->addMatcher(traverse(TK_IgnoreUnlessSpelledInSource, Matcher), this);
};

// Find membership tests which use `count()`.
Finder->addMatcher(implicitCastExpr(hasImplicitDestinationType(booleanType()),
hasSourceExpression(CountCall))
.bind("positiveComparison"),
this);
AddSimpleMatcher(
binaryOperation(hasOperatorName("!="), hasOperands(CountCall, Literal0))
.bind("positiveComparison"));
AddSimpleMatcher(
binaryOperation(hasLHS(CountCall), hasOperatorName(">"), hasRHS(Literal0))
.bind("positiveComparison"));
AddSimpleMatcher(
binaryOperation(hasLHS(Literal0), hasOperatorName("<"), hasRHS(CountCall))
.bind("positiveComparison"));
AddSimpleMatcher(binaryOperation(hasLHS(CountCall), hasOperatorName(">="),
hasRHS(Literal1))
.bind("positiveComparison"));
AddSimpleMatcher(binaryOperation(hasLHS(Literal1), hasOperatorName("<="),
hasRHS(CountCall))
.bind("positiveComparison"));

// Find inverted membership tests which use `count()`.
AddSimpleMatcher(
binaryOperation(hasOperatorName("=="), hasOperands(CountCall, Literal0))
.bind("negativeComparison"));
AddSimpleMatcher(binaryOperation(hasLHS(CountCall), hasOperatorName("<="),
hasRHS(Literal0))
.bind("negativeComparison"));
AddSimpleMatcher(binaryOperation(hasLHS(Literal0), hasOperatorName(">="),
hasRHS(CountCall))
.bind("negativeComparison"));
AddSimpleMatcher(
binaryOperation(hasLHS(CountCall), hasOperatorName("<"), hasRHS(Literal1))
.bind("negativeComparison"));
AddSimpleMatcher(
binaryOperation(hasLHS(Literal1), hasOperatorName(">"), hasRHS(CountCall))
.bind("negativeComparison"));

// Find membership tests based on `find() == end()` or `find() == npos`.
AddSimpleMatcher(
binaryOperation(hasOperatorName("!="),
hasOperands(FindCall, anyOf(EndCall, StringNpos)))
.bind("positiveComparison"));
AddSimpleMatcher(
binaryOperation(hasOperatorName("=="),
hasOperands(FindCall, anyOf(EndCall, StringNpos)))
.bind("negativeComparison"));
Finder->addMatcher(
traverse(TK_AsIs,
implicitCastExpr(hasImplicitDestinationType(booleanType()),
hasSourceExpression(CountCall))
.bind("positiveComparison")),
this);

const auto PositiveComparison =
anyOf(allOf(hasOperatorName("!="), hasOperands(CountCall, Literal0)),
allOf(hasLHS(CountCall), hasOperatorName(">"), hasRHS(Literal0)),
allOf(hasLHS(Literal0), hasOperatorName("<"), hasRHS(CountCall)),
allOf(hasLHS(CountCall), hasOperatorName(">="), hasRHS(Literal1)),
allOf(hasLHS(Literal1), hasOperatorName("<="), hasRHS(CountCall)),
allOf(hasOperatorName("!="),
hasOperands(FindCall, anyOf(EndCall, StringNpos))));

const auto NegativeComparison =
anyOf(allOf(hasOperatorName("=="), hasOperands(CountCall, Literal0)),
allOf(hasLHS(CountCall), hasOperatorName("<="), hasRHS(Literal0)),
allOf(hasLHS(Literal0), hasOperatorName(">="), hasRHS(CountCall)),
allOf(hasLHS(CountCall), hasOperatorName("<"), hasRHS(Literal1)),
allOf(hasLHS(Literal1), hasOperatorName(">"), hasRHS(CountCall)),
allOf(hasOperatorName("=="),
hasOperands(FindCall, anyOf(EndCall, StringNpos))));

Finder->addMatcher(
binaryOperation(
anyOf(allOf(PositiveComparison, expr().bind("positiveComparison")),
allOf(NegativeComparison, expr().bind("negativeComparison")))),
this);
}

void ContainerContainsCheck::check(const MatchFinder::MatchResult &Result) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ContainerContainsCheck : public ClangTidyCheck {
return LO.CPlusPlus;
}
std::optional<TraversalKind> getCheckTraversalKind() const override {
return TK_AsIs;
return TK_IgnoreUnlessSpelledInSource;
}
};

Expand Down