unusual_byte_groupings triggers for binary literals
#11,749 opened on Nov 2, 2023
Description
Summary
This is kind of related to https://github.com/rust-lang/rust-clippy/issues/9183 and https://github.com/rust-lang/rust-clippy/issues/11243.
When I'm using binary literals for parsing formats, I usually don't use equal groupings but instead use groupings that reflect the underlying format.
It's easy enough to allow unusual_byte_groupings for these parts of the code, but I'm probably not the only one doing that and maybe the lint should be disabled completely for binary literals or only trigger in more restricted cases (all groups the same but one is off-by-one or so?).
Lint Name
unusual_byte_groupings
Reproducer
The following example is from parsing the header of an MP3 frame, specifically parsing the layer description bits.
The second byte of the header contains 4 groups: 3 bits, 2 bits, 2 bits (what we're interested in here) and another bit in the end so I'd use 0b000_00_11_0 as a mask.
fn main() {
let data = [0u8; 4];
let layer_desc = (data[1] & 0b000_00_11_0) >> 1;
println!("meh {layer_desc}");
}
I saw this happen:
warning: digits of hex, binary or octal literal not in groups of equal size
--> src/main.rs:4:33
|
4 | let layer_desc = (data[1] & 0b000_00_11_0) >> 1;
| ^^^^^^^^^^^^^ help: consider: `0b0000_0110`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unusual_byte_groupings
= note: `#[warn(clippy::unusual_byte_groupings)]` on by default
I expected to see this happen: [nothing]
Version
rustc 1.73.0 (cc66ad468 2023-10-03)
binary: rustc
commit-hash: cc66ad468955717ab92600c770da8c1601a4ff33
commit-date: 2023-10-03
host: x86_64-unknown-linux-gnu
release: 1.73.0
LLVM version: 17.0.2
Additional Labels
No response