Issue 208078
Summary [clang-tidy] `bugprone-misplaced-widening-cast` shouldn't warn on correct `constexpr` values
Labels
Assignees
Reporter Jacobfaib
    Consider the following (https://godbolt.org/z/5jTTWh9cb):
```c++
#include <cstddef>

void bar(std::size_t);

void foo() {
  constexpr int x = 256;

  bar(static_cast<std::size_t>(x * 2));
}
```
The `static_cast<std::size_t>` is indeed pointless if the intent was to avoid overflow of `int` before passing to `bar()`. The correct form would be
```c++
static_cast<std::size_t>(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.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to