diff options
| author | bors <bors@rust-lang.org> | 2022-07-01 15:51:41 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2022-07-01 15:51:41 +0000 |
| commit | 097f7654b55e00167bdf74fc2da73ceba1f68287 (patch) | |
| tree | 0db51e8a742575201074d60fd5655da7c290de0f /clippy_lints/src | |
| parent | d8970bfa089ad515d914ce12b0049e427082b254 (diff) | |
| parent | a5b70a4c1df9d009823ec067e7544ce28ec29c2f (diff) | |
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`]
Diffstat (limited to 'clippy_lints/src')
| -rw-r--r-- | clippy_lints/src/unit_types/let_unit_value.rs | 17 | ||||
| -rw-r--r-- | clippy_lints/src/unit_types/mod.rs | 6 |
2 files changed, 11 insertions, 12 deletions
diff --git a/clippy_lints/src/unit_types/let_unit_value.rs b/clippy_lints/src/unit_types/let_unit_value.rs index 27678c8ba3c..17d60a40dce 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 a9e2073dec2..e57d4fc3b89 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<'_>) { |
