diff --git a/clippy_lints/src/misc_early.rs b/clippy_lints/src/misc_early.rs index b998d3f8a331..aabd5ace3317 100644 --- a/clippy_lints/src/misc_early.rs +++ b/clippy_lints/src/misc_early.rs @@ -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; } @@ -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; } diff --git a/tests/ui/literals.stderr b/tests/ui/literals.stderr index 22692160d73a..c140bd88e815 100644 --- a/tests/ui/literals.stderr +++ b/tests/ui/literals.stderr @@ -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` @@ -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