llvm / llvm/llvm-project

[clang-tidy] false positive and invalid fix for readability-container-size-empty with regex

Open
#171,193 5 comments 0 reactions 0 assignees View on GitHub
clang-tidy false-positive
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

given the following code
```cpp
#include
void foo() {
std::smatch match;
if (match.length());
}
```
clang-tidy will suggest to replace `match.length()` with `!match.empty()`; however, this is invalid because for successful matches of zero length, `match.length() == 0` and `match.empty() == false`.

godbolt sample demonstrating clang-tidy's incorrect diagnostic https://godbolt.org/z/qnzc4Pcrc

godbolt sample demonstrating a zero length match (execute the code) https://godbolt.org/z/YvT7hb4f4

regex 101 sample demonstrating a zero length regex & match https://regex101.com/r/dzUrRp/1

Related: https://github.com/llvm/llvm-project/issues/88203

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.