Skip to content

Commit 835205b

Browse files
committed
Auto merge of rust-lang#4401 - JJJollyjim:literal-separation-suggestion, r=flip1995
Add autofixable suggestion for unseparated integer literal suffixes changelog: Add autofixable suggestion for unseparated integer literal suffixes Somewhat WIP, since I haven't been able to get this working when adding `// run-rustfix` to `ui/literals.rs`. I think the issue is that there are multiple suggestions operating on one numerical literal, and I'm not sure what the best approach is to work around that. Thanks
2 parents cd3df6b + 370433f commit 835205b

File tree

6 files changed

+116
-61
lines changed

6 files changed

+116
-61
lines changed

clippy_lints/src/misc_early.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use crate::utils::{constants, snippet, snippet_opt, span_help_and_lint, span_lint, span_lint_and_then};
1+
use crate::utils::{
2+
constants, snippet, snippet_opt, span_help_and_lint, span_lint, span_lint_and_sugg, span_lint_and_then,
3+
};
24
use if_chain::if_chain;
35
use rustc::lint::{in_external_macro, EarlyContext, EarlyLintPass, LintArray, LintContext, LintPass};
46
use rustc::{declare_lint_pass, declare_tool_lint};
@@ -396,11 +398,18 @@ impl MiscEarlyLints {
396398
if char::to_digit(firstch, 10).is_some();
397399
then {
398400
let mut prev = '\0';
399-
for ch in src.chars() {
401+
for (idx, ch) in src.chars().enumerate() {
400402
if ch == 'i' || ch == 'u' {
401403
if prev != '_' {
402-
span_lint(cx, UNSEPARATED_LITERAL_SUFFIX, lit.span,
403-
"integer type suffix should be separated by an underscore");
404+
span_lint_and_sugg(
405+
cx,
406+
UNSEPARATED_LITERAL_SUFFIX,
407+
lit.span,
408+
"integer type suffix should be separated by an underscore",
409+
"add an underscore",
410+
format!("{}_{}", &src[0..idx], &src[idx..]),
411+
Applicability::MachineApplicable,
412+
);
404413
}
405414
break;
406415
}
@@ -451,11 +460,18 @@ impl MiscEarlyLints {
451460
if char::to_digit(firstch, 10).is_some();
452461
then {
453462
let mut prev = '\0';
454-
for ch in src.chars() {
463+
for (idx, ch) in src.chars().enumerate() {
455464
if ch == 'f' {
456465
if prev != '_' {
457-
span_lint(cx, UNSEPARATED_LITERAL_SUFFIX, lit.span,
458-
"float type suffix should be separated by an underscore");
466+
span_lint_and_sugg(
467+
cx,
468+
UNSEPARATED_LITERAL_SUFFIX,
469+
lit.span,
470+
"float type suffix should be separated by an underscore",
471+
"add an underscore",
472+
format!("{}_{}", &src[0..idx], &src[idx..]),
473+
Applicability::MachineApplicable,
474+
);
459475
}
460476
break;
461477
}

tests/ui/literals.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![warn(clippy::large_digit_groups)]
22
#![warn(clippy::mixed_case_hex_literals)]
3-
#![warn(clippy::unseparated_literal_suffix)]
43
#![warn(clippy::zero_prefixed_literal)]
4+
#![allow(clippy::unseparated_literal_suffix)]
55
#![allow(dead_code)]
66

77
fn main() {
@@ -15,15 +15,6 @@ fn main() {
1515
let fail2 = 0xabCD_isize;
1616
let fail_multi_zero = 000_123usize;
1717

18-
let ok6 = 1234_i32;
19-
let ok7 = 1234_f32;
20-
let ok8 = 1234_isize;
21-
let fail3 = 1234i32;
22-
let fail4 = 1234u32;
23-
let fail5 = 1234isize;
24-
let fail6 = 1234usize;
25-
let fail7 = 1.5f32;
26-
2718
let ok9 = 0;
2819
let ok10 = 0_i64;
2920
let fail8 = 0123;

tests/ui/literals.stderr

Lines changed: 6 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,6 @@ error: inconsistent casing in hexadecimal literal
1818
LL | let fail2 = 0xabCD_isize;
1919
| ^^^^^^^^^^^^
2020

21-
error: integer type suffix should be separated by an underscore
22-
--> $DIR/literals.rs:16:27
23-
|
24-
LL | let fail_multi_zero = 000_123usize;
25-
| ^^^^^^^^^^^^
26-
|
27-
= note: `-D clippy::unseparated-literal-suffix` implied by `-D warnings`
28-
2921
error: this is a decimal constant
3022
--> $DIR/literals.rs:16:27
3123
|
@@ -42,38 +34,8 @@ help: if you mean to use an octal constant, use `0o`
4234
LL | let fail_multi_zero = 0o123usize;
4335
| ^^^^^^^^^^
4436

45-
error: integer type suffix should be separated by an underscore
46-
--> $DIR/literals.rs:21:17
47-
|
48-
LL | let fail3 = 1234i32;
49-
| ^^^^^^^
50-
51-
error: integer type suffix should be separated by an underscore
52-
--> $DIR/literals.rs:22:17
53-
|
54-
LL | let fail4 = 1234u32;
55-
| ^^^^^^^
56-
57-
error: integer type suffix should be separated by an underscore
58-
--> $DIR/literals.rs:23:17
59-
|
60-
LL | let fail5 = 1234isize;
61-
| ^^^^^^^^^
62-
63-
error: integer type suffix should be separated by an underscore
64-
--> $DIR/literals.rs:24:17
65-
|
66-
LL | let fail6 = 1234usize;
67-
| ^^^^^^^^^
68-
69-
error: float type suffix should be separated by an underscore
70-
--> $DIR/literals.rs:25:17
71-
|
72-
LL | let fail7 = 1.5f32;
73-
| ^^^^^^
74-
7537
error: this is a decimal constant
76-
--> $DIR/literals.rs:29:17
38+
--> $DIR/literals.rs:20:17
7739
|
7840
LL | let fail8 = 0123;
7941
| ^^^^
@@ -87,32 +49,32 @@ LL | let fail8 = 0o123;
8749
| ^^^^^
8850

8951
error: digit groups should be smaller
90-
--> $DIR/literals.rs:40:18
52+
--> $DIR/literals.rs:31:18
9153
|
9254
LL | let fail13 = 0x1_23456_78901_usize;
9355
| ^^^^^^^^^^^^^^^^^^^^^ help: consider: `0x0123_4567_8901_usize`
9456
|
9557
= note: `-D clippy::large-digit-groups` implied by `-D warnings`
9658

9759
error: digits grouped inconsistently by underscores
98-
--> $DIR/literals.rs:42:18
60+
--> $DIR/literals.rs:33:18
9961
|
10062
LL | let fail19 = 12_3456_21;
10163
| ^^^^^^^^^^ help: consider: `12_345_621`
10264
|
10365
= note: `-D clippy::inconsistent-digit-grouping` implied by `-D warnings`
10466

10567
error: digits grouped inconsistently by underscores
106-
--> $DIR/literals.rs:43:18
68+
--> $DIR/literals.rs:34:18
10769
|
10870
LL | let fail22 = 3__4___23;
10971
| ^^^^^^^^^ help: consider: `3_423`
11072

11173
error: digits grouped inconsistently by underscores
112-
--> $DIR/literals.rs:44:18
74+
--> $DIR/literals.rs:35:18
11375
|
11476
LL | let fail23 = 3__16___23;
11577
| ^^^^^^^^^^ help: consider: `31_623`
11678

117-
error: aborting due to 15 previous errors
79+
error: aborting due to 9 previous errors
11880

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// run-rustfix
2+
3+
#![warn(clippy::unseparated_literal_suffix)]
4+
#![allow(dead_code)]
5+
6+
fn main() {
7+
let _ok1 = 1234_i32;
8+
let _ok2 = 1234_isize;
9+
let _ok3 = 0x123_isize;
10+
let _fail1 = 1234_i32;
11+
let _fail2 = 1234_u32;
12+
let _fail3 = 1234_isize;
13+
let _fail4 = 1234_usize;
14+
let _fail5 = 0x123_isize;
15+
16+
let _okf1 = 1.5_f32;
17+
let _okf2 = 1_f32;
18+
let _failf1 = 1.5_f32;
19+
let _failf2 = 1_f32;
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// run-rustfix
2+
3+
#![warn(clippy::unseparated_literal_suffix)]
4+
#![allow(dead_code)]
5+
6+
fn main() {
7+
let _ok1 = 1234_i32;
8+
let _ok2 = 1234_isize;
9+
let _ok3 = 0x123_isize;
10+
let _fail1 = 1234i32;
11+
let _fail2 = 1234u32;
12+
let _fail3 = 1234isize;
13+
let _fail4 = 1234usize;
14+
let _fail5 = 0x123isize;
15+
16+
let _okf1 = 1.5_f32;
17+
let _okf2 = 1_f32;
18+
let _failf1 = 1.5f32;
19+
let _failf2 = 1f32;
20+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
error: integer type suffix should be separated by an underscore
2+
--> $DIR/unseparated_prefix_literals.rs:10:18
3+
|
4+
LL | let _fail1 = 1234i32;
5+
| ^^^^^^^ help: add an underscore: `1234_i32`
6+
|
7+
= note: `-D clippy::unseparated-literal-suffix` implied by `-D warnings`
8+
9+
error: integer type suffix should be separated by an underscore
10+
--> $DIR/unseparated_prefix_literals.rs:11:18
11+
|
12+
LL | let _fail2 = 1234u32;
13+
| ^^^^^^^ help: add an underscore: `1234_u32`
14+
15+
error: integer type suffix should be separated by an underscore
16+
--> $DIR/unseparated_prefix_literals.rs:12:18
17+
|
18+
LL | let _fail3 = 1234isize;
19+
| ^^^^^^^^^ help: add an underscore: `1234_isize`
20+
21+
error: integer type suffix should be separated by an underscore
22+
--> $DIR/unseparated_prefix_literals.rs:13:18
23+
|
24+
LL | let _fail4 = 1234usize;
25+
| ^^^^^^^^^ help: add an underscore: `1234_usize`
26+
27+
error: integer type suffix should be separated by an underscore
28+
--> $DIR/unseparated_prefix_literals.rs:14:18
29+
|
30+
LL | let _fail5 = 0x123isize;
31+
| ^^^^^^^^^^ help: add an underscore: `0x123_isize`
32+
33+
error: float type suffix should be separated by an underscore
34+
--> $DIR/unseparated_prefix_literals.rs:18:19
35+
|
36+
LL | let _failf1 = 1.5f32;
37+
| ^^^^^^ help: add an underscore: `1.5_f32`
38+
39+
error: float type suffix should be separated by an underscore
40+
--> $DIR/unseparated_prefix_literals.rs:19:19
41+
|
42+
LL | let _failf2 = 1f32;
43+
| ^^^^ help: add an underscore: `1_f32`
44+
45+
error: aborting due to 7 previous errors
46+

0 commit comments

Comments
 (0)