llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.6k stars 11.82k forks source link

FR: Warn on ineffective use of std::bit_cast<> #74639

Open pkasting opened 10 months ago

pkasting commented 10 months ago

I propose warning on any use of std::bit_cast<> where source or destination types are pointers.

Such usage is technically safe and well-defined, but it's: (1) Unnecessary, as reinterpret_cast<> is also safe and well-defined for this. (2) A strong signal the user misunderstands bit_cast as "reinterpret_cast, but it's not UB".

In auditing Chromium and its third-party dependencies, we've found most usage of std::/absl::/etc.::bit_cast falls into the above bucket (~2500 uses in ~400 source files).

avidrissman commented 10 months ago

Sorry, the “~2500 uses in ~400 source files” is a misunderstanding. That is the total number of bit_cast usages in Chromium. There are a few dozen uses on pointer types, but those are not easy to move, as they cross API boundaries.

tbaederr commented 10 months ago

Sounds like something that belongs in clang-tidy instead?

pkasting commented 10 months ago

I don't know; we have -Wreinterpret-base-class for another case of "defined cast, but probably isn't how you wanted to hold it, you're likely about to do some UB".

I also don't know how clang-tidy would propose to fix these (if we wanted that).