<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/106987>106987</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[clang-tidy] Check request: detect incorrect usage of `std::bit_cast`
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang-tidy,
check-request
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
carlosgalvezp
</td>
</tr>
</table>
<pre>
`std::bit_cast` is often presented as the correct alternative to type punning in C++. However, we see people use it as a drop-in replacement to `reinterpret_cast` , which only obfuscates the code and still has UB:
```cpp
float x{};
-int* y = reinterpret_cast<int*>(&x);
+int* y = std::bit_cast<int*>(&x);
```
This usage is incorrect. Instead, the code should do:
```cpp
int y = std::bit_cast<int>(x);
```
Most likely all usages of `std::bit_cast<PointerType>` are incorrect.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyEk0lv2zAQhX_N6DKwQVPW4oMOXmq0hwI9pOeCIscSG5pUyZET99cX8pKkSIsAggQMMU_fe8NRKdnOEzVQbKDYZWrkPsRGq-hC6pQ70e8ha4M5N1CKxAbyNeTr1vIPrRJDKdAmDAcmj0OkRJ7JoErIPaEOMZJmVI4pesX2RMgB-TwQDqP31ndoPW5BbkBu5vg5PNGJIsgtPhEmIhwoDI5wTISWJ1mFJoZhZj1GGpzSdCTPkyiUIpL1THGI9Ap30eqt7jF4d8bQHsakFdMd0BAqbzCxdQ57lfD7ZnIodiDu71JcHz0M18rBBcX4DNUGqh3km2t1Zj2DXOMZId_hO5Z8ez2H_BPIGmT5DHL10gxy81f7-6A_6L9DviV_6G3CMamOphlZfxvHHL_4xKTMlM1LCqkPozNowqv9d8at5w_4LnAfk30NidHZR3JnVM5dIadrhP-8ZPn2W7jE-XAeaPpHKVBFemMpM01uVvlKZdQsKlnIOi-rOusbsVDLQ9WuZEWka6WLsq6olaqmtioWRme2kUIuxUrIRS6ropq3elnUuSqrZV6VS1PBUtBRWTd37nSch9hlNqWRmoUoV3WVOdWSS5ftkVI75bsZW3MGKUFup1JP-nEW6ddIiadqsctiM2nN2rFLsBTOJk6v6mzZXbbxjVixw-2kg3edfI2GeNqtlwxuk_5fhqXIxuiannlI05Hcg9x3lvuxnetwBLmfCG6f2RDDT9IMcn8xm0Dub35PjfwTAAD__53_TJU">