Skip to content

Commit 86fb33a

Browse files
committed
Auto merge of rust-lang#10353 - nindalf:master, r=llogiq
Change unusual_byte_groupings to require byte groupings of equal size Fixes issue rust-lang#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.
2 parents 5b6795f + f12b492 commit 86fb33a

6 files changed

+17
-39
lines changed

clippy_lints/src/literal_representation.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ impl WarningType {
210210
cx,
211211
UNUSUAL_BYTE_GROUPINGS,
212212
span,
213-
"digits of hex or binary literal not grouped by four",
213+
"digits of hex, binary or octal literal not in groups of equal size",
214214
"consider",
215215
suggested_format,
216216
Applicability::MachineApplicable,
@@ -427,8 +427,12 @@ impl LiteralDigitGrouping {
427427

428428
let first = groups.next().expect("At least one group");
429429

430-
if (radix == Radix::Binary || radix == Radix::Hexadecimal) && groups.any(|i| i != 4 && i != 2) {
431-
return Err(WarningType::UnusualByteGroupings);
430+
if radix == Radix::Binary || radix == Radix::Octal || radix == Radix::Hexadecimal {
431+
if let Some(second_size) = groups.next() {
432+
if !groups.all(|i| i == second_size) || first > second_size {
433+
return Err(WarningType::UnusualByteGroupings);
434+
}
435+
}
432436
}
433437

434438
if let Some(second) = groups.next() {

tests/ui/large_digit_groups.fixed

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ fn main() {
1111
let _good = (
1212
0b1011_i64,
1313
0o1_234_u32,
14-
0x0123_4567,
14+
0x1_234_567,
1515
1_2345_6789,
1616
1234_f32,
1717
1_234.12_f32,
1818
1_234.123_f32,
1919
1.123_4_f32,
2020
);
2121
let _bad = (
22-
0b11_0110_i64,
22+
0b1_10110_i64,
2323
0xdead_beef_usize,
2424
123_456_f32,
2525
123_456.12_f32,

tests/ui/large_digit_groups.stderr

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,10 @@
1-
error: digits of hex or binary literal not grouped by four
2-
--> $DIR/large_digit_groups.rs:14:9
3-
|
4-
LL | 0x1_234_567,
5-
| ^^^^^^^^^^^ help: consider: `0x0123_4567`
6-
|
7-
= note: `-D clippy::unusual-byte-groupings` implied by `-D warnings`
8-
9-
error: digits of hex or binary literal not grouped by four
10-
--> $DIR/large_digit_groups.rs:22:9
11-
|
12-
LL | 0b1_10110_i64,
13-
| ^^^^^^^^^^^^^ help: consider: `0b11_0110_i64`
14-
15-
error: digits of hex or binary literal not grouped by four
1+
error: digits of hex, binary or octal literal not in groups of equal size
162
--> $DIR/large_digit_groups.rs:23:9
173
|
184
LL | 0xd_e_adbee_f_usize,
195
| ^^^^^^^^^^^^^^^^^^^ help: consider: `0xdead_beef_usize`
6+
|
7+
= note: `-D clippy::unusual-byte-groupings` implied by `-D warnings`
208

219
error: digit groups should be smaller
2210
--> $DIR/large_digit_groups.rs:24:9
@@ -44,5 +32,5 @@ error: digit groups should be smaller
4432
LL | 1_23456.12345_6_f64,
4533
| ^^^^^^^^^^^^^^^^^^^ help: consider: `123_456.123_456_f64`
4634

47-
error: aborting due to 7 previous errors
35+
error: aborting due to 5 previous errors
4836

tests/ui/literals.stderr

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,20 +121,14 @@ error: digits grouped inconsistently by underscores
121121
LL | let fail23 = 3__16___23;
122122
| ^^^^^^^^^^ help: consider: `31_623`
123123

124-
error: digits of hex or binary literal not grouped by four
124+
error: digits of hex, binary or octal literal not in groups of equal size
125125
--> $DIR/literals.rs:38:18
126126
|
127127
LL | let fail24 = 0xAB_ABC_AB;
128128
| ^^^^^^^^^^^ help: consider: `0x0ABA_BCAB`
129129
|
130130
= note: `-D clippy::unusual-byte-groupings` implied by `-D warnings`
131131

132-
error: digits of hex or binary literal not grouped by four
133-
--> $DIR/literals.rs:39:18
134-
|
135-
LL | let fail25 = 0b01_100_101;
136-
| ^^^^^^^^^^^^ help: consider: `0b0110_0101`
137-
138132
error: this is a decimal constant
139133
--> $DIR/literals.rs:46:13
140134
|
@@ -168,5 +162,5 @@ help: if you mean to use a decimal constant, remove the `0` to avoid confusion
168162
LL | let _ = 89;
169163
| ~~
170164

171-
error: aborting due to 21 previous errors
165+
error: aborting due to 20 previous errors
172166

tests/ui/unreadable_literal.fixed

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fn main() {
2323
let _good = (
2424
0b1011_i64,
2525
0o1_234_u32,
26-
0x0123_4567,
26+
0x1_234_567,
2727
65536,
2828
1_2345_6789,
2929
1234_f32,

tests/ui/unreadable_literal.stderr

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
error: digits of hex or binary literal not grouped by four
2-
--> $DIR/unreadable_literal.rs:26:9
3-
|
4-
LL | 0x1_234_567,
5-
| ^^^^^^^^^^^ help: consider: `0x0123_4567`
6-
|
7-
= note: `-D clippy::unusual-byte-groupings` implied by `-D warnings`
8-
91
error: long literal lacking separators
102
--> $DIR/unreadable_literal.rs:34:17
113
|
@@ -68,5 +60,5 @@ error: long literal lacking separators
6860
LL | let _fail5 = 1.100300400;
6961
| ^^^^^^^^^^^ help: consider: `1.100_300_400`
7062

71-
error: aborting due to 11 previous errors
63+
error: aborting due to 10 previous errors
7264

0 commit comments

Comments
 (0)