[clang-tools-extra] [clang-tidy] Add `modernize-use-bit-cast` check (PR #189962)
Daniil Dudkin via cfe-commits
cfe-commits at lists.llvm.org
Sat Sep 5 03:42:57 PDT 2026
unterumarmung wrote:
@serge-sans-paille @5chmidti @zwuis @vbvictor I rebased the PR onto the current `main` and addressed the remaining review comments.
The check now supports pointer objects and volatile values, and uses Clang's fix-it helpers.
While testing comma expressions, I found that always inserting `(void)` was unnecessary:
```cpp
int value = (std::memcpy(&dst, &src, sizeof(src)), 42);
```
For scalar operands, the fix is now:
```cpp
int value = (dst = std::bit_cast<unsigned int>(src), 42);
```
The cast is retained when a class, enum, or dependent operand could cause overload resolution to select `operator,`.
I also found that printing the destination type directly produced invalid fixes for anonymous records and lambdas:
```cpp
auto dst = [] {};
std::memcpy(&dst, &src, sizeof(src));
```
The old fix tried to use Clang's internal lambda type spelling. The check now emits:
```cpp
dst = std::bit_cast<decltype(dst)>(src);
```
Another false positive involved user-defined functions that happened to have the same name and pointer parameters as `memcpy`:
```cpp
void *memcpy(void *, const void *, int);
::memcpy(&dst, &src, sizeof(src));
```
The check now requires the third parameter to have the canonical `size_t` type, so this overload is ignored.
Non-dependent copies inside function templates are diagnosed once, while dependent copies and calls inside requires expressions remain ignored. `sizeof(Type)` and `sizeof(*ptr)` are accepted when the resulting type matches either copied object type. Please take another look.
https://github.com/llvm/llvm-project/pull/189962
More information about the cfe-commits
mailing list