Issue 138566
Summary Different behavior for comparison of integer comparison
Labels new issue
Assignees
Reporter StefanoD
    clang-version: 20.1.0
compiler flags: -Wall -O1 -Wextra -Wshadow -Weffc++ -Wno-long-long -pedantic -pedantic-errors -std=c++14

When two variables are multiplied, which leads to integer promotion and the result is not stored into a variable, following example [compiles without problems](https://godbolt.org/z/69v43z9nx):

```cpp
#include <cstdint>

bool square_less_than_method_A(uint16_t a, uint64_t b) {
    // a * a does integer promotion
    return (a * a < b);
}

int main(int, char**) {
    return 0;
}
```

Assigning the product to a variable, results in a [compile error](https://godbolt.org/z/sMso378va):

```cpp
#include <cstdint>

bool square_less_than_method_B(uint16_t a, uint64_t b) {
 const auto a_squared = a * a;
    return a_squared < b;
}

int main(int, char**) {
    return 0;
}
```

```
<source>:5:22: warning: comparison of integers of different signs: 'const int' and 'uint64_t' (aka 'unsigned long') [-Wsign-compare]
    5 |     return a_squared < b;
      | ~~~~~~~~~ ^ ~
```

The second example should also compile as `a*a` is always positive. So, I get here a false-positive warning.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to