Summary
Instead of counting occurrences in sets and maps, the C++20 ::contains
method expresses the intent unambiguously and can return early on first encounter.
Context
Applied clang‑tidy’s readability‑container‑contains check, though many cases required manual changes since tidy couldn’t fix them automatically.
Changes
The changes made here were:
From | To |
---|---|
m.find(k) == m.end() |
!m.contains(k) |
m.find(k) != m.end() |
m.contains(k) |
m.count(k) |
m.contains(k) |
!m.count(k) |
!m.contains(k) |
m.count(k) == 0 |
!m.contains(k) |
m.count(k) != 1 |
!m.contains(k) |
m.count(k) == 1 |
m.contains(k) |
m.count(k) < 1 |
!m.contains(k) |
m.count(k) > 0 |
m.contains(k) |
m.count(k) != 0 |
m.contains(k) |
Note that
== 1
/!= 1
/< 1
only apply to simple maps/sets and had to be changed manually.
There are many other cases that could have been changed, but we’ve reverted most of those to reduce conflict with other open PRs.
0rm -rfd build && \
1cmake -B build \
2 -DCMAKE_C_COMPILER="$(brew --prefix llvm)/bin/clang" \
3 -DCMAKE_CXX_COMPILER="$(brew --prefix llvm)/bin/clang++" \
4 -DCMAKE_OSX_SYSROOT="$(xcrun --show-sdk-path)" \
5 -DCMAKE_C_FLAGS="-target arm64-apple-macos11" \
6 -DCMAKE_CXX_FLAGS="-target arm64-apple-macos11" \
7 -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DBUILD_BENCH=ON -DBUILD_FUZZ_BINARY=ON -DBUILD_FOR_FUZZING=ON
8
9 "$(brew --prefix llvm)/bin/run-clang-tidy" -quiet -p build -j$(nproc) -checks='-*,readability-container-contains' | grep -v 'clang-tidy'
Note: this is a take 2 of #33094 with fewer contentious changes.