[clang-tools-extra] [clang-tidy] Support find for string-like classes in readability-container-contains (PR #157243)
Victor Chernyakin via cfe-commits
cfe-commits at lists.llvm.org
Mon Sep 8 11:16:21 PDT 2025
================
@@ -318,17 +321,15 @@ void testPrivateContains(CustomMapPrivateContains<int, int> &MyMap,
if (MyMap2.count(0)) {};
}
-struct MyString {};
-
struct WeirdNonMatchingContains {
unsigned count(char) const;
- bool contains(const MyString&) const;
+ bool contains(const std::string&) const;
};
-void testWeirdNonMatchingContains(WeirdNonMatchingContains &MyMap) {
- // No warning if there is no `contains` method with the right type.
- if (MyMap.count('a')) {};
-}
+// False positives: when count/find and contains take different types,
+// the check will suggest an invalid code transformation. These cases
+// should not exist in real code or be rare enough.
+// void f(WeirdNonMatchingContains MyMap) { MyMap.count('a'); }
----------------
localspook wrote:
I think (correct me if I'm wrong) we try to avoid commenting out false positives/negatives and prefer writing out a test asserting the incorrect behaviour (in this case, writing out `// CHECK-MESSAGES` and `// CHECK-FIXES`). That way another PR can't accidentally fix the issue and make the comment stale.
https://github.com/llvm/llvm-project/pull/157243
More information about the cfe-commits
mailing list