Skip to content

Commit

Permalink
Auto merge of rust-lang#9082 - Alexendoo:let_unit_allow, r=xFrednet
Browse files Browse the repository at this point in the history
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`]
  • Loading branch information
bors committed Jul 1, 2022
2 parents d8970bf + a5b70a4 commit 097f765
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 25 deletions.
17 changes: 8 additions & 9 deletions clippy_lints/src/unit_types/let_unit_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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(
Expand All @@ -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
);
}
Expand Down
6 changes: 3 additions & 3 deletions clippy_lints/src/unit_types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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<'_>) {
Expand Down
12 changes: 11 additions & 1 deletion tests/ui/let_unit.fixed
Original file line number Diff line number Diff line change
@@ -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) => {{
Expand Down Expand Up @@ -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();
}
12 changes: 11 additions & 1 deletion tests/ui/let_unit.rs
Original file line number Diff line number Diff line change
@@ -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) => {{
Expand Down Expand Up @@ -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();
}
22 changes: 11 additions & 11 deletions tests/ui/let_unit.stderr
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
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");`
|
= 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()
Expand All @@ -36,55 +36,55 @@ 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.
| ^^^^-^^^^^^^^^^^
| |
| 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.
| ^^^^-^^^^^^^^^^^^^^^^
| |
| 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
| ^^^^-^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| 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),
Expand Down

0 comments on commit 097f765

Please sign in to comment.