Skip to content

[clang-tidy] False negative modernize-use-integer-sign-comparison without cast? #127471

@chrchr-github

Description

@chrchr-github
#include <vector>

bool f(int i, const std::vector<int>& v) {
    return i >= v.size();
}
bool g(int i, const std::vector<int>& v) {
    return i >= static_cast<int>(v.size()); // warning
}
bool h(int i, const std::vector<int>& v) {
    return static_cast<std::size_t>(i) >= v.size();
}

int main() {
    return f(-1, {});
}
<source>:7:12: warning: comparison between 'signed' and 'unsigned' integers [modernize-use-integer-sign-comparison]
    1 | #include <vector>
    2 | 
    3 | bool f(int i, const std::vector<int>& v) {
    4 |     return i >= v.size();
    5 | }
    6 | bool g(int i, const std::vector<int>& v) {
    7 |     return i >= static_cast<int>(v.size());
      |            ^ ~~~~~~~~~~~~~~~~~~~~         
      |            std::cmp_greater_equal( ,      )
1 warning generated.

I wonder if the observed behavior is intentional (warning only for g(), but not for f() and h()).
https://godbolt.org/z/8GWMrh781

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions