2021-08-12 04:16:25 -05:00
|
|
|
use super::utils::make_iterator_snippet;
|
2021-03-12 08:30:50 -06:00
|
|
|
use super::NEVER_LOOP;
|
2021-08-12 04:16:25 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_then;
|
2021-08-08 09:49:13 -05:00
|
|
|
use clippy_utils::higher::ForLoop;
|
2021-08-12 04:16:25 -05:00
|
|
|
use clippy_utils::source::snippet;
|
|
|
|
use rustc_errors::Applicability;
|
2022-11-21 13:34:47 -06:00
|
|
|
use rustc_hir::{Block, Destination, Expr, ExprKind, HirId, InlineAsmOperand, Pat, Stmt, StmtKind};
|
2021-03-12 08:30:50 -06:00
|
|
|
use rustc_lint::LateContext;
|
2021-10-27 09:48:06 -05:00
|
|
|
use rustc_span::Span;
|
2021-03-12 08:30:50 -06:00
|
|
|
use std::iter::{once, Iterator};
|
|
|
|
|
2021-10-27 09:48:06 -05:00
|
|
|
pub(super) fn check(
|
2022-01-13 06:18:19 -06:00
|
|
|
cx: &LateContext<'_>,
|
|
|
|
block: &Block<'_>,
|
2021-10-27 09:48:06 -05:00
|
|
|
loop_id: HirId,
|
|
|
|
span: Span,
|
|
|
|
for_loop: Option<&ForLoop<'_>>,
|
|
|
|
) {
|
2022-11-21 13:34:47 -06:00
|
|
|
match never_loop_block(block, &mut Vec::new(), loop_id) {
|
2021-10-27 09:48:06 -05:00
|
|
|
NeverLoopResult::AlwaysBreak => {
|
|
|
|
span_lint_and_then(cx, NEVER_LOOP, span, "this loop never actually loops", |diag| {
|
|
|
|
if let Some(ForLoop {
|
|
|
|
arg: iterator,
|
|
|
|
pat,
|
|
|
|
span: for_span,
|
|
|
|
..
|
|
|
|
}) = for_loop
|
|
|
|
{
|
|
|
|
// Suggests using an `if let` instead. This is `Unspecified` because the
|
|
|
|
// loop may (probably) contain `break` statements which would be invalid
|
|
|
|
// in an `if let`.
|
|
|
|
diag.span_suggestion_verbose(
|
|
|
|
for_span.with_hi(iterator.span.hi()),
|
|
|
|
"if you need the first element of the iterator, try writing",
|
|
|
|
for_to_if_let_sugg(cx, iterator, pat),
|
|
|
|
Applicability::Unspecified,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
NeverLoopResult::MayContinueMainLoop | NeverLoopResult::Otherwise => (),
|
2023-02-25 18:08:29 -06:00
|
|
|
NeverLoopResult::IgnoreUntilEnd(_) => unreachable!(),
|
2021-03-12 08:30:50 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-06 02:44:38 -05:00
|
|
|
#[derive(Copy, Clone)]
|
2021-03-12 08:30:50 -06:00
|
|
|
enum NeverLoopResult {
|
|
|
|
// A break/return always get triggered but not necessarily for the main loop.
|
|
|
|
AlwaysBreak,
|
|
|
|
// A continue may occur for the main loop.
|
|
|
|
MayContinueMainLoop,
|
2023-02-25 18:08:29 -06:00
|
|
|
// Ignore everything until the end of the block with this id
|
|
|
|
IgnoreUntilEnd(HirId),
|
2021-03-12 08:30:50 -06:00
|
|
|
Otherwise,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[must_use]
|
2022-10-06 02:44:38 -05:00
|
|
|
fn absorb_break(arg: NeverLoopResult) -> NeverLoopResult {
|
|
|
|
match arg {
|
2021-03-12 08:30:50 -06:00
|
|
|
NeverLoopResult::AlwaysBreak | NeverLoopResult::Otherwise => NeverLoopResult::Otherwise,
|
|
|
|
NeverLoopResult::MayContinueMainLoop => NeverLoopResult::MayContinueMainLoop,
|
2023-02-25 18:08:29 -06:00
|
|
|
NeverLoopResult::IgnoreUntilEnd(id) => NeverLoopResult::IgnoreUntilEnd(id),
|
2021-03-12 08:30:50 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Combine two results for parts that are called in order.
|
|
|
|
#[must_use]
|
|
|
|
fn combine_seq(first: NeverLoopResult, second: NeverLoopResult) -> NeverLoopResult {
|
|
|
|
match first {
|
2023-02-25 18:08:29 -06:00
|
|
|
NeverLoopResult::AlwaysBreak | NeverLoopResult::MayContinueMainLoop | NeverLoopResult::IgnoreUntilEnd(_) => {
|
|
|
|
first
|
2021-03-12 08:30:50 -06:00
|
|
|
},
|
2023-02-25 18:08:29 -06:00
|
|
|
NeverLoopResult::Otherwise => second,
|
2021-03-12 08:30:50 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Combine two results where only one of the part may have been executed.
|
|
|
|
#[must_use]
|
2023-02-25 18:08:29 -06:00
|
|
|
fn combine_branches(b1: NeverLoopResult, b2: NeverLoopResult, ignore_ids: &[HirId]) -> NeverLoopResult {
|
2021-03-12 08:30:50 -06:00
|
|
|
match (b1, b2) {
|
2023-02-25 18:08:29 -06:00
|
|
|
(NeverLoopResult::IgnoreUntilEnd(a), NeverLoopResult::IgnoreUntilEnd(b)) => {
|
|
|
|
if ignore_ids.iter().find(|&e| e == &a || e == &b).unwrap() == &a {
|
|
|
|
NeverLoopResult::IgnoreUntilEnd(b)
|
|
|
|
} else {
|
|
|
|
NeverLoopResult::IgnoreUntilEnd(a)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
(i @ NeverLoopResult::IgnoreUntilEnd(_), NeverLoopResult::AlwaysBreak)
|
|
|
|
| (NeverLoopResult::AlwaysBreak, i @ NeverLoopResult::IgnoreUntilEnd(_)) => i,
|
2021-03-12 08:30:50 -06:00
|
|
|
(NeverLoopResult::AlwaysBreak, NeverLoopResult::AlwaysBreak) => NeverLoopResult::AlwaysBreak,
|
|
|
|
(NeverLoopResult::MayContinueMainLoop, _) | (_, NeverLoopResult::MayContinueMainLoop) => {
|
|
|
|
NeverLoopResult::MayContinueMainLoop
|
|
|
|
},
|
|
|
|
(NeverLoopResult::Otherwise, _) | (_, NeverLoopResult::Otherwise) => NeverLoopResult::Otherwise,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-21 13:34:47 -06:00
|
|
|
fn never_loop_block(block: &Block<'_>, ignore_ids: &mut Vec<HirId>, main_loop_id: HirId) -> NeverLoopResult {
|
|
|
|
let iter = block
|
2022-10-06 02:44:38 -05:00
|
|
|
.stmts
|
|
|
|
.iter()
|
|
|
|
.filter_map(stmt_to_expr)
|
|
|
|
.chain(block.expr.map(|expr| (expr, None)));
|
2021-03-12 08:30:50 -06:00
|
|
|
|
2022-11-21 13:34:47 -06:00
|
|
|
iter.map(|(e, els)| {
|
|
|
|
let e = never_loop_expr(e, ignore_ids, main_loop_id);
|
|
|
|
// els is an else block in a let...else binding
|
|
|
|
els.map_or(e, |els| {
|
2023-02-25 18:08:29 -06:00
|
|
|
combine_branches(e, never_loop_block(els, ignore_ids, main_loop_id), ignore_ids)
|
2022-11-21 13:34:47 -06:00
|
|
|
})
|
2022-10-06 02:44:38 -05:00
|
|
|
})
|
|
|
|
.fold(NeverLoopResult::Otherwise, combine_seq)
|
2021-03-12 08:30:50 -06:00
|
|
|
}
|
|
|
|
|
2022-10-06 02:44:38 -05:00
|
|
|
fn stmt_to_expr<'tcx>(stmt: &Stmt<'tcx>) -> Option<(&'tcx Expr<'tcx>, Option<&'tcx Block<'tcx>>)> {
|
2021-03-12 08:30:50 -06:00
|
|
|
match stmt.kind {
|
2022-11-21 13:34:47 -06:00
|
|
|
StmtKind::Semi(e) | StmtKind::Expr(e) => Some((e, None)),
|
|
|
|
// add the let...else expression (if present)
|
2022-10-06 02:44:38 -05:00
|
|
|
StmtKind::Local(local) => local.init.map(|init| (init, local.els)),
|
2021-03-25 13:29:11 -05:00
|
|
|
StmtKind::Item(..) => None,
|
2021-03-12 08:30:50 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-21 13:34:47 -06:00
|
|
|
#[allow(clippy::too_many_lines)]
|
|
|
|
fn never_loop_expr(expr: &Expr<'_>, ignore_ids: &mut Vec<HirId>, main_loop_id: HirId) -> NeverLoopResult {
|
2021-03-12 08:30:50 -06:00
|
|
|
match expr.kind {
|
2023-03-14 12:18:26 -05:00
|
|
|
ExprKind::Unary(_, e)
|
2021-04-08 10:50:13 -05:00
|
|
|
| ExprKind::Cast(e, _)
|
|
|
|
| ExprKind::Type(e, _)
|
|
|
|
| ExprKind::Field(e, _)
|
|
|
|
| ExprKind::AddrOf(_, _, e)
|
|
|
|
| ExprKind::Repeat(e, _)
|
2022-11-21 13:34:47 -06:00
|
|
|
| ExprKind::DropTemps(e) => never_loop_expr(e, ignore_ids, main_loop_id),
|
|
|
|
ExprKind::Let(let_expr) => never_loop_expr(let_expr.init, ignore_ids, main_loop_id),
|
|
|
|
ExprKind::Array(es) | ExprKind::Tup(es) => never_loop_expr_all(&mut es.iter(), ignore_ids, main_loop_id),
|
|
|
|
ExprKind::MethodCall(_, receiver, es, _) => never_loop_expr_all(
|
|
|
|
&mut std::iter::once(receiver).chain(es.iter()),
|
|
|
|
ignore_ids,
|
|
|
|
main_loop_id,
|
|
|
|
),
|
2022-06-16 10:39:06 -05:00
|
|
|
ExprKind::Struct(_, fields, base) => {
|
2022-11-21 13:34:47 -06:00
|
|
|
let fields = never_loop_expr_all(&mut fields.iter().map(|f| f.expr), ignore_ids, main_loop_id);
|
2022-06-16 10:39:06 -05:00
|
|
|
if let Some(base) = base {
|
2023-02-25 18:08:29 -06:00
|
|
|
combine_seq(fields, never_loop_expr(base, ignore_ids, main_loop_id))
|
2022-06-16 10:39:06 -05:00
|
|
|
} else {
|
|
|
|
fields
|
|
|
|
}
|
|
|
|
},
|
2022-11-21 13:34:47 -06:00
|
|
|
ExprKind::Call(e, es) => never_loop_expr_all(&mut once(e).chain(es.iter()), ignore_ids, main_loop_id),
|
2021-04-08 10:50:13 -05:00
|
|
|
ExprKind::Binary(_, e1, e2)
|
|
|
|
| ExprKind::Assign(e1, e2, _)
|
|
|
|
| ExprKind::AssignOp(_, e1, e2)
|
2022-11-21 13:34:47 -06:00
|
|
|
| ExprKind::Index(e1, e2) => never_loop_expr_all(&mut [e1, e2].iter().copied(), ignore_ids, main_loop_id),
|
2021-04-08 10:50:13 -05:00
|
|
|
ExprKind::Loop(b, _, _, _) => {
|
2021-03-12 08:30:50 -06:00
|
|
|
// Break can come from the inner loop so remove them.
|
2022-11-21 13:34:47 -06:00
|
|
|
absorb_break(never_loop_block(b, ignore_ids, main_loop_id))
|
2021-03-12 08:30:50 -06:00
|
|
|
},
|
2021-08-08 09:49:13 -05:00
|
|
|
ExprKind::If(e, e2, e3) => {
|
2022-11-21 13:34:47 -06:00
|
|
|
let e1 = never_loop_expr(e, ignore_ids, main_loop_id);
|
|
|
|
let e2 = never_loop_expr(e2, ignore_ids, main_loop_id);
|
|
|
|
let e3 = e3.as_ref().map_or(NeverLoopResult::Otherwise, |e| {
|
|
|
|
never_loop_expr(e, ignore_ids, main_loop_id)
|
|
|
|
});
|
2023-02-25 18:08:29 -06:00
|
|
|
combine_seq(e1, combine_branches(e2, e3, ignore_ids))
|
2021-03-12 08:30:50 -06:00
|
|
|
},
|
2021-04-08 10:50:13 -05:00
|
|
|
ExprKind::Match(e, arms, _) => {
|
2022-11-21 13:34:47 -06:00
|
|
|
let e = never_loop_expr(e, ignore_ids, main_loop_id);
|
2021-03-12 08:30:50 -06:00
|
|
|
if arms.is_empty() {
|
|
|
|
e
|
|
|
|
} else {
|
2022-11-21 13:34:47 -06:00
|
|
|
let arms = never_loop_expr_branch(&mut arms.iter().map(|a| a.body), ignore_ids, main_loop_id);
|
2021-03-12 08:30:50 -06:00
|
|
|
combine_seq(e, arms)
|
|
|
|
}
|
|
|
|
},
|
2022-11-21 13:34:47 -06:00
|
|
|
ExprKind::Block(b, l) => {
|
|
|
|
if l.is_some() {
|
|
|
|
ignore_ids.push(b.hir_id);
|
|
|
|
}
|
|
|
|
let ret = never_loop_block(b, ignore_ids, main_loop_id);
|
2023-02-25 18:08:29 -06:00
|
|
|
if l.is_some() {
|
|
|
|
ignore_ids.pop();
|
|
|
|
}
|
|
|
|
match ret {
|
|
|
|
NeverLoopResult::IgnoreUntilEnd(a) if a == b.hir_id => NeverLoopResult::Otherwise,
|
|
|
|
_ => ret,
|
|
|
|
}
|
2022-11-21 13:34:47 -06:00
|
|
|
},
|
2021-03-12 08:30:50 -06:00
|
|
|
ExprKind::Continue(d) => {
|
|
|
|
let id = d
|
|
|
|
.target_id
|
|
|
|
.expect("target ID can only be missing in the presence of compilation errors");
|
|
|
|
if id == main_loop_id {
|
|
|
|
NeverLoopResult::MayContinueMainLoop
|
|
|
|
} else {
|
|
|
|
NeverLoopResult::AlwaysBreak
|
|
|
|
}
|
|
|
|
},
|
2022-11-21 13:34:47 -06:00
|
|
|
// checks if break targets a block instead of a loop
|
|
|
|
ExprKind::Break(Destination { target_id: Ok(t), .. }, e) if ignore_ids.contains(&t) => e
|
2023-02-25 18:08:29 -06:00
|
|
|
.map_or(NeverLoopResult::IgnoreUntilEnd(t), |e| {
|
|
|
|
never_loop_expr(e, ignore_ids, main_loop_id)
|
2022-11-21 13:34:47 -06:00
|
|
|
}),
|
2021-08-08 09:49:13 -05:00
|
|
|
ExprKind::Break(_, e) | ExprKind::Ret(e) => e.as_ref().map_or(NeverLoopResult::AlwaysBreak, |e| {
|
2022-11-21 13:34:47 -06:00
|
|
|
combine_seq(
|
|
|
|
never_loop_expr(e, ignore_ids, main_loop_id),
|
|
|
|
NeverLoopResult::AlwaysBreak,
|
|
|
|
)
|
2021-03-12 08:30:50 -06:00
|
|
|
}),
|
2021-04-08 10:50:13 -05:00
|
|
|
ExprKind::InlineAsm(asm) => asm
|
2021-03-12 08:30:50 -06:00
|
|
|
.operands
|
|
|
|
.iter()
|
|
|
|
.map(|(o, _)| match o {
|
2022-05-05 09:12:52 -05:00
|
|
|
InlineAsmOperand::In { expr, .. } | InlineAsmOperand::InOut { expr, .. } => {
|
2022-11-21 13:34:47 -06:00
|
|
|
never_loop_expr(expr, ignore_ids, main_loop_id)
|
2022-05-05 09:12:52 -05:00
|
|
|
},
|
2022-11-21 13:34:47 -06:00
|
|
|
InlineAsmOperand::Out { expr, .. } => {
|
|
|
|
never_loop_expr_all(&mut expr.iter().copied(), ignore_ids, main_loop_id)
|
2021-03-12 08:30:50 -06:00
|
|
|
},
|
2022-11-21 13:34:47 -06:00
|
|
|
InlineAsmOperand::SplitInOut { in_expr, out_expr, .. } => never_loop_expr_all(
|
|
|
|
&mut once(*in_expr).chain(out_expr.iter().copied()),
|
|
|
|
ignore_ids,
|
|
|
|
main_loop_id,
|
|
|
|
),
|
2022-02-28 18:50:56 -06:00
|
|
|
InlineAsmOperand::Const { .. }
|
|
|
|
| InlineAsmOperand::SymFn { .. }
|
|
|
|
| InlineAsmOperand::SymStatic { .. } => NeverLoopResult::Otherwise,
|
2021-03-12 08:30:50 -06:00
|
|
|
})
|
2023-02-25 18:08:29 -06:00
|
|
|
.fold(NeverLoopResult::Otherwise, combine_seq),
|
2022-06-16 10:39:06 -05:00
|
|
|
ExprKind::Yield(_, _)
|
2022-06-11 14:25:25 -05:00
|
|
|
| ExprKind::Closure { .. }
|
2021-03-12 08:30:50 -06:00
|
|
|
| ExprKind::Path(_)
|
|
|
|
| ExprKind::ConstBlock(_)
|
|
|
|
| ExprKind::Lit(_)
|
2023-02-22 20:46:49 -06:00
|
|
|
| ExprKind::Err(_) => NeverLoopResult::Otherwise,
|
2021-03-12 08:30:50 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-21 13:34:47 -06:00
|
|
|
fn never_loop_expr_all<'a, T: Iterator<Item = &'a Expr<'a>>>(
|
|
|
|
es: &mut T,
|
|
|
|
ignore_ids: &mut Vec<HirId>,
|
|
|
|
main_loop_id: HirId,
|
|
|
|
) -> NeverLoopResult {
|
|
|
|
es.map(|e| never_loop_expr(e, ignore_ids, main_loop_id))
|
2023-02-25 18:08:29 -06:00
|
|
|
.fold(NeverLoopResult::Otherwise, combine_seq)
|
2021-03-12 08:30:50 -06:00
|
|
|
}
|
|
|
|
|
2022-11-21 13:34:47 -06:00
|
|
|
fn never_loop_expr_branch<'a, T: Iterator<Item = &'a Expr<'a>>>(
|
|
|
|
e: &mut T,
|
|
|
|
ignore_ids: &mut Vec<HirId>,
|
|
|
|
main_loop_id: HirId,
|
|
|
|
) -> NeverLoopResult {
|
2023-02-25 18:08:29 -06:00
|
|
|
e.fold(NeverLoopResult::AlwaysBreak, |a, b| {
|
|
|
|
combine_branches(a, never_loop_expr(b, ignore_ids, main_loop_id), ignore_ids)
|
|
|
|
})
|
2021-03-12 08:30:50 -06:00
|
|
|
}
|
2021-08-12 04:16:25 -05:00
|
|
|
|
|
|
|
fn for_to_if_let_sugg(cx: &LateContext<'_>, iterator: &Expr<'_>, pat: &Pat<'_>) -> String {
|
|
|
|
let pat_snippet = snippet(cx, pat.span, "_");
|
|
|
|
let iter_snippet = make_iterator_snippet(cx, iterator, &mut Applicability::Unspecified);
|
|
|
|
|
2022-10-06 02:44:38 -05:00
|
|
|
format!("if let Some({pat_snippet}) = {iter_snippet}.next()")
|
2021-08-12 04:16:25 -05:00
|
|
|
}
|