Skip to content

Commit c8fb621

Browse files
committed
Add autofixable suggestion for unseparated integer literal suffices
1 parent e92c489 commit c8fb621

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

clippy_lints/src/misc_early.rs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -396,11 +396,23 @@ impl MiscEarlyLints {
396396
if char::to_digit(firstch, 10).is_some();
397397
then {
398398
let mut prev = '\0';
399-
for ch in src.chars() {
399+
for (idx, ch) in src.chars().enumerate() {
400400
if ch == 'i' || ch == 'u' {
401401
if prev != '_' {
402-
span_lint(cx, UNSEPARATED_LITERAL_SUFFIX, lit.span,
403-
"integer type suffix should be separated by an underscore");
402+
span_lint_and_then(
403+
cx,
404+
UNSEPARATED_LITERAL_SUFFIX,
405+
lit.span,
406+
"integer type suffix should be separated by an underscore",
407+
|db| {
408+
db.span_suggestion(
409+
lit.span,
410+
"add an underscore",
411+
format!("{}_{}", &src[0..idx], &src[idx..]),
412+
Applicability::MachineApplicable,
413+
);
414+
},
415+
);
404416
}
405417
break;
406418
}
@@ -451,11 +463,23 @@ impl MiscEarlyLints {
451463
if char::to_digit(firstch, 10).is_some();
452464
then {
453465
let mut prev = '\0';
454-
for ch in src.chars() {
466+
for (idx, ch) in src.chars().enumerate() {
455467
if ch == 'f' {
456468
if prev != '_' {
457-
span_lint(cx, UNSEPARATED_LITERAL_SUFFIX, lit.span,
458-
"float type suffix should be separated by an underscore");
469+
span_lint_and_then(
470+
cx,
471+
UNSEPARATED_LITERAL_SUFFIX,
472+
lit.span,
473+
"float type suffix should be separated by an underscore",
474+
|db| {
475+
db.span_suggestion(
476+
lit.span,
477+
"add an underscore",
478+
format!("{}_{}", &src[0..idx], &src[idx..]),
479+
Applicability::MachineApplicable,
480+
);
481+
},
482+
);
459483
}
460484
break;
461485
}

tests/ui/literals.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ error: integer type suffix should be separated by an underscore
2222
--> $DIR/literals.rs:16:27
2323
|
2424
LL | let fail_multi_zero = 000_123usize;
25-
| ^^^^^^^^^^^^
25+
| ^^^^^^^^^^^^ help: add an underscore: `000_123_usize`
2626
|
2727
= note: `-D clippy::unseparated-literal-suffix` implied by `-D warnings`
2828

@@ -46,31 +46,31 @@ error: integer type suffix should be separated by an underscore
4646
--> $DIR/literals.rs:21:17
4747
|
4848
LL | let fail3 = 1234i32;
49-
| ^^^^^^^
49+
| ^^^^^^^ help: add an underscore: `1234_i32`
5050

5151
error: integer type suffix should be separated by an underscore
5252
--> $DIR/literals.rs:22:17
5353
|
5454
LL | let fail4 = 1234u32;
55-
| ^^^^^^^
55+
| ^^^^^^^ help: add an underscore: `1234_u32`
5656

5757
error: integer type suffix should be separated by an underscore
5858
--> $DIR/literals.rs:23:17
5959
|
6060
LL | let fail5 = 1234isize;
61-
| ^^^^^^^^^
61+
| ^^^^^^^^^ help: add an underscore: `1234_isize`
6262

6363
error: integer type suffix should be separated by an underscore
6464
--> $DIR/literals.rs:24:17
6565
|
6666
LL | let fail6 = 1234usize;
67-
| ^^^^^^^^^
67+
| ^^^^^^^^^ help: add an underscore: `1234_usize`
6868

6969
error: float type suffix should be separated by an underscore
7070
--> $DIR/literals.rs:25:17
7171
|
7272
LL | let fail7 = 1.5f32;
73-
| ^^^^^^
73+
| ^^^^^^ help: add an underscore: `1.5_f32`
7474

7575
error: this is a decimal constant
7676
--> $DIR/literals.rs:29:17

0 commit comments

Comments
 (0)