diff --git a/clippy_lints/src/unit_types/let_unit_value.rs b/clippy_lints/src/unit_types/let_unit_value.rs index 27678c8ba3c46..17d60a40dce10 100644 --- a/clippy_lints/src/unit_types/let_unit_value.rs +++ b/clippy_lints/src/unit_types/let_unit_value.rs @@ -4,18 +4,17 @@ use clippy_utils::visitors::for_each_value_source; use core::ops::ControlFlow; use rustc_errors::Applicability; use rustc_hir::def::{DefKind, Res}; -use rustc_hir::{Expr, ExprKind, PatKind, Stmt, StmtKind}; +use rustc_hir::{Expr, ExprKind, Local, PatKind}; use rustc_lint::{LateContext, LintContext}; use rustc_middle::lint::in_external_macro; use rustc_middle::ty::{self, Ty, TypeFoldable, TypeSuperFoldable, TypeVisitor}; use super::LET_UNIT_VALUE; -pub(super) fn check(cx: &LateContext<'_>, stmt: &Stmt<'_>) { - if let StmtKind::Local(local) = stmt.kind - && let Some(init) = local.init +pub(super) fn check(cx: &LateContext<'_>, local: &Local<'_>) { + if let Some(init) = local.init && !local.pat.span.from_expansion() - && !in_external_macro(cx.sess(), stmt.span) + && !in_external_macro(cx.sess(), local.span) && cx.typeck_results().pat_ty(local.pat).is_unit() { let needs_inferred = for_each_value_source(init, &mut |e| if needs_inferred_result_ty(cx, e) { @@ -29,7 +28,7 @@ pub(super) fn check(cx: &LateContext<'_>, stmt: &Stmt<'_>) { span_lint_and_then( cx, LET_UNIT_VALUE, - stmt.span, + local.span, "this let-binding has unit value", |diag| { diag.span_suggestion( @@ -45,15 +44,15 @@ pub(super) fn check(cx: &LateContext<'_>, stmt: &Stmt<'_>) { span_lint_and_then( cx, LET_UNIT_VALUE, - stmt.span, + local.span, "this let-binding has unit value", |diag| { if let Some(expr) = &local.init { let snip = snippet_with_macro_callsite(cx, expr.span, "()"); diag.span_suggestion( - stmt.span, + local.span, "omit the `let` binding", - format!("{};", snip), + format!("{snip};"), Applicability::MachineApplicable, // snippet ); } diff --git a/clippy_lints/src/unit_types/mod.rs b/clippy_lints/src/unit_types/mod.rs index a9e2073dec251..e57d4fc3b8917 100644 --- a/clippy_lints/src/unit_types/mod.rs +++ b/clippy_lints/src/unit_types/mod.rs @@ -3,7 +3,7 @@ mod unit_arg; mod unit_cmp; mod utils; -use rustc_hir::{Expr, Stmt}; +use rustc_hir::{Expr, Local}; use rustc_lint::{LateContext, LateLintPass}; use rustc_session::{declare_lint_pass, declare_tool_lint}; @@ -99,8 +99,8 @@ declare_clippy_lint! { declare_lint_pass!(UnitTypes => [LET_UNIT_VALUE, UNIT_CMP, UNIT_ARG]); impl LateLintPass<'_> for UnitTypes { - fn check_stmt(&mut self, cx: &LateContext<'_>, stmt: &Stmt<'_>) { - let_unit_value::check(cx, stmt); + fn check_local(&mut self, cx: &LateContext<'_>, local: &Local<'_>) { + let_unit_value::check(cx, local); } fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { diff --git a/tests/ui/let_unit.fixed b/tests/ui/let_unit.fixed index e72b746232551..18c2672f8804d 100644 --- a/tests/ui/let_unit.fixed +++ b/tests/ui/let_unit.fixed @@ -1,8 +1,9 @@ // run-rustfix +#![feature(lint_reasons)] #![warn(clippy::let_unit_value)] #![allow(clippy::no_effect)] -#![allow(unused_variables)] +#![allow(unused)] macro_rules! let_and_return { ($n:expr) => {{ @@ -113,3 +114,12 @@ fn _returns_generic() { Some(_) => (), }; } + +fn attributes() { + fn f() {} + + #[allow(clippy::let_unit_value)] + let _ = f(); + #[expect(clippy::let_unit_value)] + let _ = f(); +} diff --git a/tests/ui/let_unit.rs b/tests/ui/let_unit.rs index 47ee0a7672479..c9c4582a956e1 100644 --- a/tests/ui/let_unit.rs +++ b/tests/ui/let_unit.rs @@ -1,8 +1,9 @@ // run-rustfix +#![feature(lint_reasons)] #![warn(clippy::let_unit_value)] #![allow(clippy::no_effect)] -#![allow(unused_variables)] +#![allow(unused)] macro_rules! let_and_return { ($n:expr) => {{ @@ -113,3 +114,12 @@ fn _returns_generic() { Some(_) => (), }; } + +fn attributes() { + fn f() {} + + #[allow(clippy::let_unit_value)] + let _ = f(); + #[expect(clippy::let_unit_value)] + let _ = f(); +} diff --git a/tests/ui/let_unit.stderr b/tests/ui/let_unit.stderr index 45bf67acdb736..47a2a6e3cc1c9 100644 --- a/tests/ui/let_unit.stderr +++ b/tests/ui/let_unit.stderr @@ -1,5 +1,5 @@ error: this let-binding has unit value - --> $DIR/let_unit.rs:14:5 + --> $DIR/let_unit.rs:15:5 | LL | let _x = println!("x"); | ^^^^^^^^^^^^^^^^^^^^^^^ help: omit the `let` binding: `println!("x");` @@ -7,13 +7,13 @@ LL | let _x = println!("x"); = note: `-D clippy::let-unit-value` implied by `-D warnings` error: this let-binding has unit value - --> $DIR/let_unit.rs:18:9 + --> $DIR/let_unit.rs:19:9 | LL | let _a = (); | ^^^^^^^^^^^^ help: omit the `let` binding: `();` error: this let-binding has unit value - --> $DIR/let_unit.rs:53:5 + --> $DIR/let_unit.rs:54:5 | LL | / let _ = v LL | | .into_iter() @@ -36,7 +36,7 @@ LL + .unwrap(); | error: this let-binding has unit value - --> $DIR/let_unit.rs:80:5 + --> $DIR/let_unit.rs:81:5 | LL | let x: () = f(); // Lint. | ^^^^-^^^^^^^^^^^ @@ -44,7 +44,7 @@ LL | let x: () = f(); // Lint. | help: use a wild (`_`) binding: `_` error: this let-binding has unit value - --> $DIR/let_unit.rs:83:5 + --> $DIR/let_unit.rs:84:5 | LL | let x: () = f2(0i32); // Lint. | ^^^^-^^^^^^^^^^^^^^^^ @@ -52,31 +52,31 @@ LL | let x: () = f2(0i32); // Lint. | help: use a wild (`_`) binding: `_` error: this let-binding has unit value - --> $DIR/let_unit.rs:85:5 + --> $DIR/let_unit.rs:86:5 | LL | let _: () = f3(()); // Lint | ^^^^^^^^^^^^^^^^^^^ help: omit the `let` binding: `f3(());` error: this let-binding has unit value - --> $DIR/let_unit.rs:86:5 + --> $DIR/let_unit.rs:87:5 | LL | let x: () = f3(()); // Lint | ^^^^^^^^^^^^^^^^^^^ help: omit the `let` binding: `f3(());` error: this let-binding has unit value - --> $DIR/let_unit.rs:88:5 + --> $DIR/let_unit.rs:89:5 | LL | let _: () = f4(vec![()]); // Lint | ^^^^^^^^^^^^^^^^^^^^^^^^^ help: omit the `let` binding: `f4(vec![()]);` error: this let-binding has unit value - --> $DIR/let_unit.rs:89:5 + --> $DIR/let_unit.rs:90:5 | LL | let x: () = f4(vec![()]); // Lint | ^^^^^^^^^^^^^^^^^^^^^^^^^ help: omit the `let` binding: `f4(vec![()]);` error: this let-binding has unit value - --> $DIR/let_unit.rs:98:5 + --> $DIR/let_unit.rs:99:5 | LL | let x: () = if true { f() } else { f2(0) }; // Lint | ^^^^-^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ @@ -84,7 +84,7 @@ LL | let x: () = if true { f() } else { f2(0) }; // Lint | help: use a wild (`_`) binding: `_` error: this let-binding has unit value - --> $DIR/let_unit.rs:109:5 + --> $DIR/let_unit.rs:110:5 | LL | / let _: () = match Some(0) { LL | | None => f2(1),