]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/loops/manual_flatten.rs
Auto merge of #88163 - camsteffen:collapsible-match-fix, r=Manishearth
[rust.git] / clippy_lints / src / loops / manual_flatten.rs
1 use super::utils::make_iterator_snippet;
2 use super::MANUAL_FLATTEN;
3 use clippy_utils::diagnostics::span_lint_and_then;
4 use clippy_utils::higher;
5 use clippy_utils::{is_lang_ctor, path_to_local_id};
6 use if_chain::if_chain;
7 use rustc_errors::Applicability;
8 use rustc_hir::LangItem::{OptionSome, ResultOk};
9 use rustc_hir::{Expr, ExprKind, Pat, PatKind, StmtKind};
10 use rustc_lint::LateContext;
11 use rustc_middle::ty;
12 use rustc_span::source_map::Span;
13
14 /// Check for unnecessary `if let` usage in a for loop where only the `Some` or `Ok` variant of the
15 /// iterator element is used.
16 pub(super) fn check<'tcx>(
17     cx: &LateContext<'tcx>,
18     pat: &'tcx Pat<'_>,
19     arg: &'tcx Expr<'_>,
20     body: &'tcx Expr<'_>,
21     span: Span,
22 ) {
23     if let ExprKind::Block(block, _) = body.kind {
24         // Ensure the `if let` statement is the only expression or statement in the for-loop
25         let inner_expr = if block.stmts.len() == 1 && block.expr.is_none() {
26             let match_stmt = &block.stmts[0];
27             if let StmtKind::Semi(inner_expr) = match_stmt.kind {
28                 Some(inner_expr)
29             } else {
30                 None
31             }
32         } else if block.stmts.is_empty() {
33             block.expr
34         } else {
35             None
36         };
37
38         if_chain! {
39             if let Some(inner_expr) = inner_expr;
40             if let Some(higher::IfLet { let_pat, let_expr, if_else: None, .. }) = higher::IfLet::hir(cx, inner_expr);
41             // Ensure match_expr in `if let` statement is the same as the pat from the for-loop
42             if let PatKind::Binding(_, pat_hir_id, _, _) = pat.kind;
43             if path_to_local_id(let_expr, pat_hir_id);
44             // Ensure the `if let` statement is for the `Some` variant of `Option` or the `Ok` variant of `Result`
45             if let PatKind::TupleStruct(ref qpath, _, _) = let_pat.kind;
46             let some_ctor = is_lang_ctor(cx, qpath, OptionSome);
47             let ok_ctor = is_lang_ctor(cx, qpath, ResultOk);
48             if some_ctor || ok_ctor;
49             then {
50                 let if_let_type = if some_ctor { "Some" } else { "Ok" };
51                 // Prepare the error message
52                 let msg = format!("unnecessary `if let` since only the `{}` variant of the iterator element is used", if_let_type);
53
54                 // Prepare the help message
55                 let mut applicability = Applicability::MaybeIncorrect;
56                 let arg_snippet = make_iterator_snippet(cx, arg, &mut applicability);
57                 let copied = match cx.typeck_results().expr_ty(let_expr).kind() {
58                     ty::Ref(_, inner, _) => match inner.kind() {
59                         ty::Ref(..) => ".copied()",
60                         _ => ""
61                     }
62                     _ => ""
63                 };
64
65                 span_lint_and_then(
66                     cx,
67                     MANUAL_FLATTEN,
68                     span,
69                     &msg,
70                     |diag| {
71                         let sugg = format!("{}{}.flatten()", arg_snippet, copied);
72                         diag.span_suggestion(
73                             arg.span,
74                             "try",
75                             sugg,
76                             Applicability::MaybeIncorrect,
77                         );
78                         diag.span_help(
79                             inner_expr.span,
80                             "...and remove the `if let` statement in the for loop",
81                         );
82                     }
83                 );
84             }
85         }
86     }
87 }