Auto merge of #9082 - Alexendoo:let_unit_allow, r=xFrednet

Fix direct `#[allow]` attributes in `let_unit_value`

Fixes part of #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`]
This commit is contained in:
bors 2022-07-01 15:51:41 +00:00
commit 097f7654b5
5 changed files with 44 additions and 25 deletions

View File

@ -4,18 +4,17 @@
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
);
}

View File

@ -3,7 +3,7 @@
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_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<'_>) {

View File

@ -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();
}

View File

@ -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 f4<T>(mut x: Vec<T>) -> T {
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
--> $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),