Skip to content

Commit

Permalink
[clang-tidy][readability-container-contains] Use hasOperands when pos…
Browse files Browse the repository at this point in the history
…sible (llvm#109178)

Simplifies two cases and matches two new cases previously missing,
`container.end() [!=]= container.find(...)`.
  • Loading branch information
nicovank authored Sep 19, 2024
1 parent 0fa258c commit 14c7632
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,7 @@ void ContainerContainsCheck::registerMatchers(MatchFinder *Finder) {
.bind("positiveComparison"),
this);
AddSimpleMatcher(
binaryOperator(hasLHS(CountCall), hasOperatorName("!="), hasRHS(Literal0))
.bind("positiveComparison"));
AddSimpleMatcher(
binaryOperator(hasLHS(Literal0), hasOperatorName("!="), hasRHS(CountCall))
binaryOperator(hasOperatorName("!="), hasOperands(CountCall, Literal0))
.bind("positiveComparison"));
AddSimpleMatcher(
binaryOperator(hasLHS(CountCall), hasOperatorName(">"), hasRHS(Literal0))
Expand All @@ -82,10 +79,7 @@ void ContainerContainsCheck::registerMatchers(MatchFinder *Finder) {

// Find inverted membership tests which use `count()`.
AddSimpleMatcher(
binaryOperator(hasLHS(CountCall), hasOperatorName("=="), hasRHS(Literal0))
.bind("negativeComparison"));
AddSimpleMatcher(
binaryOperator(hasLHS(Literal0), hasOperatorName("=="), hasRHS(CountCall))
binaryOperator(hasOperatorName("=="), hasOperands(CountCall, Literal0))
.bind("negativeComparison"));
AddSimpleMatcher(
binaryOperator(hasLHS(CountCall), hasOperatorName("<="), hasRHS(Literal0))
Expand All @@ -102,10 +96,10 @@ void ContainerContainsCheck::registerMatchers(MatchFinder *Finder) {

// Find membership tests based on `find() == end()`.
AddSimpleMatcher(
binaryOperator(hasLHS(FindCall), hasOperatorName("!="), hasRHS(EndCall))
binaryOperator(hasOperatorName("!="), hasOperands(FindCall, EndCall))
.bind("positiveComparison"));
AddSimpleMatcher(
binaryOperator(hasLHS(FindCall), hasOperatorName("=="), hasRHS(EndCall))
binaryOperator(hasOperatorName("=="), hasOperands(FindCall, EndCall))
.bind("negativeComparison"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -426,3 +426,30 @@ void testBox() {
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use 'contains' to check for membership [readability-container-contains]
// CHECK-FIXES: if (Set.contains(0)) {};
}

void testOperandPermutations(std::map<int, int>& Map) {
if (Map.count(0) != 0) {};
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use 'contains' to check for membership [readability-container-contains]
// CHECK-FIXES: if (Map.contains(0)) {};
if (0 != Map.count(0)) {};
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use 'contains' to check for membership [readability-container-contains]
// CHECK-FIXES: if (Map.contains(0)) {};
if (Map.count(0) == 0) {};
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use 'contains' to check for membership [readability-container-contains]
// CHECK-FIXES: if (!Map.contains(0)) {};
if (0 == Map.count(0)) {};
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use 'contains' to check for membership [readability-container-contains]
// CHECK-FIXES: if (!Map.contains(0)) {};
if (Map.find(0) != Map.end()) {};
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use 'contains' to check for membership [readability-container-contains]
// CHECK-FIXES: if (Map.contains(0)) {};
if (Map.end() != Map.find(0)) {};
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use 'contains' to check for membership [readability-container-contains]
// CHECK-FIXES: if (Map.contains(0)) {};
if (Map.find(0) == Map.end()) {};
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use 'contains' to check for membership [readability-container-contains]
// CHECK-FIXES: if (!Map.contains(0)) {};
if (Map.end() == Map.find(0)) {};
// CHECK-MESSAGES: :[[@LINE-1]]:{{[0-9]+}}: warning: use 'contains' to check for membership [readability-container-contains]
// CHECK-FIXES: if (!Map.contains(0)) {};
}

0 comments on commit 14c7632

Please sign in to comment.