about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-06-07 05:02:42 +0000
committerbors <bors@rust-lang.org>2023-06-07 05:02:42 +0000
commitf72914732527904480887847fe2c20df42e24dc2 (patch)
treed75cdeebb1cc3c2027ad3a93a1992523b8c3f01d
parentc8a056547b1b91ff3e5468d9331e8bcdcf837015 (diff)
parentba1fb74bf1633a5150b4843f6d0bde7af2443950 (diff)
downloadrust-f72914732527904480887847fe2c20df42e24dc2.tar.gz
rust-f72914732527904480887847fe2c20df42e24dc2.zip
Auto merge of #10865 - Centri3:let_with_type_underscore_tracing, r=Jarcho
[`let_with_type_underscore`]: Don't emit on locals from procedural macros

closes #10498

changelog: [`let_with_type_underscore`]: Don't emit on locals from procedural macros
-rw-r--r--clippy_lints/src/let_with_type_underscore.rs12
-rw-r--r--tests/ui/let_with_type_underscore.rs25
-rw-r--r--tests/ui/let_with_type_underscore.stderr38
3 files changed, 65 insertions, 10 deletions
diff --git a/clippy_lints/src/let_with_type_underscore.rs b/clippy_lints/src/let_with_type_underscore.rs
index 2f10e3d2581..4e9d77ea156 100644
--- a/clippy_lints/src/let_with_type_underscore.rs
+++ b/clippy_lints/src/let_with_type_underscore.rs
@@ -1,4 +1,5 @@
 use clippy_utils::diagnostics::span_lint_and_help;
+use clippy_utils::source::snippet;
 use rustc_hir::{Local, TyKind};
 use rustc_lint::{LateContext, LateLintPass};
 use rustc_middle::lint::in_external_macro;
@@ -25,14 +26,21 @@ declare_clippy_lint! {
 declare_lint_pass!(UnderscoreTyped => [LET_WITH_TYPE_UNDERSCORE]);
 
 impl LateLintPass<'_> for UnderscoreTyped {
-    fn check_local<'tcx>(&mut self, cx: &LateContext<'tcx>, local: &'tcx Local<'tcx>) {
+    fn check_local(&mut self, cx: &LateContext<'_>, local: &Local<'_>) {
         if_chain! {
             if !in_external_macro(cx.tcx.sess, local.span);
             if let Some(ty) = local.ty; // Ensure that it has a type defined
             if let TyKind::Infer = &ty.kind; // that type is '_'
             if local.span.ctxt() == ty.span.ctxt();
             then {
-                span_lint_and_help(cx,
+                // NOTE: Using `is_from_proc_macro` on `init` will require that it's initialized,
+                // this doesn't. Alternatively, `WithSearchPat` can be implemented for `Ty`
+                if snippet(cx, ty.span, "_").trim() != "_" {
+                    return;
+                }
+
+                span_lint_and_help(
+                    cx,
                     LET_WITH_TYPE_UNDERSCORE,
                     local.span,
                     "variable declared with type underscore",
diff --git a/tests/ui/let_with_type_underscore.rs b/tests/ui/let_with_type_underscore.rs
index 7c1835e8cd1..ae1a480bcfc 100644
--- a/tests/ui/let_with_type_underscore.rs
+++ b/tests/ui/let_with_type_underscore.rs
@@ -1,19 +1,42 @@
+//@aux-build: proc_macros.rs
 #![allow(unused)]
 #![warn(clippy::let_with_type_underscore)]
-#![allow(clippy::let_unit_value)]
+#![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 _: _ = 2;
     let x: _ = func();
+    let x: _;
+    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;
+
+    // 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 16bf83c708f..a749552c7fa 100644
--- a/tests/ui/let_with_type_underscore.stderr
+++ b/tests/ui/let_with_type_underscore.stderr
@@ -1,39 +1,63 @@
 error: variable declared with type underscore
-  --> $DIR/let_with_type_underscore.rs:11:5
+  --> $DIR/let_with_type_underscore.rs:15:5
    |
 LL |     let x: _ = 1;
    |     ^^^^^^^^^^^^^
    |
 help: remove the explicit type `_` declaration
-  --> $DIR/let_with_type_underscore.rs:11:10
+  --> $DIR/let_with_type_underscore.rs:15:10
    |
 LL |     let x: _ = 1;
    |          ^^^
    = note: `-D clippy::let-with-type-underscore` implied by `-D warnings`
 
 error: variable declared with type underscore
-  --> $DIR/let_with_type_underscore.rs:12:5
+  --> $DIR/let_with_type_underscore.rs:16:5
    |
 LL |     let _: _ = 2;
    |     ^^^^^^^^^^^^^
    |
 help: remove the explicit type `_` declaration
-  --> $DIR/let_with_type_underscore.rs:12:10
+  --> $DIR/let_with_type_underscore.rs:16:10
    |
 LL |     let _: _ = 2;
    |          ^^^
 
 error: variable declared with type underscore
-  --> $DIR/let_with_type_underscore.rs:13:5
+  --> $DIR/let_with_type_underscore.rs:17:5
    |
 LL |     let x: _ = func();
    |     ^^^^^^^^^^^^^^^^^^
    |
 help: remove the explicit type `_` declaration
-  --> $DIR/let_with_type_underscore.rs:13:10
+  --> $DIR/let_with_type_underscore.rs:17:10
    |
 LL |     let x: _ = func();
    |          ^^^
 
-error: aborting due to 3 previous errors
+error: variable declared with type underscore
+  --> $DIR/let_with_type_underscore.rs:18:5
+   |
+LL |     let x: _;
+   |     ^^^^^^^^^
+   |
+help: remove the explicit type `_` declaration
+  --> $DIR/let_with_type_underscore.rs:18:10
+   |
+LL |     let x: _;
+   |          ^^^
+
+error: variable declared with type underscore
+  --> $DIR/let_with_type_underscore.rs:25:5
+   |
+LL |     let x : _ = 1;
+   |     ^^^^^^^^^^^^^^
+   |
+help: remove the explicit type `_` declaration
+  --> $DIR/let_with_type_underscore.rs:25:10
+   |
+LL |     let x : _ = 1;
+   |          ^^^^
+
+error: aborting due to 5 previous errors