[clang-tools-extra] [clang-tidy] fix cppcoreguidelines-narrowing-conversions false positives when narrowing integer to signed integer in C++20 (PR #116591)

Julian Schmidt via cfe-commits cfe-commits at lists.llvm.org
Sun Dec 15 15:01:39 PST 2024


5chmidti wrote:

The move to bugprone makes sense to me. 

However, I'm not sure that conversions of integers to signed integers should be ignored-by-default since C++20. It may be well-defined behavior w.r.t. the resulting value, but the pattern is still prone to bugs. You will get a well-defined wrong value from C++20 on, instead of an implementation-defined wrong value.

---

> 1. really implement defined behavior -> The above example about float to unsigned
For the first level, we emit warning for explicit and implicit cast.

That sounds like a good idea.

> 2. standard implement defined behavior but at least gcc and clang in each target has same behavior <- default

... and MSVC, and NVCC? I'd drop condition on the compilers behaving the same for targets. All implementation-defined behavior according to the standard sounds better than only those that are not agreed upon. There are also downstream versions of compilers for a few architectures (e.g., microcontrollers), and those may or may not follow the practice of their upstream counterparts (different targets).

> 3. most strictly check according to cppcodingguideline. <- create alias in this level

I think that the bugprone check should also detect narrowing conversions for well-defined conversions because the check is primarily about loosing information (in my opinion), not that these narrowing conversions are implementation defined. Though that is also a core part of this check.

https://github.com/llvm/llvm-project/pull/116591


More information about the cfe-commits mailing list