about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJamie McClymont <jamie@kwiius.com>2019-08-17 17:10:10 +1200
committerJamie McClymont <jamie@kwiius.com>2019-08-19 02:46:12 +1200
commitc8fb62148e1339a8361c297d1166beae802a85bc (patch)
tree22ab193a446db689a7a98a1402baca94381eb580
parente92c48989f1c8a3356cde3829c88408da9d8c983 (diff)
downloadrust-c8fb62148e1339a8361c297d1166beae802a85bc.tar.gz
rust-c8fb62148e1339a8361c297d1166beae802a85bc.zip
Add autofixable suggestion for unseparated integer literal suffices
-rw-r--r--clippy_lints/src/misc_early.rs36
-rw-r--r--tests/ui/literals.stderr12
2 files changed, 36 insertions, 12 deletions
diff --git a/clippy_lints/src/misc_early.rs b/clippy_lints/src/misc_early.rs
index b998d3f8a33..aabd5ace331 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 22692160d73..c140bd88e81 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