Skip to content

[clang-tidy] false positive readability-container-size-empty #88203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
iillyyaa opened this issue Apr 9, 2024 · 0 comments · Fixed by #93724
Closed

[clang-tidy] false positive readability-container-size-empty #88203

iillyyaa opened this issue Apr 9, 2024 · 0 comments · Fixed by #93724
Assignees
Labels
clang-tidy false-positive Warning fires when it should not

Comments

@iillyyaa
Copy link

iillyyaa commented Apr 9, 2024

std::regex_result type has two methods: difference_type length(size_type n = 0) const and bool empty() const
However, they are not an exact match for each other with respect to the readability-container-size-empty check, in particular, when the index n is supplied, since there is no corresponding empty(size_type n) overload.

Yet, this (trimmed) code is claimed to violate the readability-container-size-empty check:

#include <regex>
#include <string>

bool test(const std::string& val) {
    static const std::regex re("([0-9]+)/([A-B]+)");
    if (std::smatch m; std::regex_match(val, m, re)) {
        return m.length(2) > 0;
    }
    return false;
}

Here is how it was originally reported against a similar fragment in our code base:

/home/ilyak/veo/crux/app/bbr/key.cpp:37:9: error: the 'empty' method should be used to check for emptiness instead of 'length' [readability-container-size-empty,-warnings-as-errors]
   37 |     if (m.length(4) > 0) {
      |         ^~~~~~~~~~~~~~~
      |         !m.empty()
/home/ilyak/veo/oe-scarthgap/tmp/build-glibc/sysroots/intel-skylake-64/usr/lib/x86_64-oe-linux/13.2.0/../../../include/c++/13.2.0/bits/regex.h:1887:7: note: method 'match_results'::empty() defined here
 1887 |       empty() const noexcept
      |       ^

Reproducible in clang-tidy (master) via: https://godbolt.org/z/eocdWo7j4

@EugeneZelenko EugeneZelenko added the false-positive Warning fires when it should not label Apr 9, 2024
@PiotrZSL PiotrZSL self-assigned this May 28, 2024
PiotrZSL added a commit to PiotrZSL/llvm-project that referenced this issue May 29, 2024
…container-size-empty

Verify that size/length methods are called with no
arguments.

Closes llvm#88203
PiotrZSL added a commit that referenced this issue May 30, 2024
…container-size-empty (#93724)

Verify that size/length methods are called with no arguments.

Closes #88203
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang-tidy false-positive Warning fires when it should not
Projects
None yet
3 participants