Skip to content

Commit 097f765

Browse files
committed
Auto merge of rust-lang#9082 - Alexendoo:let_unit_allow, r=xFrednet
Fix direct `#[allow]` attributes in `let_unit_value` Fixes part of rust-lang#9080 Not sure why it doesn't work when the lint is emitted at the statement, but switching it to the local works fine changelog: Fix direct `#[allow]` attributes in [`let_unit_value`]
2 parents d8970bf + a5b70a4 commit 097f765

File tree

5 files changed

+44
-25
lines changed

5 files changed

+44
-25
lines changed

clippy_lints/src/unit_types/let_unit_value.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,17 @@ use clippy_utils::visitors::for_each_value_source;
44
use core::ops::ControlFlow;
55
use rustc_errors::Applicability;
66
use rustc_hir::def::{DefKind, Res};
7-
use rustc_hir::{Expr, ExprKind, PatKind, Stmt, StmtKind};
7+
use rustc_hir::{Expr, ExprKind, Local, PatKind};
88
use rustc_lint::{LateContext, LintContext};
99
use rustc_middle::lint::in_external_macro;
1010
use rustc_middle::ty::{self, Ty, TypeFoldable, TypeSuperFoldable, TypeVisitor};
1111

1212
use super::LET_UNIT_VALUE;
1313

14-
pub(super) fn check(cx: &LateContext<'_>, stmt: &Stmt<'_>) {
15-
if let StmtKind::Local(local) = stmt.kind
16-
&& let Some(init) = local.init
14+
pub(super) fn check(cx: &LateContext<'_>, local: &Local<'_>) {
15+
if let Some(init) = local.init
1716
&& !local.pat.span.from_expansion()
18-
&& !in_external_macro(cx.sess(), stmt.span)
17+
&& !in_external_macro(cx.sess(), local.span)
1918
&& cx.typeck_results().pat_ty(local.pat).is_unit()
2019
{
2120
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<'_>) {
2928
span_lint_and_then(
3029
cx,
3130
LET_UNIT_VALUE,
32-
stmt.span,
31+
local.span,
3332
"this let-binding has unit value",
3433
|diag| {
3534
diag.span_suggestion(
@@ -45,15 +44,15 @@ pub(super) fn check(cx: &LateContext<'_>, stmt: &Stmt<'_>) {
4544
span_lint_and_then(
4645
cx,
4746
LET_UNIT_VALUE,
48-
stmt.span,
47+
local.span,
4948
"this let-binding has unit value",
5049
|diag| {
5150
if let Some(expr) = &local.init {
5251
let snip = snippet_with_macro_callsite(cx, expr.span, "()");
5352
diag.span_suggestion(
54-
stmt.span,
53+
local.span,
5554
"omit the `let` binding",
56-
format!("{};", snip),
55+
format!("{snip};"),
5756
Applicability::MachineApplicable, // snippet
5857
);
5958
}

clippy_lints/src/unit_types/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ mod unit_arg;
33
mod unit_cmp;
44
mod utils;
55

6-
use rustc_hir::{Expr, Stmt};
6+
use rustc_hir::{Expr, Local};
77
use rustc_lint::{LateContext, LateLintPass};
88
use rustc_session::{declare_lint_pass, declare_tool_lint};
99

@@ -99,8 +99,8 @@ declare_clippy_lint! {
9999
declare_lint_pass!(UnitTypes => [LET_UNIT_VALUE, UNIT_CMP, UNIT_ARG]);
100100

101101
impl LateLintPass<'_> for UnitTypes {
102-
fn check_stmt(&mut self, cx: &LateContext<'_>, stmt: &Stmt<'_>) {
103-
let_unit_value::check(cx, stmt);
102+
fn check_local(&mut self, cx: &LateContext<'_>, local: &Local<'_>) {
103+
let_unit_value::check(cx, local);
104104
}
105105

106106
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {

tests/ui/let_unit.fixed

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// run-rustfix
22

3+
#![feature(lint_reasons)]
34
#![warn(clippy::let_unit_value)]
45
#![allow(clippy::no_effect)]
5-
#![allow(unused_variables)]
6+
#![allow(unused)]
67

78
macro_rules! let_and_return {
89
($n:expr) => {{
@@ -113,3 +114,12 @@ fn _returns_generic() {
113114
Some(_) => (),
114115
};
115116
}
117+
118+
fn attributes() {
119+
fn f() {}
120+
121+
#[allow(clippy::let_unit_value)]
122+
let _ = f();
123+
#[expect(clippy::let_unit_value)]
124+
let _ = f();
125+
}

tests/ui/let_unit.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
// run-rustfix
22

3+
#![feature(lint_reasons)]
34
#![warn(clippy::let_unit_value)]
45
#![allow(clippy::no_effect)]
5-
#![allow(unused_variables)]
6+
#![allow(unused)]
67

78
macro_rules! let_and_return {
89
($n:expr) => {{
@@ -113,3 +114,12 @@ fn _returns_generic() {
113114
Some(_) => (),
114115
};
115116
}
117+
118+
fn attributes() {
119+
fn f() {}
120+
121+
#[allow(clippy::let_unit_value)]
122+
let _ = f();
123+
#[expect(clippy::let_unit_value)]
124+
let _ = f();
125+
}

tests/ui/let_unit.stderr

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
error: this let-binding has unit value
2-
--> $DIR/let_unit.rs:14:5
2+
--> $DIR/let_unit.rs:15:5
33
|
44
LL | let _x = println!("x");
55
| ^^^^^^^^^^^^^^^^^^^^^^^ help: omit the `let` binding: `println!("x");`
66
|
77
= note: `-D clippy::let-unit-value` implied by `-D warnings`
88

99
error: this let-binding has unit value
10-
--> $DIR/let_unit.rs:18:9
10+
--> $DIR/let_unit.rs:19:9
1111
|
1212
LL | let _a = ();
1313
| ^^^^^^^^^^^^ help: omit the `let` binding: `();`
1414

1515
error: this let-binding has unit value
16-
--> $DIR/let_unit.rs:53:5
16+
--> $DIR/let_unit.rs:54:5
1717
|
1818
LL | / let _ = v
1919
LL | | .into_iter()
@@ -36,55 +36,55 @@ LL + .unwrap();
3636
|
3737

3838
error: this let-binding has unit value
39-
--> $DIR/let_unit.rs:80:5
39+
--> $DIR/let_unit.rs:81:5
4040
|
4141
LL | let x: () = f(); // Lint.
4242
| ^^^^-^^^^^^^^^^^
4343
| |
4444
| help: use a wild (`_`) binding: `_`
4545

4646
error: this let-binding has unit value
47-
--> $DIR/let_unit.rs:83:5
47+
--> $DIR/let_unit.rs:84:5
4848
|
4949
LL | let x: () = f2(0i32); // Lint.
5050
| ^^^^-^^^^^^^^^^^^^^^^
5151
| |
5252
| help: use a wild (`_`) binding: `_`
5353

5454
error: this let-binding has unit value
55-
--> $DIR/let_unit.rs:85:5
55+
--> $DIR/let_unit.rs:86:5
5656
|
5757
LL | let _: () = f3(()); // Lint
5858
| ^^^^^^^^^^^^^^^^^^^ help: omit the `let` binding: `f3(());`
5959

6060
error: this let-binding has unit value
61-
--> $DIR/let_unit.rs:86:5
61+
--> $DIR/let_unit.rs:87:5
6262
|
6363
LL | let x: () = f3(()); // Lint
6464
| ^^^^^^^^^^^^^^^^^^^ help: omit the `let` binding: `f3(());`
6565

6666
error: this let-binding has unit value
67-
--> $DIR/let_unit.rs:88:5
67+
--> $DIR/let_unit.rs:89:5
6868
|
6969
LL | let _: () = f4(vec![()]); // Lint
7070
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: omit the `let` binding: `f4(vec![()]);`
7171

7272
error: this let-binding has unit value
73-
--> $DIR/let_unit.rs:89:5
73+
--> $DIR/let_unit.rs:90:5
7474
|
7575
LL | let x: () = f4(vec![()]); // Lint
7676
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: omit the `let` binding: `f4(vec![()]);`
7777

7878
error: this let-binding has unit value
79-
--> $DIR/let_unit.rs:98:5
79+
--> $DIR/let_unit.rs:99:5
8080
|
8181
LL | let x: () = if true { f() } else { f2(0) }; // Lint
8282
| ^^^^-^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8383
| |
8484
| help: use a wild (`_`) binding: `_`
8585

8686
error: this let-binding has unit value
87-
--> $DIR/let_unit.rs:109:5
87+
--> $DIR/let_unit.rs:110:5
8888
|
8989
LL | / let _: () = match Some(0) {
9090
LL | | None => f2(1),

0 commit comments

Comments
 (0)