]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/loops/while_let_loop.rs
Merge remote-tracking branch 'upstream/master' into rustup
[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::source::snippet_with_applicability;
4 use rustc_errors::Applicability;
5 use rustc_hir::{Block, Expr, ExprKind, MatchSource, StmtKind};
6 use rustc_lint::{LateContext, LintContext};
7 use rustc_middle::lint::in_external_macro;
8
9 pub(super) fn check(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, loop_block: &'tcx Block<'_>) {
10     // extract the expression from the first statement (if any) in a block
11     let inner_stmt_expr = extract_expr_from_first_stmt(loop_block);
12     // or extract the first expression (if any) from the block
13     if let Some(inner) = inner_stmt_expr.or_else(|| extract_first_expr(loop_block)) {
14         if let ExprKind::Match(matchexpr, arms, ref source) = inner.kind {
15             // ensure "if let" compatible match structure
16             match *source {
17                 MatchSource::Normal | MatchSource::IfLetDesugar { .. } => {
18                     if arms.len() == 2
19                         && arms[0].guard.is_none()
20                         && arms[1].guard.is_none()
21                         && is_simple_break_expr(arms[1].body)
22                     {
23                         if in_external_macro(cx.sess(), expr.span) {
24                             return;
25                         }
26
27                         // NOTE: we used to build a body here instead of using
28                         // ellipsis, this was removed because:
29                         // 1) it was ugly with big bodies;
30                         // 2) it was not indented properly;
31                         // 3) it wasn’t very smart (see #675).
32                         let mut applicability = Applicability::HasPlaceholders;
33                         span_lint_and_sugg(
34                             cx,
35                             WHILE_LET_LOOP,
36                             expr.span,
37                             "this loop could be written as a `while let` loop",
38                             "try",
39                             format!(
40                                 "while let {} = {} {{ .. }}",
41                                 snippet_with_applicability(cx, arms[0].pat.span, "..", &mut applicability),
42                                 snippet_with_applicability(cx, matchexpr.span, "..", &mut applicability),
43                             ),
44                             applicability,
45                         );
46                     }
47                 },
48                 _ => (),
49             }
50         }
51     }
52 }
53
54 /// If a block begins with a statement (possibly a `let` binding) and has an
55 /// expression, return it.
56 fn extract_expr_from_first_stmt<'tcx>(block: &Block<'tcx>) -> Option<&'tcx Expr<'tcx>> {
57     if block.stmts.is_empty() {
58         return None;
59     }
60     if let StmtKind::Local(local) = block.stmts[0].kind {
61         local.init //.map(|expr| expr)
62     } else {
63         None
64     }
65 }
66
67 /// If a block begins with an expression (with or without semicolon), return it.
68 fn extract_first_expr<'tcx>(block: &Block<'tcx>) -> Option<&'tcx Expr<'tcx>> {
69     match block.expr {
70         Some(expr) if block.stmts.is_empty() => Some(expr),
71         None if !block.stmts.is_empty() => match block.stmts[0].kind {
72             StmtKind::Expr(expr) | StmtKind::Semi(expr) => Some(expr),
73             StmtKind::Local(..) | StmtKind::Item(..) => None,
74         },
75         _ => None,
76     }
77 }
78
79 /// Returns `true` if expr contains a single break expr without destination label
80 /// and
81 /// passed expression. The expression may be within a block.
82 fn is_simple_break_expr(expr: &Expr<'_>) -> bool {
83     match expr.kind {
84         ExprKind::Break(dest, ref passed_expr) if dest.label.is_none() && passed_expr.is_none() => true,
85         ExprKind::Block(b, _) => extract_first_expr(b).map_or(false, |subexpr| is_simple_break_expr(subexpr)),
86         _ => false,
87     }
88 }