Skip to content

Commit c6d53ad

Browse files
authored
Merge pull request #2813 from terry90/master
unreadable_literal: Fills hexadecimal values with 0 to allow better grouping
2 parents 1c6c79f + 2c14518 commit c6d53ad

File tree

5 files changed

+84
-34
lines changed

5 files changed

+84
-34
lines changed

clippy_lints/src/literal_representation.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,16 +193,22 @@ impl<'a> DigitInfo<'a> {
193193
self.suffix.unwrap_or("")
194194
)
195195
} else {
196-
let hint = self.digits
196+
let filtered_digits_vec = self.digits
197197
.chars()
198-
.rev()
199198
.filter(|&c| c != '_')
200-
.collect::<Vec<_>>()
199+
.rev()
200+
.collect::<Vec<_>>();
201+
let mut hint = filtered_digits_vec
201202
.chunks(group_size)
202203
.map(|chunk| chunk.into_iter().rev().collect())
203204
.rev()
204205
.collect::<Vec<String>>()
205206
.join("_");
207+
// Forces hexadecimal values to be grouped by 4 being filled with zeroes (e.g 0x00ab_cdef)
208+
let nb_digits_to_fill = filtered_digits_vec.len() % 4;
209+
if self.radix == Radix::Hexadecimal && nb_digits_to_fill != 0 {
210+
hint = format!("{:0>4}{}", &hint[..nb_digits_to_fill], &hint[nb_digits_to_fill..]);
211+
}
206212
format!(
207213
"{}{}{}",
208214
self.prefix.unwrap_or(""),

tests/ui/large_digit_groups.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ error: digit groups should be smaller
1010
--> $DIR/large_digit_groups.rs:7:31
1111
|
1212
7 | let bad = (0b1_10110_i64, 0x1_23456_78901_usize, 1_23456_f32, 1_23456.12_f32, 1_23456.12345_f32, 1_23456.12345_6_f32);
13-
| ^^^^^^^^^^^^^^^^^^^^^ help: consider: `0x123_4567_8901_usize`
13+
| ^^^^^^^^^^^^^^^^^^^^^ help: consider: `0x0123_4567_8901_usize`
1414

1515
error: digit groups should be smaller
1616
--> $DIR/large_digit_groups.rs:7:54

tests/ui/literals.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
2-
31
#![warn(mixed_case_hex_literals)]
42
#![warn(unseparated_literal_suffix)]
53
#![warn(zero_prefixed_literal)]
@@ -31,4 +29,16 @@ fn main() {
3129

3230
let ok11 = 0o123;
3331
let ok12 = 0b10_1010;
32+
33+
let ok13 = 0xab_abcd;
34+
let ok14 = 0xBAFE_BAFE;
35+
let ok15 = 0xab_cabc_abca_bcab_cabc;
36+
let ok16 = 0xFE_BAFE_ABAB_ABCD;
37+
let ok17 = 0x123_4567_8901_usize;
38+
39+
let fail9 = 0xabcdef;
40+
let fail10 = 0xBAFEBAFE;
41+
let fail11 = 0xabcdeff;
42+
let fail12 = 0xabcabcabcabcabcabc;
43+
let fail13 = 0x1_23456_78901_usize;
3444
}

tests/ui/literals.stderr

Lines changed: 61 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,124 @@
11
error: inconsistent casing in hexadecimal literal
2-
--> $DIR/literals.rs:14:17
2+
--> $DIR/literals.rs:12:17
33
|
4-
14 | let fail1 = 0xabCD;
4+
12 | let fail1 = 0xabCD;
55
| ^^^^^^
66
|
77
= note: `-D mixed-case-hex-literals` implied by `-D warnings`
88

99
error: inconsistent casing in hexadecimal literal
10-
--> $DIR/literals.rs:15:17
10+
--> $DIR/literals.rs:13:17
1111
|
12-
15 | let fail2 = 0xabCD_u32;
12+
13 | let fail2 = 0xabCD_u32;
1313
| ^^^^^^^^^^
1414

1515
error: inconsistent casing in hexadecimal literal
16-
--> $DIR/literals.rs:16:17
16+
--> $DIR/literals.rs:14:17
1717
|
18-
16 | let fail2 = 0xabCD_isize;
18+
14 | let fail2 = 0xabCD_isize;
1919
| ^^^^^^^^^^^^
2020

2121
error: integer type suffix should be separated by an underscore
22-
--> $DIR/literals.rs:17:27
22+
--> $DIR/literals.rs:15:27
2323
|
24-
17 | let fail_multi_zero = 000_123usize;
24+
15 | let fail_multi_zero = 000_123usize;
2525
| ^^^^^^^^^^^^
2626
|
2727
= note: `-D unseparated-literal-suffix` implied by `-D warnings`
2828

2929
error: this is a decimal constant
30-
--> $DIR/literals.rs:17:27
30+
--> $DIR/literals.rs:15:27
3131
|
32-
17 | let fail_multi_zero = 000_123usize;
32+
15 | let fail_multi_zero = 000_123usize;
3333
| ^^^^^^^^^^^^
3434
|
3535
= note: `-D zero-prefixed-literal` implied by `-D warnings`
3636
help: if you mean to use a decimal constant, remove the `0` to remove confusion
3737
|
38-
17 | let fail_multi_zero = 123usize;
38+
15 | let fail_multi_zero = 123usize;
3939
| ^^^^^^^^
4040
help: if you mean to use an octal constant, use `0o`
4141
|
42-
17 | let fail_multi_zero = 0o123usize;
42+
15 | let fail_multi_zero = 0o123usize;
4343
| ^^^^^^^^^^
4444

4545
error: integer type suffix should be separated by an underscore
46-
--> $DIR/literals.rs:22:17
46+
--> $DIR/literals.rs:20:17
4747
|
48-
22 | let fail3 = 1234i32;
48+
20 | let fail3 = 1234i32;
4949
| ^^^^^^^
5050

5151
error: integer type suffix should be separated by an underscore
52-
--> $DIR/literals.rs:23:17
52+
--> $DIR/literals.rs:21:17
5353
|
54-
23 | let fail4 = 1234u32;
54+
21 | let fail4 = 1234u32;
5555
| ^^^^^^^
5656

5757
error: integer type suffix should be separated by an underscore
58-
--> $DIR/literals.rs:24:17
58+
--> $DIR/literals.rs:22:17
5959
|
60-
24 | let fail5 = 1234isize;
60+
22 | let fail5 = 1234isize;
6161
| ^^^^^^^^^
6262

6363
error: integer type suffix should be separated by an underscore
64-
--> $DIR/literals.rs:25:17
64+
--> $DIR/literals.rs:23:17
6565
|
66-
25 | let fail6 = 1234usize;
66+
23 | let fail6 = 1234usize;
6767
| ^^^^^^^^^
6868

6969
error: float type suffix should be separated by an underscore
70-
--> $DIR/literals.rs:26:17
70+
--> $DIR/literals.rs:24:17
7171
|
72-
26 | let fail7 = 1.5f32;
72+
24 | let fail7 = 1.5f32;
7373
| ^^^^^^
7474

7575
error: this is a decimal constant
76-
--> $DIR/literals.rs:30:17
76+
--> $DIR/literals.rs:28:17
7777
|
78-
30 | let fail8 = 0123;
78+
28 | let fail8 = 0123;
7979
| ^^^^
8080
help: if you mean to use a decimal constant, remove the `0` to remove confusion
8181
|
82-
30 | let fail8 = 123;
82+
28 | let fail8 = 123;
8383
| ^^^
8484
help: if you mean to use an octal constant, use `0o`
8585
|
86-
30 | let fail8 = 0o123;
86+
28 | let fail8 = 0o123;
8787
| ^^^^^
8888

89-
error: aborting due to 11 previous errors
89+
error: long literal lacking separators
90+
--> $DIR/literals.rs:39:17
91+
|
92+
39 | let fail9 = 0xabcdef;
93+
| ^^^^^^^^ help: consider: `0x00ab_cdef`
94+
|
95+
= note: `-D unreadable-literal` implied by `-D warnings`
96+
97+
error: long literal lacking separators
98+
--> $DIR/literals.rs:40:18
99+
|
100+
40 | let fail10 = 0xBAFEBAFE;
101+
| ^^^^^^^^^^ help: consider: `0xBAFE_BAFE`
102+
103+
error: long literal lacking separators
104+
--> $DIR/literals.rs:41:18
105+
|
106+
41 | let fail11 = 0xabcdeff;
107+
| ^^^^^^^^^ help: consider: `0x0abc_deff`
108+
109+
error: long literal lacking separators
110+
--> $DIR/literals.rs:42:18
111+
|
112+
42 | let fail12 = 0xabcabcabcabcabcabc;
113+
| ^^^^^^^^^^^^^^^^^^^^ help: consider: `0x00ab_cabc_abca_bcab_cabc`
114+
115+
error: digit groups should be smaller
116+
--> $DIR/literals.rs:43:18
117+
|
118+
43 | let fail13 = 0x1_23456_78901_usize;
119+
| ^^^^^^^^^^^^^^^^^^^^^ help: consider: `0x0123_4567_8901_usize`
120+
|
121+
= note: `-D large-digit-groups` implied by `-D warnings`
122+
123+
error: aborting due to 16 previous errors
90124

tests/ui/unreadable_literal.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ error: long literal lacking separators
1010
--> $DIR/unreadable_literal.rs:7:30
1111
|
1212
7 | let bad = (0b110110_i64, 0x12345678901_usize, 123456_f32, 1.234567_f32);
13-
| ^^^^^^^^^^^^^^^^^^^ help: consider: `0x123_4567_8901_usize`
13+
| ^^^^^^^^^^^^^^^^^^^ help: consider: `0x0123_4567_8901_usize`
1414

1515
error: long literal lacking separators
1616
--> $DIR/unreadable_literal.rs:7:51

0 commit comments

Comments
 (0)