[clang-tidy]: Check request - bugprone-unordered-equal-compare
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
std::equal should not be used to compare the ranges formed by the iterators from [std::unordered_set](https://en.cppreference.com/w/cpp/container/unordered_set.html), [std::unordered_multiset](https://en.cppreference.com/w/cpp/container/unordered_multiset.html), [std::unordered_map](https://en.cppreference.com/w/cpp/container/unordered_map.html), or [std::unordered_multimap](https://en.cppreference.com/w/cpp/container/unordered_multimap.html) because the order in which the elements are stored in those containers may be different even if the two containers store the same elements.
When comparing entire containers or string views(since C++17) for equality, `operator==` for the corresponding type are usually preferred.
See https://en.cppreference.com/w/cpp/algorithm/equal.html for more details.
Example:
```
void bad_example() {
std::unordered_set a, b;
// WARNING: bugprone-unordered-equal-compare
bool wrong = std::equal(a.begin(), a.end(), b.begin());
// OK - good comparison
bool correct = (a == b);
}
```
Contributor guide
Research direction
The issue names clang-tidy but no files or tests; start by finding the check-registration entry point and comparable check tests. Use the supplied std::equal example to define the diagnostic, and consider the work done when the listed unordered containers are flagged while operator== remains accepted.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- compilers, tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100