-
Notifications
You must be signed in to change notification settings - Fork 15.4k
Closed
Closed
Copy link
Labels
Description
I encountered this in a large codebase. Running run-clang-tidy-20 -checks='-*,readability-container-size-empty' -p . -fix resulted in code that no longer compiled.
The cases I noticed involved a subclass of a std container where size() was being used in a member function.
Unexpected behavior: !size() replaced by size->empty() and size() >= 1 was replaced by !size->empty()
Expected behavior: !size() should be replaced by empty() and size() >= 1 should be replaced by !empty(). (Alternatively, this->empty() and !this->empty() would also be acceptable.)
I have attached a minimal repro where the incorrect replacement can be seen with
$ clang-tidy -checks='-*,readability-container-size-empty' break-readability-container-size-empty.c++
Error while trying to load a compilation database:
Could not auto-detect compilation database for file "break-readability-container-size-empty.c++"
No compilation database found in /home/ivan/git/bug-readability-container-size-empty or any parent directory
fixed-compilation-database: Error while opening fixed database: No such file or directory
json-compilation-database: Error while opening JSON database: No such file or directory
Running without flags.
1 warning generated.
/home/ivan/git/bug-readability-container-size-empty/break-readability-container-size-empty.c++:10:10: warning: the 'empty' method should be used to check for emptiness instead of 'size' [readability-container-size-empty]
10 | if (!size())
| ~^~~~~~
| size->empty()
/nix/store/qnwxpk0in4bm43q2qnykvkjxa9qhqd0z-gcc-14.3.0/include/c++/14.3.0/bits/basic_string.h:1232:7: note: method 'basic_string<char>'::empty() defined here
1232 | empty() const _GLIBCXX_NOEXCEPT
| ^break-readability-container-size-empty.c++
The same can also be seen on Godbolt: https://godbolt.org/z/jx4xWscdj
nicovank