]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/loops/never_loop.rs
c025f5972d5195ff67ea0f6e2c47c7ee65759a55
[rust.git] / clippy_lints / src / loops / never_loop.rs
1 use super::utils::make_iterator_snippet;
2 use super::NEVER_LOOP;
3 use clippy_utils::diagnostics::span_lint_and_then;
4 use clippy_utils::higher::ForLoop;
5 use clippy_utils::source::snippet;
6 use rustc_errors::Applicability;
7 use rustc_hir::{Block, Expr, ExprKind, HirId, InlineAsmOperand, Pat, Stmt, StmtKind};
8 use rustc_lint::LateContext;
9 use rustc_span::Span;
10 use std::iter::{once, Iterator};
11
12 pub(super) fn check(
13     cx: &LateContext<'_>,
14     block: &Block<'_>,
15     loop_id: HirId,
16     span: Span,
17     for_loop: Option<&ForLoop<'_>>,
18 ) {
19     match never_loop_block(block, loop_id) {
20         NeverLoopResult::AlwaysBreak => {
21             span_lint_and_then(cx, NEVER_LOOP, span, "this loop never actually loops", |diag| {
22                 if let Some(ForLoop {
23                     arg: iterator,
24                     pat,
25                     span: for_span,
26                     ..
27                 }) = for_loop
28                 {
29                     // Suggests using an `if let` instead. This is `Unspecified` because the
30                     // loop may (probably) contain `break` statements which would be invalid
31                     // in an `if let`.
32                     diag.span_suggestion_verbose(
33                         for_span.with_hi(iterator.span.hi()),
34                         "if you need the first element of the iterator, try writing",
35                         for_to_if_let_sugg(cx, iterator, pat),
36                         Applicability::Unspecified,
37                     );
38                 }
39             });
40         },
41         NeverLoopResult::MayContinueMainLoop | NeverLoopResult::Otherwise => (),
42     }
43 }
44
45 enum NeverLoopResult {
46     // A break/return always get triggered but not necessarily for the main loop.
47     AlwaysBreak,
48     // A continue may occur for the main loop.
49     MayContinueMainLoop,
50     Otherwise,
51 }
52
53 #[must_use]
54 fn absorb_break(arg: &NeverLoopResult) -> NeverLoopResult {
55     match *arg {
56         NeverLoopResult::AlwaysBreak | NeverLoopResult::Otherwise => NeverLoopResult::Otherwise,
57         NeverLoopResult::MayContinueMainLoop => NeverLoopResult::MayContinueMainLoop,
58     }
59 }
60
61 // Combine two results for parts that are called in order.
62 #[must_use]
63 fn combine_seq(first: NeverLoopResult, second: NeverLoopResult) -> NeverLoopResult {
64     match first {
65         NeverLoopResult::AlwaysBreak | NeverLoopResult::MayContinueMainLoop => first,
66         NeverLoopResult::Otherwise => second,
67     }
68 }
69
70 // Combine two results where both parts are called but not necessarily in order.
71 #[must_use]
72 fn combine_both(left: NeverLoopResult, right: NeverLoopResult) -> NeverLoopResult {
73     match (left, right) {
74         (NeverLoopResult::MayContinueMainLoop, _) | (_, NeverLoopResult::MayContinueMainLoop) => {
75             NeverLoopResult::MayContinueMainLoop
76         },
77         (NeverLoopResult::AlwaysBreak, _) | (_, NeverLoopResult::AlwaysBreak) => NeverLoopResult::AlwaysBreak,
78         (NeverLoopResult::Otherwise, NeverLoopResult::Otherwise) => NeverLoopResult::Otherwise,
79     }
80 }
81
82 // Combine two results where only one of the part may have been executed.
83 #[must_use]
84 fn combine_branches(b1: NeverLoopResult, b2: NeverLoopResult) -> NeverLoopResult {
85     match (b1, b2) {
86         (NeverLoopResult::AlwaysBreak, NeverLoopResult::AlwaysBreak) => NeverLoopResult::AlwaysBreak,
87         (NeverLoopResult::MayContinueMainLoop, _) | (_, NeverLoopResult::MayContinueMainLoop) => {
88             NeverLoopResult::MayContinueMainLoop
89         },
90         (NeverLoopResult::Otherwise, _) | (_, NeverLoopResult::Otherwise) => NeverLoopResult::Otherwise,
91     }
92 }
93
94 fn never_loop_block(block: &Block<'_>, main_loop_id: HirId) -> NeverLoopResult {
95     let mut iter = block.stmts.iter().filter_map(stmt_to_expr).chain(block.expr);
96     never_loop_expr_seq(&mut iter, main_loop_id)
97 }
98
99 fn never_loop_expr_seq<'a, T: Iterator<Item = &'a Expr<'a>>>(es: &mut T, main_loop_id: HirId) -> NeverLoopResult {
100     es.map(|e| never_loop_expr(e, main_loop_id))
101         .fold(NeverLoopResult::Otherwise, combine_seq)
102 }
103
104 fn stmt_to_expr<'tcx>(stmt: &Stmt<'tcx>) -> Option<&'tcx Expr<'tcx>> {
105     match stmt.kind {
106         StmtKind::Semi(e, ..) | StmtKind::Expr(e, ..) => Some(e),
107         StmtKind::Local(local) => local.init,
108         StmtKind::Item(..) => None,
109     }
110 }
111
112 fn never_loop_expr(expr: &Expr<'_>, main_loop_id: HirId) -> NeverLoopResult {
113     match expr.kind {
114         ExprKind::Box(e)
115         | ExprKind::Unary(_, e)
116         | ExprKind::Cast(e, _)
117         | ExprKind::Type(e, _)
118         | ExprKind::Field(e, _)
119         | ExprKind::AddrOf(_, _, e)
120         | ExprKind::Struct(_, _, Some(e))
121         | ExprKind::Repeat(e, _)
122         | ExprKind::DropTemps(e) => never_loop_expr(e, main_loop_id),
123         ExprKind::Let(let_expr) => never_loop_expr(let_expr.init, main_loop_id),
124         ExprKind::Array(es) | ExprKind::MethodCall(_, es, _) | ExprKind::Tup(es) => {
125             never_loop_expr_all(&mut es.iter(), main_loop_id)
126         },
127         ExprKind::Call(e, es) => never_loop_expr_all(&mut once(e).chain(es.iter()), main_loop_id),
128         ExprKind::Binary(_, e1, e2)
129         | ExprKind::Assign(e1, e2, _)
130         | ExprKind::AssignOp(_, e1, e2)
131         | ExprKind::Index(e1, e2) => never_loop_expr_all(&mut [e1, e2].iter().copied(), main_loop_id),
132         ExprKind::Loop(b, _, _, _) => {
133             // Break can come from the inner loop so remove them.
134             absorb_break(&never_loop_block(b, main_loop_id))
135         },
136         ExprKind::If(e, e2, e3) => {
137             let e1 = never_loop_expr(e, main_loop_id);
138             let e2 = never_loop_expr(e2, main_loop_id);
139             let e3 = e3
140                 .as_ref()
141                 .map_or(NeverLoopResult::Otherwise, |e| never_loop_expr(e, main_loop_id));
142             combine_seq(e1, combine_branches(e2, e3))
143         },
144         ExprKind::Match(e, arms, _) => {
145             let e = never_loop_expr(e, main_loop_id);
146             if arms.is_empty() {
147                 e
148             } else {
149                 let arms = never_loop_expr_branch(&mut arms.iter().map(|a| a.body), main_loop_id);
150                 combine_seq(e, arms)
151             }
152         },
153         ExprKind::Block(b, _) => never_loop_block(b, main_loop_id),
154         ExprKind::Continue(d) => {
155             let id = d
156                 .target_id
157                 .expect("target ID can only be missing in the presence of compilation errors");
158             if id == main_loop_id {
159                 NeverLoopResult::MayContinueMainLoop
160             } else {
161                 NeverLoopResult::AlwaysBreak
162             }
163         },
164         ExprKind::Break(_, e) | ExprKind::Ret(e) => e.as_ref().map_or(NeverLoopResult::AlwaysBreak, |e| {
165             combine_seq(never_loop_expr(e, main_loop_id), NeverLoopResult::AlwaysBreak)
166         }),
167         ExprKind::InlineAsm(asm) => asm
168             .operands
169             .iter()
170             .map(|(o, _)| match o {
171                 InlineAsmOperand::In { expr, .. } | InlineAsmOperand::InOut { expr, .. } => {
172                     never_loop_expr(expr, main_loop_id)
173                 },
174                 InlineAsmOperand::Out { expr, .. } => never_loop_expr_all(&mut expr.iter(), main_loop_id),
175                 InlineAsmOperand::SplitInOut { in_expr, out_expr, .. } => {
176                     never_loop_expr_all(&mut once(in_expr).chain(out_expr.iter()), main_loop_id)
177                 },
178                 InlineAsmOperand::Const { .. }
179                 | InlineAsmOperand::SymFn { .. }
180                 | InlineAsmOperand::SymStatic { .. } => NeverLoopResult::Otherwise,
181             })
182             .fold(NeverLoopResult::Otherwise, combine_both),
183         ExprKind::Struct(_, _, None)
184         | ExprKind::Yield(_, _)
185         | ExprKind::Closure(_, _, _, _, _)
186         | ExprKind::Path(_)
187         | ExprKind::ConstBlock(_)
188         | ExprKind::Lit(_)
189         | ExprKind::Err => NeverLoopResult::Otherwise,
190     }
191 }
192
193 fn never_loop_expr_all<'a, T: Iterator<Item = &'a Expr<'a>>>(es: &mut T, main_loop_id: HirId) -> NeverLoopResult {
194     es.map(|e| never_loop_expr(e, main_loop_id))
195         .fold(NeverLoopResult::Otherwise, combine_both)
196 }
197
198 fn never_loop_expr_branch<'a, T: Iterator<Item = &'a Expr<'a>>>(e: &mut T, main_loop_id: HirId) -> NeverLoopResult {
199     e.map(|e| never_loop_expr(e, main_loop_id))
200         .fold(NeverLoopResult::AlwaysBreak, combine_branches)
201 }
202
203 fn for_to_if_let_sugg(cx: &LateContext<'_>, iterator: &Expr<'_>, pat: &Pat<'_>) -> String {
204     let pat_snippet = snippet(cx, pat.span, "_");
205     let iter_snippet = make_iterator_snippet(cx, iterator, &mut Applicability::Unspecified);
206
207     format!(
208         "if let Some({pat}) = {iter}.next()",
209         pat = pat_snippet,
210         iter = iter_snippet
211     )
212 }