]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/loops/while_let_loop.rs
separate the receiver from arguments in HIR under /clippy
[rust.git] / clippy_lints / src / loops / while_let_loop.rs
1 use super::WHILE_LET_LOOP;
2 use clippy_utils::diagnostics::span_lint_and_sugg;
3 use clippy_utils::higher;
4 use clippy_utils::source::snippet_with_applicability;
5 use clippy_utils::ty::needs_ordered_drop;
6 use clippy_utils::visitors::any_temporaries_need_ordered_drop;
7 use rustc_errors::Applicability;
8 use rustc_hir::{Block, Expr, ExprKind, Local, MatchSource, Pat, StmtKind};
9 use rustc_lint::LateContext;
10
11 pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, loop_block: &'tcx Block<'_>) {
12     let (init, has_trailing_exprs) = match (loop_block.stmts, loop_block.expr) {
13         ([stmt, stmts @ ..], expr) => {
14             if let StmtKind::Local(&Local {
15                 init: Some(e),
16                 els: None,
17                 ..
18             })
19             | StmtKind::Semi(e)
20             | StmtKind::Expr(e) = stmt.kind
21             {
22                 (e, !stmts.is_empty() || expr.is_some())
23             } else {
24                 return;
25             }
26         },
27         ([], Some(e)) => (e, false),
28         _ => return,
29     };
30
31     if let Some(if_let) = higher::IfLet::hir(cx, init)
32         && let Some(else_expr) = if_let.if_else
33         && is_simple_break_expr(else_expr)
34     {
35         could_be_while_let(cx, expr, if_let.let_pat, if_let.let_expr, has_trailing_exprs);
36     } else if let ExprKind::Match(scrutinee, [arm1, arm2], MatchSource::Normal) = init.kind
37         && arm1.guard.is_none()
38         && arm2.guard.is_none()
39         && is_simple_break_expr(arm2.body)
40     {
41         could_be_while_let(cx, expr, arm1.pat, scrutinee, has_trailing_exprs);
42     }
43 }
44
45 /// Returns `true` if expr contains a single break expression without a label or eub-expression.
46 fn is_simple_break_expr(e: &Expr<'_>) -> bool {
47     matches!(peel_blocks(e).kind, ExprKind::Break(dest, None) if dest.label.is_none())
48 }
49
50 /// Removes any blocks containing only a single expression.
51 fn peel_blocks<'tcx>(e: &'tcx Expr<'tcx>) -> &'tcx Expr<'tcx> {
52     if let ExprKind::Block(b, _) = e.kind {
53         match (b.stmts, b.expr) {
54             ([s], None) => {
55                 if let StmtKind::Expr(e) | StmtKind::Semi(e) = s.kind {
56                     peel_blocks(e)
57                 } else {
58                     e
59                 }
60             },
61             ([], Some(e)) => peel_blocks(e),
62             _ => e,
63         }
64     } else {
65         e
66     }
67 }
68
69 fn could_be_while_let<'tcx>(
70     cx: &LateContext<'tcx>,
71     expr: &'tcx Expr<'_>,
72     let_pat: &'tcx Pat<'_>,
73     let_expr: &'tcx Expr<'_>,
74     has_trailing_exprs: bool,
75 ) {
76     if has_trailing_exprs
77         && (needs_ordered_drop(cx, cx.typeck_results().expr_ty(let_expr))
78             || any_temporaries_need_ordered_drop(cx, let_expr))
79     {
80         // Switching to a `while let` loop will extend the lifetime of some values.
81         return;
82     }
83
84     // NOTE: we used to build a body here instead of using
85     // ellipsis, this was removed because:
86     // 1) it was ugly with big bodies;
87     // 2) it was not indented properly;
88     // 3) it wasn’t very smart (see #675).
89     let mut applicability = Applicability::HasPlaceholders;
90     span_lint_and_sugg(
91         cx,
92         WHILE_LET_LOOP,
93         expr.span,
94         "this loop could be written as a `while let` loop",
95         "try",
96         format!(
97             "while let {} = {} {{ .. }}",
98             snippet_with_applicability(cx, let_pat.span, "..", &mut applicability),
99             snippet_with_applicability(cx, let_expr.span, "..", &mut applicability),
100         ),
101         applicability,
102     );
103 }