Fix allow attributes in let_unit_value

This commit is contained in:
Alex Macleod 2022-07-01 12:08:28 +00:00
parent d4488a520a
commit a5b70a4c1d
5 changed files with 44 additions and 25 deletions

View File

@ -4,18 +4,17 @@ use clippy_utils::visitors::for_each_value_source;
use core::ops::ControlFlow; use core::ops::ControlFlow;
use rustc_errors::Applicability; use rustc_errors::Applicability;
use rustc_hir::def::{DefKind, Res}; 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_lint::{LateContext, LintContext};
use rustc_middle::lint::in_external_macro; use rustc_middle::lint::in_external_macro;
use rustc_middle::ty::{self, Ty, TypeFoldable, TypeSuperFoldable, TypeVisitor}; use rustc_middle::ty::{self, Ty, TypeFoldable, TypeSuperFoldable, TypeVisitor};
use super::LET_UNIT_VALUE; use super::LET_UNIT_VALUE;
pub(super) fn check(cx: &LateContext<'_>, stmt: &Stmt<'_>) { pub(super) fn check(cx: &LateContext<'_>, local: &Local<'_>) {
if let StmtKind::Local(local) = stmt.kind if let Some(init) = local.init
&& let Some(init) = local.init
&& !local.pat.span.from_expansion() && !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() && 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) { 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( span_lint_and_then(
cx, cx,
LET_UNIT_VALUE, LET_UNIT_VALUE,
stmt.span, local.span,
"this let-binding has unit value", "this let-binding has unit value",
|diag| { |diag| {
diag.span_suggestion( diag.span_suggestion(
@ -45,15 +44,15 @@ pub(super) fn check(cx: &LateContext<'_>, stmt: &Stmt<'_>) {
span_lint_and_then( span_lint_and_then(
cx, cx,
LET_UNIT_VALUE, LET_UNIT_VALUE,
stmt.span, local.span,
"this let-binding has unit value", "this let-binding has unit value",
|diag| { |diag| {
if let Some(expr) = &local.init { if let Some(expr) = &local.init {
let snip = snippet_with_macro_callsite(cx, expr.span, "()"); let snip = snippet_with_macro_callsite(cx, expr.span, "()");
diag.span_suggestion( diag.span_suggestion(
stmt.span, local.span,
"omit the `let` binding", "omit the `let` binding",
format!("{};", snip), format!("{snip};"),
Applicability::MachineApplicable, // snippet Applicability::MachineApplicable, // snippet
); );
} }

View File

@ -3,7 +3,7 @@ mod unit_arg;
mod unit_cmp; mod unit_cmp;
mod utils; mod utils;
use rustc_hir::{Expr, Stmt}; use rustc_hir::{Expr, Local};
use rustc_lint::{LateContext, LateLintPass}; use rustc_lint::{LateContext, LateLintPass};
use rustc_session::{declare_lint_pass, declare_tool_lint}; 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]); declare_lint_pass!(UnitTypes => [LET_UNIT_VALUE, UNIT_CMP, UNIT_ARG]);
impl LateLintPass<'_> for UnitTypes { impl LateLintPass<'_> for UnitTypes {
fn check_stmt(&mut self, cx: &LateContext<'_>, stmt: &Stmt<'_>) { fn check_local(&mut self, cx: &LateContext<'_>, local: &Local<'_>) {
let_unit_value::check(cx, stmt); let_unit_value::check(cx, local);
} }
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {

View File

@ -1,8 +1,9 @@
// run-rustfix // run-rustfix
#![feature(lint_reasons)]
#![warn(clippy::let_unit_value)] #![warn(clippy::let_unit_value)]
#![allow(clippy::no_effect)] #![allow(clippy::no_effect)]
#![allow(unused_variables)] #![allow(unused)]
macro_rules! let_and_return { macro_rules! let_and_return {
($n:expr) => {{ ($n:expr) => {{
@ -113,3 +114,12 @@ fn _returns_generic() {
Some(_) => (), Some(_) => (),
}; };
} }
fn attributes() {
fn f() {}
#[allow(clippy::let_unit_value)]
let _ = f();
#[expect(clippy::let_unit_value)]
let _ = f();
}

View File

@ -1,8 +1,9 @@
// run-rustfix // run-rustfix
#![feature(lint_reasons)]
#![warn(clippy::let_unit_value)] #![warn(clippy::let_unit_value)]
#![allow(clippy::no_effect)] #![allow(clippy::no_effect)]
#![allow(unused_variables)] #![allow(unused)]
macro_rules! let_and_return { macro_rules! let_and_return {
($n:expr) => {{ ($n:expr) => {{
@ -113,3 +114,12 @@ fn _returns_generic() {
Some(_) => (), Some(_) => (),
}; };
} }
fn attributes() {
fn f() {}
#[allow(clippy::let_unit_value)]
let _ = f();
#[expect(clippy::let_unit_value)]
let _ = f();
}

View File

@ -1,5 +1,5 @@
error: this let-binding has unit value error: this let-binding has unit value
--> $DIR/let_unit.rs:14:5 --> $DIR/let_unit.rs:15:5
| |
LL | let _x = println!("x"); LL | let _x = println!("x");
| ^^^^^^^^^^^^^^^^^^^^^^^ help: omit the `let` binding: `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` = note: `-D clippy::let-unit-value` implied by `-D warnings`
error: this let-binding has unit value error: this let-binding has unit value
--> $DIR/let_unit.rs:18:9 --> $DIR/let_unit.rs:19:9
| |
LL | let _a = (); LL | let _a = ();
| ^^^^^^^^^^^^ help: omit the `let` binding: `();` | ^^^^^^^^^^^^ help: omit the `let` binding: `();`
error: this let-binding has unit value error: this let-binding has unit value
--> $DIR/let_unit.rs:53:5 --> $DIR/let_unit.rs:54:5
| |
LL | / let _ = v LL | / let _ = v
LL | | .into_iter() LL | | .into_iter()
@ -36,7 +36,7 @@ LL + .unwrap();
| |
error: this let-binding has unit value error: this let-binding has unit value
--> $DIR/let_unit.rs:80:5 --> $DIR/let_unit.rs:81:5
| |
LL | let x: () = f(); // Lint. LL | let x: () = f(); // Lint.
| ^^^^-^^^^^^^^^^^ | ^^^^-^^^^^^^^^^^
@ -44,7 +44,7 @@ LL | let x: () = f(); // Lint.
| help: use a wild (`_`) binding: `_` | help: use a wild (`_`) binding: `_`
error: this let-binding has unit value 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. LL | let x: () = f2(0i32); // Lint.
| ^^^^-^^^^^^^^^^^^^^^^ | ^^^^-^^^^^^^^^^^^^^^^
@ -52,31 +52,31 @@ LL | let x: () = f2(0i32); // Lint.
| help: use a wild (`_`) binding: `_` | help: use a wild (`_`) binding: `_`
error: this let-binding has unit value error: this let-binding has unit value
--> $DIR/let_unit.rs:85:5 --> $DIR/let_unit.rs:86:5
| |
LL | let _: () = f3(()); // Lint LL | let _: () = f3(()); // Lint
| ^^^^^^^^^^^^^^^^^^^ help: omit the `let` binding: `f3(());` | ^^^^^^^^^^^^^^^^^^^ help: omit the `let` binding: `f3(());`
error: this let-binding has unit value error: this let-binding has unit value
--> $DIR/let_unit.rs:86:5 --> $DIR/let_unit.rs:87:5
| |
LL | let x: () = f3(()); // Lint LL | let x: () = f3(()); // Lint
| ^^^^^^^^^^^^^^^^^^^ help: omit the `let` binding: `f3(());` | ^^^^^^^^^^^^^^^^^^^ help: omit the `let` binding: `f3(());`
error: this let-binding has unit value error: this let-binding has unit value
--> $DIR/let_unit.rs:88:5 --> $DIR/let_unit.rs:89:5
| |
LL | let _: () = f4(vec![()]); // Lint LL | let _: () = f4(vec![()]); // Lint
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: omit the `let` binding: `f4(vec![()]);` | ^^^^^^^^^^^^^^^^^^^^^^^^^ help: omit the `let` binding: `f4(vec![()]);`
error: this let-binding has unit value 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 LL | let x: () = f4(vec![()]); // Lint
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: omit the `let` binding: `f4(vec![()]);` | ^^^^^^^^^^^^^^^^^^^^^^^^^ help: omit the `let` binding: `f4(vec![()]);`
error: this let-binding has unit value 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 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: `_` | help: use a wild (`_`) binding: `_`
error: this let-binding has unit value 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 | / let _: () = match Some(0) {
LL | | None => f2(1), LL | | None => f2(1),