Skip to content

Commit 388e6b7

Browse files
committed
Auto merge of rust-lang#8742 - goth-turtle:mistyped-literal-suffix, r=llogiq
mistyped_literal_suffix: improve integer suggestions, avoid wrong float suggestions This PR fixes 2 things: - The known problem that integer types are always suggested as signed, by suggesting an unsigned suffix for literals that wouldnt fit in the signed type, and ignores any literals too big for the corresponding unsigned type too. - The lint would only look at the integer part of any floating point literals without an exponent, this causing rust-lang#6129. This just ignores those literals. Examples: ```rust let _ = 2_32; // still 2_i32 let _ = 234_8; // would now suggest 234_u8 // these are now ignored let _ = 500_8; let _ = 123_32.123; ``` changelog: suggest correct integer types in [`mistyped_literal_suffix`], ignore float literals without an exponent fixes rust-lang#6129
2 parents 6aa3684 + b4a50e9 commit 388e6b7

File tree

4 files changed

+109
-27
lines changed

4 files changed

+109
-27
lines changed

clippy_lints/src/literal_representation.rs

+37-13
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,12 @@ declare_clippy_lint! {
4242
/// This is most probably a typo
4343
///
4444
/// ### Known problems
45-
/// - Recommends a signed suffix, even though the number might be too big and an unsigned
46-
/// suffix is required
45+
/// - Does not match on integers too large to fit in the corresponding unsigned type
4746
/// - Does not match on `_127` since that is a valid grouping for decimal and octal numbers
4847
///
4948
/// ### Example
50-
/// ```rust
51-
/// // Probably mistyped
52-
/// 2_32;
53-
///
54-
/// // Good
55-
/// 2_i32;
49+
/// `2_32` => `2_i32`
50+
/// `250_8 => `250_u8`
5651
/// ```
5752
#[clippy::version = "1.30.0"]
5853
pub MISTYPED_LITERAL_SUFFIXES,
@@ -310,18 +305,47 @@ impl LiteralDigitGrouping {
310305
return true;
311306
}
312307

313-
let (part, mistyped_suffixes, missing_char) = if let Some((_, exponent)) = &mut num_lit.exponent {
314-
(exponent, &["32", "64"][..], 'f')
308+
let (part, mistyped_suffixes, is_float) = if let Some((_, exponent)) = &mut num_lit.exponent {
309+
(exponent, &["32", "64"][..], true)
315310
} else if num_lit.fraction.is_some() {
316-
(&mut num_lit.integer, &["32", "64"][..], 'f')
311+
return true;
317312
} else {
318-
(&mut num_lit.integer, &["8", "16", "32", "64"][..], 'i')
313+
(&mut num_lit.integer, &["8", "16", "32", "64"][..], false)
319314
};
320315

321316
let mut split = part.rsplit('_');
322317
let last_group = split.next().expect("At least one group");
323318
if split.next().is_some() && mistyped_suffixes.contains(&last_group) {
324-
*part = &part[..part.len() - last_group.len()];
319+
let main_part = &part[..part.len() - last_group.len()];
320+
let missing_char;
321+
if is_float {
322+
missing_char = 'f';
323+
} else {
324+
let radix = match num_lit.radix {
325+
Radix::Binary => 2,
326+
Radix::Octal => 8,
327+
Radix::Decimal => 10,
328+
Radix::Hexadecimal => 16,
329+
};
330+
if let Ok(int) = u64::from_str_radix(&main_part.replace('_', ""), radix) {
331+
missing_char = match (last_group, int) {
332+
("8", i) if i8::try_from(i).is_ok() => 'i',
333+
("16", i) if i16::try_from(i).is_ok() => 'i',
334+
("32", i) if i32::try_from(i).is_ok() => 'i',
335+
("64", i) if i64::try_from(i).is_ok() => 'i',
336+
("8", u) if u8::try_from(u).is_ok() => 'u',
337+
("16", u) if u16::try_from(u).is_ok() => 'u',
338+
("32", u) if u32::try_from(u).is_ok() => 'u',
339+
("64", _) => 'u',
340+
_ => {
341+
return true;
342+
},
343+
}
344+
} else {
345+
return true;
346+
}
347+
}
348+
*part = main_part;
325349
let mut sugg = num_lit.format();
326350
sugg.push('_');
327351
sugg.push(missing_char);

tests/ui/mistyped_literal_suffix.fixed

+15-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
unused_variables,
66
overflowing_literals,
77
clippy::excessive_precision,
8-
clippy::inconsistent_digit_grouping
8+
clippy::inconsistent_digit_grouping,
9+
clippy::unusual_byte_groupings
910
)]
1011

1112
fn main() {
@@ -25,5 +26,18 @@ fn main() {
2526
let fail28 = 241_251_235E723_f64;
2627
let ok29 = 42279.911_32;
2728

29+
// testing that the suggestion actually fits in its type
30+
let fail30 = 127_i8; // should be i8
31+
let fail31 = 240_u8; // should be u8
32+
let ok32 = 360_8; // doesnt fit in either, should be ignored
33+
let fail33 = 0x1234_i16;
34+
let fail34 = 0xABCD_u16;
35+
let ok35 = 0x12345_16;
36+
let fail36 = 0xFFFF_FFFF_FFFF_FFFF_u64; // u64
37+
38+
// issue #6129
39+
let ok37 = 123_32.123;
40+
let ok38 = 124_64.0;
41+
2842
let _ = 1.123_45E1_f32;
2943
}

tests/ui/mistyped_literal_suffix.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
unused_variables,
66
overflowing_literals,
77
clippy::excessive_precision,
8-
clippy::inconsistent_digit_grouping
8+
clippy::inconsistent_digit_grouping,
9+
clippy::unusual_byte_groupings
910
)]
1011

1112
fn main() {
@@ -25,5 +26,18 @@ fn main() {
2526
let fail28 = 241251235E723_64;
2627
let ok29 = 42279.911_32;
2728

29+
// testing that the suggestion actually fits in its type
30+
let fail30 = 127_8; // should be i8
31+
let fail31 = 240_8; // should be u8
32+
let ok32 = 360_8; // doesnt fit in either, should be ignored
33+
let fail33 = 0x1234_16;
34+
let fail34 = 0xABCD_16;
35+
let ok35 = 0x12345_16;
36+
let fail36 = 0xFFFF_FFFF_FFFF_FFFF_64; // u64
37+
38+
// issue #6129
39+
let ok37 = 123_32.123;
40+
let ok38 = 124_64.0;
41+
2842
let _ = 1.12345E1_32;
2943
}
+42-12
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,100 @@
11
error: mistyped literal suffix
2-
--> $DIR/mistyped_literal_suffix.rs:12:18
2+
--> $DIR/mistyped_literal_suffix.rs:13:18
33
|
44
LL | let fail14 = 2_32;
55
| ^^^^ help: did you mean to write: `2_i32`
66
|
77
= note: `#[deny(clippy::mistyped_literal_suffixes)]` on by default
88

99
error: mistyped literal suffix
10-
--> $DIR/mistyped_literal_suffix.rs:13:18
10+
--> $DIR/mistyped_literal_suffix.rs:14:18
1111
|
1212
LL | let fail15 = 4_64;
1313
| ^^^^ help: did you mean to write: `4_i64`
1414

1515
error: mistyped literal suffix
16-
--> $DIR/mistyped_literal_suffix.rs:14:18
16+
--> $DIR/mistyped_literal_suffix.rs:15:18
1717
|
1818
LL | let fail16 = 7_8; //
1919
| ^^^ help: did you mean to write: `7_i8`
2020

2121
error: mistyped literal suffix
22-
--> $DIR/mistyped_literal_suffix.rs:15:18
22+
--> $DIR/mistyped_literal_suffix.rs:16:18
2323
|
2424
LL | let fail17 = 23_16; //
2525
| ^^^^^ help: did you mean to write: `23_i16`
2626

2727
error: mistyped literal suffix
28-
--> $DIR/mistyped_literal_suffix.rs:18:18
28+
--> $DIR/mistyped_literal_suffix.rs:19:18
2929
|
3030
LL | let fail20 = 2__8; //
3131
| ^^^^ help: did you mean to write: `2_i8`
3232

3333
error: mistyped literal suffix
34-
--> $DIR/mistyped_literal_suffix.rs:19:18
34+
--> $DIR/mistyped_literal_suffix.rs:20:18
3535
|
3636
LL | let fail21 = 4___16; //
3737
| ^^^^^^ help: did you mean to write: `4_i16`
3838

3939
error: mistyped literal suffix
40-
--> $DIR/mistyped_literal_suffix.rs:22:18
40+
--> $DIR/mistyped_literal_suffix.rs:23:18
4141
|
4242
LL | let fail25 = 1E2_32;
4343
| ^^^^^^ help: did you mean to write: `1E2_f32`
4444

4545
error: mistyped literal suffix
46-
--> $DIR/mistyped_literal_suffix.rs:23:18
46+
--> $DIR/mistyped_literal_suffix.rs:24:18
4747
|
4848
LL | let fail26 = 43E7_64;
4949
| ^^^^^^^ help: did you mean to write: `43E7_f64`
5050

5151
error: mistyped literal suffix
52-
--> $DIR/mistyped_literal_suffix.rs:24:18
52+
--> $DIR/mistyped_literal_suffix.rs:25:18
5353
|
5454
LL | let fail27 = 243E17_32;
5555
| ^^^^^^^^^ help: did you mean to write: `243E17_f32`
5656

5757
error: mistyped literal suffix
58-
--> $DIR/mistyped_literal_suffix.rs:25:18
58+
--> $DIR/mistyped_literal_suffix.rs:26:18
5959
|
6060
LL | let fail28 = 241251235E723_64;
6161
| ^^^^^^^^^^^^^^^^ help: did you mean to write: `241_251_235E723_f64`
6262

6363
error: mistyped literal suffix
64-
--> $DIR/mistyped_literal_suffix.rs:28:13
64+
--> $DIR/mistyped_literal_suffix.rs:30:18
65+
|
66+
LL | let fail30 = 127_8; // should be i8
67+
| ^^^^^ help: did you mean to write: `127_i8`
68+
69+
error: mistyped literal suffix
70+
--> $DIR/mistyped_literal_suffix.rs:31:18
71+
|
72+
LL | let fail31 = 240_8; // should be u8
73+
| ^^^^^ help: did you mean to write: `240_u8`
74+
75+
error: mistyped literal suffix
76+
--> $DIR/mistyped_literal_suffix.rs:33:18
77+
|
78+
LL | let fail33 = 0x1234_16;
79+
| ^^^^^^^^^ help: did you mean to write: `0x1234_i16`
80+
81+
error: mistyped literal suffix
82+
--> $DIR/mistyped_literal_suffix.rs:34:18
83+
|
84+
LL | let fail34 = 0xABCD_16;
85+
| ^^^^^^^^^ help: did you mean to write: `0xABCD_u16`
86+
87+
error: mistyped literal suffix
88+
--> $DIR/mistyped_literal_suffix.rs:36:18
89+
|
90+
LL | let fail36 = 0xFFFF_FFFF_FFFF_FFFF_64; // u64
91+
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: did you mean to write: `0xFFFF_FFFF_FFFF_FFFF_u64`
92+
93+
error: mistyped literal suffix
94+
--> $DIR/mistyped_literal_suffix.rs:42:13
6595
|
6696
LL | let _ = 1.12345E1_32;
6797
| ^^^^^^^^^^^^ help: did you mean to write: `1.123_45E1_f32`
6898

69-
error: aborting due to 11 previous errors
99+
error: aborting due to 16 previous errors
70100

0 commit comments

Comments
 (0)