Auto merge of #10353 - nindalf:master, r=llogiq

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.
This commit is contained in:
bors 2023-02-16 11:40:45 +00:00
commit 86fb33a973
6 changed files with 17 additions and 39 deletions

View File

@ -210,7 +210,7 @@ fn display(&self, suggested_format: String, cx: &EarlyContext<'_>, span: rustc_s
cx,
UNUSUAL_BYTE_GROUPINGS,
span,
"digits of hex or binary literal not grouped by four",
"digits of hex, binary or octal literal not in groups of equal size",
"consider",
suggested_format,
Applicability::MachineApplicable,
@ -427,8 +427,12 @@ fn get_group_size<'a>(
let first = groups.next().expect("At least one group");
if (radix == Radix::Binary || radix == Radix::Hexadecimal) && groups.any(|i| i != 4 && i != 2) {
return Err(WarningType::UnusualByteGroupings);
if radix == Radix::Binary || radix == Radix::Octal || radix == Radix::Hexadecimal {
if let Some(second_size) = groups.next() {
if !groups.all(|i| i == second_size) || first > second_size {
return Err(WarningType::UnusualByteGroupings);
}
}
}
if let Some(second) = groups.next() {

View File

@ -11,7 +11,7 @@ fn main() {
let _good = (
0b1011_i64,
0o1_234_u32,
0x0123_4567,
0x1_234_567,
1_2345_6789,
1234_f32,
1_234.12_f32,
@ -19,7 +19,7 @@ fn main() {
1.123_4_f32,
);
let _bad = (
0b11_0110_i64,
0b1_10110_i64,
0xdead_beef_usize,
123_456_f32,
123_456.12_f32,

View File

@ -1,22 +1,10 @@
error: digits of hex or binary literal not grouped by four
--> $DIR/large_digit_groups.rs:14:9
|
LL | 0x1_234_567,
| ^^^^^^^^^^^ help: consider: `0x0123_4567`
|
= note: `-D clippy::unusual-byte-groupings` implied by `-D warnings`
error: digits of hex or binary literal not grouped by four
--> $DIR/large_digit_groups.rs:22:9
|
LL | 0b1_10110_i64,
| ^^^^^^^^^^^^^ help: consider: `0b11_0110_i64`
error: digits of hex or binary literal not grouped by four
error: digits of hex, binary or octal literal not in groups of equal size
--> $DIR/large_digit_groups.rs:23:9
|
LL | 0xd_e_adbee_f_usize,
| ^^^^^^^^^^^^^^^^^^^ help: consider: `0xdead_beef_usize`
|
= note: `-D clippy::unusual-byte-groupings` implied by `-D warnings`
error: digit groups should be smaller
--> $DIR/large_digit_groups.rs:24:9
@ -44,5 +32,5 @@ error: digit groups should be smaller
LL | 1_23456.12345_6_f64,
| ^^^^^^^^^^^^^^^^^^^ help: consider: `123_456.123_456_f64`
error: aborting due to 7 previous errors
error: aborting due to 5 previous errors

View File

@ -121,7 +121,7 @@ error: digits grouped inconsistently by underscores
LL | let fail23 = 3__16___23;
| ^^^^^^^^^^ help: consider: `31_623`
error: digits of hex or binary literal not grouped by four
error: digits of hex, binary or octal literal not in groups of equal size
--> $DIR/literals.rs:38:18
|
LL | let fail24 = 0xAB_ABC_AB;
@ -129,12 +129,6 @@ LL | let fail24 = 0xAB_ABC_AB;
|
= note: `-D clippy::unusual-byte-groupings` implied by `-D warnings`
error: digits of hex or binary literal not grouped by four
--> $DIR/literals.rs:39:18
|
LL | let fail25 = 0b01_100_101;
| ^^^^^^^^^^^^ help: consider: `0b0110_0101`
error: this is a decimal constant
--> $DIR/literals.rs:46:13
|
@ -168,5 +162,5 @@ help: if you mean to use a decimal constant, remove the `0` to avoid confusion
LL | let _ = 89;
| ~~
error: aborting due to 21 previous errors
error: aborting due to 20 previous errors

View File

@ -23,7 +23,7 @@ fn main() {
let _good = (
0b1011_i64,
0o1_234_u32,
0x0123_4567,
0x1_234_567,
65536,
1_2345_6789,
1234_f32,

View File

@ -1,11 +1,3 @@
error: digits of hex or binary literal not grouped by four
--> $DIR/unreadable_literal.rs:26:9
|
LL | 0x1_234_567,
| ^^^^^^^^^^^ help: consider: `0x0123_4567`
|
= note: `-D clippy::unusual-byte-groupings` implied by `-D warnings`
error: long literal lacking separators
--> $DIR/unreadable_literal.rs:34:17
|
@ -68,5 +60,5 @@ error: long literal lacking separators
LL | let _fail5 = 1.100300400;
| ^^^^^^^^^^^ help: consider: `1.100_300_400`
error: aborting due to 11 previous errors
error: aborting due to 10 previous errors