Open pkasting opened 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.
Sounds like something that belongs in clang-tidy instead?
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).
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).