]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/unit_types/let_unit_value.rs
Rollup merge of #91239 - cameron1024:issue-87490-regression, r=jackh726
[rust.git] / src / tools / clippy / clippy_lints / src / unit_types / let_unit_value.rs
1 use clippy_utils::diagnostics::span_lint_and_then;
2 use clippy_utils::source::snippet_with_macro_callsite;
3 use rustc_errors::Applicability;
4 use rustc_hir::{Stmt, StmtKind};
5 use rustc_lint::{LateContext, LintContext};
6 use rustc_middle::lint::in_external_macro;
7
8 use super::LET_UNIT_VALUE;
9
10 pub(super) fn check(cx: &LateContext<'_>, stmt: &Stmt<'_>) {
11     if let StmtKind::Local(local) = stmt.kind {
12         if cx.typeck_results().pat_ty(local.pat).is_unit() {
13             if in_external_macro(cx.sess(), stmt.span) || local.pat.span.from_expansion() {
14                 return;
15             }
16             span_lint_and_then(
17                 cx,
18                 LET_UNIT_VALUE,
19                 stmt.span,
20                 "this let-binding has unit value",
21                 |diag| {
22                     if let Some(expr) = &local.init {
23                         let snip = snippet_with_macro_callsite(cx, expr.span, "()");
24                         diag.span_suggestion(
25                             stmt.span,
26                             "omit the `let` binding",
27                             format!("{};", snip),
28                             Applicability::MachineApplicable, // snippet
29                         );
30                     }
31                 },
32             );
33         }
34     }
35 }