Skip to content

Commit

Permalink
Add autofixable suggestion for unseparated integer literal suffices
Browse files Browse the repository at this point in the history
  • Loading branch information
JJJollyjim committed Aug 17, 2019
1 parent 49dff2c commit ae6d1c8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
36 changes: 30 additions & 6 deletions clippy_lints/src/misc_early.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,11 +396,23 @@ impl MiscEarlyLints {
if char::to_digit(firstch, 10).is_some();
then {
let mut prev = '\0';
for ch in src.chars() {
for (idx, ch) in src.chars().enumerate() {
if ch == 'i' || ch == 'u' {
if prev != '_' {
span_lint(cx, UNSEPARATED_LITERAL_SUFFIX, lit.span,
"integer type suffix should be separated by an underscore");
span_lint_and_then(
cx,
UNSEPARATED_LITERAL_SUFFIX,
lit.span,
"integer type suffix should be separated by an underscore",
|db| {
db.span_suggestion(
lit.span,
"add an underscore",
format!("{}_{}", &src[0..idx], &src[idx..]),
Applicability::MachineApplicable,
);
},
);
}
break;
}
Expand Down Expand Up @@ -451,11 +463,23 @@ impl MiscEarlyLints {
if char::to_digit(firstch, 10).is_some();
then {
let mut prev = '\0';
for ch in src.chars() {
for (idx, ch) in src.chars().enumerate() {
if ch == 'f' {
if prev != '_' {
span_lint(cx, UNSEPARATED_LITERAL_SUFFIX, lit.span,
"float type suffix should be separated by an underscore");
span_lint_and_then(
cx,
UNSEPARATED_LITERAL_SUFFIX,
lit.span,
"float type suffix should be separated by an underscore",
|db| {
db.span_suggestion(
lit.span,
"add an underscore",
format!("{}_{}", &src[0..idx], &src[idx..]),
Applicability::MachineApplicable,
);
},
);
}
break;
}
Expand Down
12 changes: 6 additions & 6 deletions tests/ui/literals.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ error: integer type suffix should be separated by an underscore
--> $DIR/literals.rs:16:27
|
LL | let fail_multi_zero = 000_123usize;
| ^^^^^^^^^^^^
| ^^^^^^^^^^^^ help: add an underscore: `000_123_usize`
|
= note: `-D clippy::unseparated-literal-suffix` implied by `-D warnings`

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

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

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

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

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

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

0 comments on commit ae6d1c8

Please sign in to comment.