[clang-tidy] `bugprone-misplaced-widening-cast` shouldn't warn on correct `constexpr` values
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
Consider the following (https://godbolt.org/z/5jTTWh9cb):
```c++
#include
void bar(std::size_t);
void foo() {
constexpr int x = 256;
bar(static_cast(x * 2));
}
```
The `static_cast` is indeed pointless if the intent was to avoid overflow of `int` before passing to `bar()`. The correct form would be
```c++
static_cast(x) * 2
```
However in this case, because `x` is `constexpr`, the compiler (and by extension `clang-tidy`) can compute the value of `x * 2` and see that it never overflows. Even if the cast is pointless, the code is still provably correct as written.
In this case `clang-tidy` should suppress the `misplaced-widening-cast` diagnostic, because otherwise it is pointless churn. There is no "cast" occurring anyways, the compiler will just constant fold the value to `size_t` anyways.
Contributor guide
Assessment
This issue has not been assessed yet.