about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--clippy_lints/src/let_with_type_underscore.rs15
-rw-r--r--tests/ui/let_with_type_underscore.fixed47
-rw-r--r--tests/ui/let_with_type_underscore.stderr34
3 files changed, 75 insertions, 21 deletions
diff --git a/clippy_lints/src/let_with_type_underscore.rs b/clippy_lints/src/let_with_type_underscore.rs
index 9c8488ff381..1917ca24a05 100644
--- a/clippy_lints/src/let_with_type_underscore.rs
+++ b/clippy_lints/src/let_with_type_underscore.rs
@@ -1,5 +1,6 @@
-use clippy_utils::diagnostics::span_lint_and_help;
+use clippy_utils::diagnostics::span_lint_and_then;
 use clippy_utils::is_from_proc_macro;
+use rustc_errors::Applicability;
 use rustc_hir::{LetStmt, TyKind};
 use rustc_lint::{LateContext, LateLintPass};
 use rustc_session::declare_lint_pass;
@@ -32,13 +33,19 @@ impl<'tcx> LateLintPass<'tcx> for UnderscoreTyped {
             && !local.span.in_external_macro(cx.tcx.sess.source_map())
             && !is_from_proc_macro(cx, ty)
         {
-            span_lint_and_help(
+            span_lint_and_then(
                 cx,
                 LET_WITH_TYPE_UNDERSCORE,
                 local.span,
                 "variable declared with type underscore",
-                Some(ty.span.with_lo(local.pat.span.hi())),
-                "remove the explicit type `_` declaration",
+                |diag| {
+                    diag.span_suggestion_verbose(
+                        ty.span.with_lo(local.pat.span.hi()),
+                        "remove the explicit type `_` declaration",
+                        "",
+                        Applicability::MachineApplicable,
+                    );
+                },
             );
         }
     }
diff --git a/tests/ui/let_with_type_underscore.fixed b/tests/ui/let_with_type_underscore.fixed
new file mode 100644
index 00000000000..7a4af4e3d1e
--- /dev/null
+++ b/tests/ui/let_with_type_underscore.fixed
@@ -0,0 +1,47 @@
+//@aux-build: proc_macros.rs
+#![allow(unused)]
+#![warn(clippy::let_with_type_underscore)]
+#![allow(clippy::let_unit_value, clippy::needless_late_init)]
+
+extern crate proc_macros;
+
+fn func() -> &'static str {
+    ""
+}
+
+#[rustfmt::skip]
+fn main() {
+    // Will lint
+    let x = 1;
+    //~^ let_with_type_underscore
+    let _ = 2;
+    //~^ let_with_type_underscore
+    let x = func();
+    //~^ let_with_type_underscore
+    let x;
+    //~^ let_with_type_underscore
+    x = ();
+
+    let x = 1; // Will not lint, Rust infers this to an integer before Clippy
+    let x = func();
+    let x: Vec<_> = Vec::<u32>::new();
+    let x: [_; 1] = [1];
+    let x = 1;
+    //~^ let_with_type_underscore
+
+    // Do not lint from procedural macros
+    proc_macros::with_span! {
+        span
+        let x: _ = ();
+        // Late initialization
+        let x: _;
+        x = ();
+        // Ensure weird formatting will not break it (hopefully)
+        let x : _ = 1;
+        let x
+: _ = 1;
+        let                   x :              
+        _;
+        x = ();
+    };
+}
diff --git a/tests/ui/let_with_type_underscore.stderr b/tests/ui/let_with_type_underscore.stderr
index 2284d1fe2e4..9179f992207 100644
--- a/tests/ui/let_with_type_underscore.stderr
+++ b/tests/ui/let_with_type_underscore.stderr
@@ -4,13 +4,13 @@ error: variable declared with type underscore
 LL |     let x: _ = 1;
    |     ^^^^^^^^^^^^^
    |
-help: remove the explicit type `_` declaration
-  --> tests/ui/let_with_type_underscore.rs:15:10
-   |
-LL |     let x: _ = 1;
-   |          ^^^
    = note: `-D clippy::let-with-type-underscore` implied by `-D warnings`
    = help: to override `-D warnings` add `#[allow(clippy::let_with_type_underscore)]`
+help: remove the explicit type `_` declaration
+   |
+LL -     let x: _ = 1;
+LL +     let x = 1;
+   |
 
 error: variable declared with type underscore
   --> tests/ui/let_with_type_underscore.rs:17:5
@@ -19,10 +19,10 @@ LL |     let _: _ = 2;
    |     ^^^^^^^^^^^^^
    |
 help: remove the explicit type `_` declaration
-  --> tests/ui/let_with_type_underscore.rs:17:10
    |
-LL |     let _: _ = 2;
-   |          ^^^
+LL -     let _: _ = 2;
+LL +     let _ = 2;
+   |
 
 error: variable declared with type underscore
   --> tests/ui/let_with_type_underscore.rs:19:5
@@ -31,10 +31,10 @@ LL |     let x: _ = func();
    |     ^^^^^^^^^^^^^^^^^^
    |
 help: remove the explicit type `_` declaration
-  --> tests/ui/let_with_type_underscore.rs:19:10
    |
-LL |     let x: _ = func();
-   |          ^^^
+LL -     let x: _ = func();
+LL +     let x = func();
+   |
 
 error: variable declared with type underscore
   --> tests/ui/let_with_type_underscore.rs:21:5
@@ -43,10 +43,10 @@ LL |     let x: _;
    |     ^^^^^^^^^
    |
 help: remove the explicit type `_` declaration
-  --> tests/ui/let_with_type_underscore.rs:21:10
    |
-LL |     let x: _;
-   |          ^^^
+LL -     let x: _;
+LL +     let x;
+   |
 
 error: variable declared with type underscore
   --> tests/ui/let_with_type_underscore.rs:29:5
@@ -55,10 +55,10 @@ LL |     let x : _ = 1;
    |     ^^^^^^^^^^^^^^
    |
 help: remove the explicit type `_` declaration
-  --> tests/ui/let_with_type_underscore.rs:29:10
    |
-LL |     let x : _ = 1;
-   |          ^^^^
+LL -     let x : _ = 1;
+LL +     let x = 1;
+   |
 
 error: aborting due to 5 previous errors