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