about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--clippy_lints/src/default_numeric_fallback.rs19
-rw-r--r--tests/ui/default_numeric_fallback.stderr95
2 files changed, 38 insertions, 76 deletions
diff --git a/clippy_lints/src/default_numeric_fallback.rs b/clippy_lints/src/default_numeric_fallback.rs
index 33a577f336d..6ace9aa6bdf 100644
--- a/clippy_lints/src/default_numeric_fallback.rs
+++ b/clippy_lints/src/default_numeric_fallback.rs
@@ -1,4 +1,5 @@
 use rustc_ast::ast::{LitFloatType, LitIntType, LitKind};
+use rustc_errors::Applicability;
 use rustc_hir::{
     intravisit::{walk_expr, walk_stmt, NestedVisitorMap, Visitor},
     Body, Expr, ExprKind, HirId, Lit, Stmt, StmtKind,
@@ -12,7 +13,7 @@ use rustc_session::{declare_lint_pass, declare_tool_lint};
 
 use if_chain::if_chain;
 
-use crate::utils::span_lint_and_help;
+use crate::utils::{snippet, span_lint_and_sugg};
 
 declare_clippy_lint! {
     /// **What it does:** Checks for usage of unconstrained numeric literals which may cause default numeric fallback in type
@@ -75,16 +76,24 @@ impl<'a, 'tcx> NumericFallbackVisitor<'a, 'tcx> {
                 if let Some(ty_bound) = self.ty_bounds.last();
                 if matches!(lit.node,
                             LitKind::Int(_, LitIntType::Unsuffixed) | LitKind::Float(_, LitFloatType::Unsuffixed));
-                if matches!(lit_ty.kind(), ty::Int(IntTy::I32) | ty::Float(FloatTy::F64));
                 if !ty_bound.is_integral();
                 then {
-                    span_lint_and_help(
+                    let suffix = match lit_ty.kind() {
+                        ty::Int(IntTy::I32) => "i32",
+                        ty::Float(FloatTy::F64) => "f64",
+                        // Default numeric fallback never results in other types.
+                        _ => return,
+                    };
+
+                    let sugg = format!("{}_{}", snippet(self.cx, lit.span, ""), suffix);
+                    span_lint_and_sugg(
                         self.cx,
                         DEFAULT_NUMERIC_FALLBACK,
                         lit.span,
                         "default numeric fallback might occur",
-                        None,
-                        "consider adding suffix to avoid default numeric fallback",
+                        "consider adding suffix",
+                        sugg,
+                        Applicability::MaybeIncorrect,
                     );
                 }
         }
diff --git a/tests/ui/default_numeric_fallback.stderr b/tests/ui/default_numeric_fallback.stderr
index 50fcdfb510e..b31aa4ebcf8 100644
--- a/tests/ui/default_numeric_fallback.stderr
+++ b/tests/ui/default_numeric_fallback.stderr
@@ -2,194 +2,147 @@ error: default numeric fallback might occur
   --> $DIR/default_numeric_fallback.rs:10:17
    |
 LL |         let x = 22;
-   |                 ^^
+   |                 ^^ help: consider adding suffix: `22_i32`
    |
    = note: `-D clippy::default-numeric-fallback` implied by `-D warnings`
-   = help: consider adding suffix to avoid default numeric fallback
 
 error: default numeric fallback might occur
   --> $DIR/default_numeric_fallback.rs:11:18
    |
 LL |         let x = [1, 2, 3];
-   |                  ^
-   |
-   = help: consider adding suffix to avoid default numeric fallback
+   |                  ^ help: consider adding suffix: `1_i32`
 
 error: default numeric fallback might occur
   --> $DIR/default_numeric_fallback.rs:11:21
    |
 LL |         let x = [1, 2, 3];
-   |                     ^
-   |
-   = help: consider adding suffix to avoid default numeric fallback
+   |                     ^ help: consider adding suffix: `2_i32`
 
 error: default numeric fallback might occur
   --> $DIR/default_numeric_fallback.rs:11:24
    |
 LL |         let x = [1, 2, 3];
-   |                        ^
-   |
-   = help: consider adding suffix to avoid default numeric fallback
+   |                        ^ help: consider adding suffix: `3_i32`
 
 error: default numeric fallback might occur
   --> $DIR/default_numeric_fallback.rs:12:28
    |
 LL |         let x = if true { (1, 2) } else { (3, 4) };
-   |                            ^
-   |
-   = help: consider adding suffix to avoid default numeric fallback
+   |                            ^ help: consider adding suffix: `1_i32`
 
 error: default numeric fallback might occur
   --> $DIR/default_numeric_fallback.rs:12:31
    |
 LL |         let x = if true { (1, 2) } else { (3, 4) };
-   |                               ^
-   |
-   = help: consider adding suffix to avoid default numeric fallback
+   |                               ^ help: consider adding suffix: `2_i32`
 
 error: default numeric fallback might occur
   --> $DIR/default_numeric_fallback.rs:12:44
    |
 LL |         let x = if true { (1, 2) } else { (3, 4) };
-   |                                            ^
-   |
-   = help: consider adding suffix to avoid default numeric fallback
+   |                                            ^ help: consider adding suffix: `3_i32`
 
 error: default numeric fallback might occur
   --> $DIR/default_numeric_fallback.rs:12:47
    |
 LL |         let x = if true { (1, 2) } else { (3, 4) };
-   |                                               ^
-   |
-   = help: consider adding suffix to avoid default numeric fallback
+   |                                               ^ help: consider adding suffix: `4_i32`
 
 error: default numeric fallback might occur
   --> $DIR/default_numeric_fallback.rs:13:23
    |
 LL |         let x = match 1 {
-   |                       ^
-   |
-   = help: consider adding suffix to avoid default numeric fallback
+   |                       ^ help: consider adding suffix: `1_i32`
 
 error: default numeric fallback might occur
   --> $DIR/default_numeric_fallback.rs:14:13
    |
 LL |             1 => 1,
-   |             ^
-   |
-   = help: consider adding suffix to avoid default numeric fallback
+   |             ^ help: consider adding suffix: `1_i32`
 
 error: default numeric fallback might occur
   --> $DIR/default_numeric_fallback.rs:14:18
    |
 LL |             1 => 1,
-   |                  ^
-   |
-   = help: consider adding suffix to avoid default numeric fallback
+   |                  ^ help: consider adding suffix: `1_i32`
 
 error: default numeric fallback might occur
   --> $DIR/default_numeric_fallback.rs:15:18
    |
 LL |             _ => 2,
-   |                  ^
-   |
-   = help: consider adding suffix to avoid default numeric fallback
+   |                  ^ help: consider adding suffix: `2_i32`
 
 error: default numeric fallback might occur
   --> $DIR/default_numeric_fallback.rs:19:17
    |
 LL |         let x = 0.12;
-   |                 ^^^^
-   |
-   = help: consider adding suffix to avoid default numeric fallback
+   |                 ^^^^ help: consider adding suffix: `0.12_f64`
 
 error: default numeric fallback might occur
   --> $DIR/default_numeric_fallback.rs:37:21
    |
 LL |             let y = 1;
-   |                     ^
-   |
-   = help: consider adding suffix to avoid default numeric fallback
+   |                     ^ help: consider adding suffix: `1_i32`
 
 error: default numeric fallback might occur
   --> $DIR/default_numeric_fallback.rs:45:21
    |
 LL |             let y = 1;
-   |                     ^
-   |
-   = help: consider adding suffix to avoid default numeric fallback
+   |                     ^ help: consider adding suffix: `1_i32`
 
 error: default numeric fallback might occur
   --> $DIR/default_numeric_fallback.rs:51:21
    |
 LL |             let y = 1;
-   |                     ^
-   |
-   = help: consider adding suffix to avoid default numeric fallback
+   |                     ^ help: consider adding suffix: `1_i32`
 
 error: default numeric fallback might occur
   --> $DIR/default_numeric_fallback.rs:63:9
    |
 LL |         1
-   |         ^
-   |
-   = help: consider adding suffix to avoid default numeric fallback
+   |         ^ help: consider adding suffix: `1_i32`
 
 error: default numeric fallback might occur
   --> $DIR/default_numeric_fallback.rs:69:27
    |
 LL |         let f = || -> _ { 1 };
-   |                           ^
-   |
-   = help: consider adding suffix to avoid default numeric fallback
+   |                           ^ help: consider adding suffix: `1_i32`
 
 error: default numeric fallback might occur
   --> $DIR/default_numeric_fallback.rs:73:29
    |
 LL |         let f = || -> i32 { 1 };
-   |                             ^
-   |
-   = help: consider adding suffix to avoid default numeric fallback
+   |                             ^ help: consider adding suffix: `1_i32`
 
 error: default numeric fallback might occur
   --> $DIR/default_numeric_fallback.rs:87:21
    |
 LL |         generic_arg(1);
-   |                     ^
-   |
-   = help: consider adding suffix to avoid default numeric fallback
+   |                     ^ help: consider adding suffix: `1_i32`
 
 error: default numeric fallback might occur
   --> $DIR/default_numeric_fallback.rs:90:32
    |
 LL |         let x: _ = generic_arg(1);
-   |                                ^
-   |
-   = help: consider adding suffix to avoid default numeric fallback
+   |                                ^ help: consider adding suffix: `1_i32`
 
 error: default numeric fallback might occur
   --> $DIR/default_numeric_fallback.rs:108:28
    |
 LL |         GenericStruct { x: 1 };
-   |                            ^
-   |
-   = help: consider adding suffix to avoid default numeric fallback
+   |                            ^ help: consider adding suffix: `1_i32`
 
 error: default numeric fallback might occur
   --> $DIR/default_numeric_fallback.rs:111:36
    |
 LL |         let _ = GenericStruct { x: 1 };
-   |                                    ^
-   |
-   = help: consider adding suffix to avoid default numeric fallback
+   |                                    ^ help: consider adding suffix: `1_i32`
 
 error: default numeric fallback might occur
   --> $DIR/default_numeric_fallback.rs:131:23
    |
 LL |         s.generic_arg(1);
-   |                       ^
-   |
-   = help: consider adding suffix to avoid default numeric fallback
+   |                       ^ help: consider adding suffix: `1_i32`
 
 error: aborting due to 24 previous errors