llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Incorrect false positive warning emitted by clang-tidy readability-container-size-empty

Open adesitter opened this issue 1 year ago • 1 comments

Considering:

#include <string_view>

struct Label {
    virtual ~Label();
};

bool operator==(std::string_view, Label const&);

auto f() {
    std::string_view s{"aa"};
    return s == Label{};
}

clang-tidy -checks=readability-container-size-empty emits the following incorrect warning:

<source>:11:12: warning: the 'empty' method should be used to check for emptiness instead of comparing to an empty object [readability-container-size-empty]
   11 |     return s == Label{};
      |            ^~~~~~~~~~~~
      |            s.empty()
/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/16.0.0/../../../../include/c++/16.0.0/string_view:251:7: note: method 'basic_string_view'::empty() defined here
  251 |       empty() const noexcept
      |       ^
1 warning generated.

compiler explorer

adesitter avatar Oct 07 '25 13:10 adesitter

@llvm/issue-subscribers-clang-tidy

Author: Arnaud Desitter (adesitter)

Considering: ``` #include <string_view>

struct Label { virtual ~Label(); };

bool operator==(std::string_view, Label const&);

auto f() { std::string_view s{"aa"}; return s == Label{}; }


`clang-tidy -checks=readability-container-size-empty` emits the following incorrect warning:

<source>:11:12: warning: the 'empty' method should be used to check for emptiness instead of comparing to an empty object [readability-container-size-empty] 11 | return s == Label{}; | ^~~~~~~~~~~~ | s.empty() /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/16.0.0/../../../../include/c++/16.0.0/string_view:251:7: note: method 'basic_string_view'::empty() defined here 251 | empty() const noexcept | ^ 1 warning generated.


[compiler explorer](https://godbolt.org/z/6953xh7d4)
</details>

llvmbot avatar Oct 07 '25 14:10 llvmbot