llvm / llvm/llvm-project

-Wtautological-constant-out-of-range-compare generates counterproductive warning

Open
#177,861 0 comments 0 reactions 0 assignees View on GitHub
clang:diagnostics
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Take into account the following dummy code

```c++
#include

class Foo
{
public:
void setSize(unsigned int size) {
std::vector bitmaps;
if (size > bitmaps.max_size()) {
return;
}
bitmaps.resize(size);
}
};

int main(int argc, char **) {
Foo f;
f.setSize(argc);
return 0;
}
```

We run against clang it will say

`warning: result of comparison of constant 2305843009213693951 with expression of type 'unsigned int' is always false [-Wtautological-constant-out-of-range-compare]`

on the

`if (size > bitmaps.max_size()) {`

as seen in https://godbolt.org/z/Yfrexofh7

Which would lead one to think "ok, let's remove the check, it's useless, clang told me it will always be false"

But then you run it in a 32 bits machine and you realize the check is actually useful [because the vector max size is now much smaller] and the warning is gone as seen in https://godbolt.org/z/rvz8evb19

Contributor guide

Open the contributing guide

Research direction

No repository file or test is named. Start by reproducing the C++ example with Clang on the 64-bit and 32-bit Godbolt links, comparing the warning at the max_size() check; done should mean the diagnostic no longer encourages removing a check that remains useful on another target.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.