[clang-tidy] Add `std::byteswap` patterns to `modernize-use-std-bit`
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
A modernization check for hand-written byte-swap code would be useful in C++23 and later. The obvious target is canonical bit-twiddling expressions that are really just spelling `std::byteswap`.
For example, code like this is still fairly common:
```cpp
uint32_t y =
((x & 0x000000FFu) << 24) |
((x & 0x0000FF00u) << 8) |
((x & 0x00FF0000u) >> 8) |
((x & 0xFF000000u) >> 24);
```
and similarly for 16-bit or 64-bit values.
In C++23 the better spelling is:
```cpp
auto y = std::byteswap(x);
```
The standard facility is shorter, clearer, and much less error-prone than hand-written masking and shifting. It also makes the intent immediately obvious to readers and reviewers.
It would also be reasonable to recognize wrappers around target-specific byte-swap intrinsics when the wrapper is clearly just spelling the same operation by hand, but the main value is in catching the portable mask-and-shift idioms that appear in real codebases.
Contributor guide
Assessment
This issue has not been assessed yet.