Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unusual_byte_groupings fires for octal values represented in binary #6556

Closed
koutheir opened this issue Jan 7, 2021 · 7 comments
Closed
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing good-first-issue These issues are a good way to get started with Clippy I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@koutheir
Copy link

koutheir commented Jan 7, 2021

Lint name: unusual_byte_groupings

I tried this code:

let unix_mode = 0b111_101_101; // rwx r-x r-x

I didn't expect to see the warning digits of hex or binary literal not grouped by four, because grouping by 3 bits is very reasonable when binary is clearer than octal.

Meta

  • cargo clippy -V:
    clippy 0.0.212 (e1884a8 2020-12-29)
    
  • rustc -Vv:
    rustc 1.49.0 (e1884a8e3 2020-12-29)
    binary: rustc
    commit-hash: e1884a8e3c3e813aada8254edfa120e85bf5ffca
    commit-date: 2020-12-29
    host: x86_64-unknown-linux-gnu
    release: 1.49.0
    
@koutheir koutheir added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Jan 7, 2021
@camsteffen
Copy link
Contributor

Hmm I guess we'll just add a special case for 3 groups of 3? Usually the groups don't have semantic meaning like they do in a unix mode.

@camsteffen camsteffen added the good-first-issue These issues are a good way to get started with Clippy label Feb 18, 2021
@koutheir
Copy link
Author

I think this doesn't depend on any semantic meaning. Grouping by four bits is a specification of one hexadecimal digit in the binary numeral system. Likewise, grouping by three bits is a specification of one octal digit in binary. The octal numeral system is used often enough to make this warning unexpected in computer science.

@mohanson
Copy link

mohanson commented May 8, 2021

In virtual machine development, we often need to encode/decode an instruction, and the instruction encoding usually has its own format, such as RISC-V ISA:

// |31|30|29|28|27|26|25|24|23|22|21|20|19|18|17|16|15|14|13|12|11|10|09|08|07|06|05|04|03|02|01|00|
// |funct7              |rs2           |rs1           |funct3  |rd            |opcode              | R-type
// |rs3           |funct|rs2           |rs1           |funct3  |rd            |opcode              | R4-type
// |imm[11:0]                          |rs1           |funct3  |rd            |opcode              | I-type
// |imm[11:5]           |rs2           |rs1           |funct3  |imm[4:0]      |opcode              | S-type
// |imm[31:12]                                                 |rd            |opcode              | U-type
// |12|imm[10:5]        |rs2           |rs1           |funct3  |imm[4:1]   |11|opcode              | B-type
// |20|imm[10:1]                    |11|imm[19:12]             |rd            |opcode              | J-type

I think the addition of this check is an impulsive decision, and it should never exist, whether it is a 4-bit grouping or a 3-bit grouping, when people choose to use binary to represent a number, they understand what they are doing better than Clippy.

@camsteffen
Copy link
Contributor

I think this doesn't depend on any semantic meaning. Grouping by four bits is a specification of one hexadecimal digit in the binary numeral system. Likewise, grouping by three bits is a specification of one octal digit in binary. The octal numeral system is used often enough to make this warning unexpected in computer science.

Good point. Seems very sensible to not lint groups of three.

In virtual machine development,

Certainly there can be any grouping for any proprietary format. But these are "unusual". However, maybe they really are not that unusual and so this lint should not warn by default?

@koutheir
Copy link
Author

Certainly there can be any grouping for any proprietary format. But these are "unusual". However, maybe they really are not that unusual and so this lint should not warn by default?

I think formats that deal with bit fields are uncommon enough to keep this lint enabled by default.

@nindalf
Copy link
Contributor

nindalf commented Feb 16, 2023

@rustbot claim

bors added a commit that referenced this issue Feb 16, 2023
Change unusual_byte_groupings to require byte groupings of equal size

Fixes issue #6556

This lint required byte groupings of size 2 or 4 for `Radix::Binary` and `Radix::Hexadecimal`. Since there are good reasons for allowing groups of other sizes, this PR relaxes the restriction. This lint now requires that

- group sizes after the first group be of the same size and
- greater or equal in size to the first group.

---

changelog: [`unusual_byte_groupings`]: reduce false positives by relaxing restriction requiring groups of specific sizes.
@llogiq
Copy link
Contributor

llogiq commented Feb 16, 2023

Fixed by #10353.

@llogiq llogiq closed this as completed Feb 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing good-first-issue These issues are a good way to get started with Clippy I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

No branches or pull requests

5 participants