Skip to content

clang-tidy: checks "readability-container-contains" does not handle "find()" #79437

@adesitter

Description

@adesitter

$ cat qq.cpp

#include <map>
bool my_contains_1(std::map<int, int> const& m, int key)
{
   return m.find(key) != m.end(); 
}
bool my_contains_2(std::map<int, int> const& m, int key)
{
   return m.count(key) > 0; 
}

$ /usr/local/clang-17/bin/clang-tidy '-checks=readability-container-contains' contains.cpp -- -std=c++20
1 warning generated.
/tmp/contains.cpp:8:13: warning: use 'contains' to check for membership [readability-container-contains]
8 | return m.count(key) > 0;
| ^~~~~ ~~~
| contains

Despite https://clang.llvm.org/extra/clang-tidy/checks/readability/container-contains.html, readability-container-contains" does not handle "find()".

Activity

PiotrZSL

PiotrZSL commented on Jan 25, 2024

@PiotrZSL
Member

Interesting, in C++20 this code looks like this:

 `-CXXRewrittenBinaryOperator <col:11, col:32> 'bool'
|         `-UnaryOperator <col:23, col:32> 'bool' prefix '!' cannot overflow
|           `-CXXOperatorCallExpr <col:11, col:32> 'bool' '==' adl

when in C++17:

CXXOperatorCallExpr <col:11, col:32> 'bool' '!=' adl

And check support only binaryOperator, so it won't work on iterators objects. Looks like support for those 2 need simply be added.

adesitter

adesitter commented on Jan 25, 2024

@adesitter
Author

readability-container-contains is only relevant in C++20 mode.
operator== is equally affected:

$ cat contains2.cpp 
#include <map>
bool my_not_contains_1(std::map<int, int> const& m, int key)
{
   return m.find(key) == m.end(); 
}
bool my_not_contains_2(std::map<int, int> const& m, int key)
{
   return m.count(key) == 0; 
}
$ /usr/local/clang-17/bin/clang-tidy '-checks=readability-container-contains' contains2.cpp -- -std=c++20
1 warning generated.
/tmp/contains2.cpp:8:13: warning: use 'contains' to check for membership [readability-container-contains]
    8 |    return m.count(key) == 0; 
      |             ^~~~~      ~~~~
      |           ! contains
adesitter

adesitter commented on Jan 31, 2024

@adesitter
Author

readability-container-contains does not handle pointer dereferencing.

$ cat contains.cpp 
#include <map>
bool my_contains_1(std::map<int, int> const* m, int key)
{
   return m->find(key) != m->end(); 
}
bool my_not_contains_1(std::map<int, int> const* m, int key)
{
   return m->find(key) == m->end(); 
}
bool my_contains_2(std::map<int, int> const* m, int key)
{
   return m->count(key) > 0; 
}
bool my_not_contains_2(std::map<int, int> const* m, int key)
{
   return m->count(key) == 0; 
}
$ /usr/local/clang-17/bin/clang-tidy '-checks=readability-container-contains' contains.cpp -- -std=c++20
added a commit that references this issue on Sep 28, 2024
032b33e
added a commit that references this issue on Sep 29, 2024
e08eed9
added a commit that references this issue on Oct 22, 2024
e1dc2f8
added a commit that references this issue on Oct 23, 2024
3605d9a
added a commit that references this issue on Nov 4, 2024
7fb5211
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Participants

      @PiotrZSL@adesitter

      Issue actions

        clang-tidy: checks "readability-container-contains" does not handle "find()" · Issue #79437 · llvm/llvm-project