Skip to content

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

@sadan4

Description

@sadan4

given the following code

#include <regex>
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: #88203

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions