Closed
Description
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