unreadable_literal: Fills hexadecimal values with 0 to allow grouping (c.f #2300)

This commit is contained in:
Terry Raimondo 2018-05-28 13:55:27 +02:00
parent fc008aa14c
commit 2033a1eb0e
No known key found for this signature in database
GPG key ID: 28F210B495D27D90
3 changed files with 67 additions and 37 deletions

View file

@ -193,7 +193,7 @@ impl<'a> DigitInfo<'a> {
self.suffix.unwrap_or("")
)
} else {
let hint = self.digits
let mut hint = self.digits
.chars()
.rev()
.filter(|&c| c != '_')
@ -203,6 +203,12 @@ impl<'a> DigitInfo<'a> {
.rev()
.collect::<Vec<String>>()
.join("_");
// Forces hexadecimal values to be grouped by 4 being filled with zeroes (e.g 0x00ab_cdef)
let nb_digits_to_fill = self.digits.len() % 4;
if self.radix == Radix::Hexadecimal && nb_digits_to_fill != 0 {
let filled_digits = format!("{:0>1$}", &hint[..nb_digits_to_fill], 4);
hint = format!("{}{}", filled_digits, &hint[nb_digits_to_fill..]);
}
format!(
"{}{}{}",
self.prefix.unwrap_or(""),

View file

@ -1,5 +1,3 @@
#![warn(mixed_case_hex_literals)]
#![warn(unseparated_literal_suffix)]
#![warn(zero_prefixed_literal)]
@ -31,4 +29,10 @@ fn main() {
let ok11 = 0o123;
let ok12 = 0b10_1010;
let ok6 = 0xab_abcd;
let ok7 = 0xBAFE_BAFE;
let fail9 = 0xabcdef;
let fail10 = 0xBAFEBAFE;
let fail11 = 0xabcdeff;
}

View file

@ -1,90 +1,110 @@
error: inconsistent casing in hexadecimal literal
--> $DIR/literals.rs:14:17
--> $DIR/literals.rs:12:17
|
14 | let fail1 = 0xabCD;
12 | let fail1 = 0xabCD;
| ^^^^^^
|
= note: `-D mixed-case-hex-literals` implied by `-D warnings`
error: inconsistent casing in hexadecimal literal
--> $DIR/literals.rs:15:17
--> $DIR/literals.rs:13:17
|
15 | let fail2 = 0xabCD_u32;
13 | let fail2 = 0xabCD_u32;
| ^^^^^^^^^^
error: inconsistent casing in hexadecimal literal
--> $DIR/literals.rs:16:17
--> $DIR/literals.rs:14:17
|
16 | let fail2 = 0xabCD_isize;
14 | let fail2 = 0xabCD_isize;
| ^^^^^^^^^^^^
error: integer type suffix should be separated by an underscore
--> $DIR/literals.rs:17:27
--> $DIR/literals.rs:15:27
|
17 | let fail_multi_zero = 000_123usize;
15 | let fail_multi_zero = 000_123usize;
| ^^^^^^^^^^^^
|
= note: `-D unseparated-literal-suffix` implied by `-D warnings`
error: this is a decimal constant
--> $DIR/literals.rs:17:27
--> $DIR/literals.rs:15:27
|
17 | let fail_multi_zero = 000_123usize;
15 | let fail_multi_zero = 000_123usize;
| ^^^^^^^^^^^^
|
= note: `-D zero-prefixed-literal` implied by `-D warnings`
help: if you mean to use a decimal constant, remove the `0` to remove confusion
|
17 | let fail_multi_zero = 123usize;
15 | let fail_multi_zero = 123usize;
| ^^^^^^^^
help: if you mean to use an octal constant, use `0o`
|
17 | let fail_multi_zero = 0o123usize;
15 | let fail_multi_zero = 0o123usize;
| ^^^^^^^^^^
error: integer type suffix should be separated by an underscore
--> $DIR/literals.rs:20:17
|
20 | let fail3 = 1234i32;
| ^^^^^^^
error: integer type suffix should be separated by an underscore
--> $DIR/literals.rs:21:17
|
21 | let fail4 = 1234u32;
| ^^^^^^^
error: integer type suffix should be separated by an underscore
--> $DIR/literals.rs:22:17
|
22 | let fail3 = 1234i32;
| ^^^^^^^
22 | let fail5 = 1234isize;
| ^^^^^^^^^
error: integer type suffix should be separated by an underscore
--> $DIR/literals.rs:23:17
|
23 | let fail4 = 1234u32;
| ^^^^^^^
error: integer type suffix should be separated by an underscore
--> $DIR/literals.rs:24:17
|
24 | let fail5 = 1234isize;
| ^^^^^^^^^
error: integer type suffix should be separated by an underscore
--> $DIR/literals.rs:25:17
|
25 | let fail6 = 1234usize;
23 | let fail6 = 1234usize;
| ^^^^^^^^^
error: float type suffix should be separated by an underscore
--> $DIR/literals.rs:26:17
--> $DIR/literals.rs:24:17
|
26 | let fail7 = 1.5f32;
24 | let fail7 = 1.5f32;
| ^^^^^^
error: this is a decimal constant
--> $DIR/literals.rs:30:17
--> $DIR/literals.rs:28:17
|
30 | let fail8 = 0123;
28 | let fail8 = 0123;
| ^^^^
help: if you mean to use a decimal constant, remove the `0` to remove confusion
|
30 | let fail8 = 123;
28 | let fail8 = 123;
| ^^^
help: if you mean to use an octal constant, use `0o`
|
30 | let fail8 = 0o123;
28 | let fail8 = 0o123;
| ^^^^^
error: aborting due to 11 previous errors
error: long literal lacking separators
--> $DIR/literals.rs:35:17
|
35 | let fail9 = 0xabcdef;
| ^^^^^^^^ help: consider: `0x00ab_cdef`
|
= note: `-D unreadable-literal` implied by `-D warnings`
error: long literal lacking separators
--> $DIR/literals.rs:36:18
|
36 | let fail10 = 0xBAFEBAFE;
| ^^^^^^^^^^ help: consider: `0xBAFE_BAFE`
error: long literal lacking separators
--> $DIR/literals.rs:37:18
|
37 | let fail11 = 0xabcdeff;
| ^^^^^^^^^ help: consider: `0x0abc_deff`
error: aborting due to 14 previous errors