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