Skip to content

Commit 91e4214

Browse files
committed
Don't lint while_let_loop when drop order would change
1 parent 02977cb commit 91e4214

File tree

2 files changed

+78
-56
lines changed

2 files changed

+78
-56
lines changed

clippy_lints/src/loops/while_let_loop.rs

Lines changed: 52 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2,71 +2,62 @@ use super::WHILE_LET_LOOP;
22
use clippy_utils::diagnostics::span_lint_and_sugg;
33
use clippy_utils::higher;
44
use clippy_utils::source::snippet_with_applicability;
5+
use clippy_utils::ty::type_needs_ordered_drop;
6+
use clippy_utils::visitors::any_temporaries_need_ordered_drop;
57
use rustc_errors::Applicability;
6-
use rustc_hir::{Block, Expr, ExprKind, MatchSource, Pat, StmtKind};
7-
use rustc_lint::{LateContext, LintContext};
8-
use rustc_middle::lint::in_external_macro;
8+
use rustc_hir::{Block, Expr, ExprKind, Local, MatchSource, Pat, StmtKind};
9+
use rustc_lint::LateContext;
910

1011
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, loop_block: &'tcx Block<'_>) {
11-
// extract the expression from the first statement (if any) in a block
12-
let inner_stmt_expr = extract_expr_from_first_stmt(loop_block);
13-
// or extract the first expression (if any) from the block
14-
if let Some(inner) = inner_stmt_expr.or_else(|| extract_first_expr(loop_block)) {
15-
if let Some(higher::IfLet {
16-
let_pat,
17-
let_expr,
18-
if_else: Some(if_else),
19-
..
20-
}) = higher::IfLet::hir(cx, inner)
21-
{
22-
if is_simple_break_expr(if_else) {
23-
could_be_while_let(cx, expr, let_pat, let_expr);
12+
let (init, has_trailing_exprs) = match (loop_block.stmts, loop_block.expr) {
13+
([stmt, stmts @ ..], expr) => {
14+
let has_trailing_exprs = !stmts.is_empty() || expr.is_some();
15+
match stmt.kind {
16+
StmtKind::Local(&Local { init: Some(e), .. }) => (e, has_trailing_exprs),
17+
StmtKind::Semi(e) | StmtKind::Expr(e) => (e, has_trailing_exprs),
18+
// Technically we could lint if this starts with an item, but the suggestion becomes complicated.
19+
_ => return,
2420
}
25-
}
26-
27-
if let ExprKind::Match(matchexpr, arms, MatchSource::Normal) = inner.kind {
28-
if arms.len() == 2
29-
&& arms[0].guard.is_none()
30-
&& arms[1].guard.is_none()
31-
&& is_simple_break_expr(arms[1].body)
32-
{
33-
could_be_while_let(cx, expr, arms[0].pat, matchexpr);
34-
}
35-
}
36-
}
37-
}
21+
},
22+
([], Some(e)) => (e, false),
23+
_ => return,
24+
};
3825

39-
/// If a block begins with a statement (possibly a `let` binding) and has an
40-
/// expression, return it.
41-
fn extract_expr_from_first_stmt<'tcx>(block: &Block<'tcx>) -> Option<&'tcx Expr<'tcx>> {
42-
if let Some(first_stmt) = block.stmts.get(0) {
43-
if let StmtKind::Local(local) = first_stmt.kind {
44-
return local.init;
45-
}
26+
if let Some(if_let) = higher::IfLet::hir(cx, init)
27+
&& let Some(else_expr) = if_let.if_else
28+
&& is_simple_break_expr(else_expr)
29+
{
30+
could_be_while_let(cx, expr, if_let.let_pat, if_let.let_expr, has_trailing_exprs);
31+
} else if let ExprKind::Match(scrutinee, [arm1, arm2], MatchSource::Normal) = init.kind
32+
&& arm1.guard.is_none()
33+
&& arm2.guard.is_none()
34+
&& is_simple_break_expr(arm2.body)
35+
{
36+
could_be_while_let(cx, expr, arm1.pat, scrutinee, has_trailing_exprs);
4637
}
47-
None
4838
}
4939

50-
/// If a block begins with an expression (with or without semicolon), return it.
51-
fn extract_first_expr<'tcx>(block: &Block<'tcx>) -> Option<&'tcx Expr<'tcx>> {
52-
match block.expr {
53-
Some(expr) if block.stmts.is_empty() => Some(expr),
54-
None if !block.stmts.is_empty() => match block.stmts[0].kind {
55-
StmtKind::Expr(expr) | StmtKind::Semi(expr) => Some(expr),
56-
StmtKind::Local(..) | StmtKind::Item(..) => None,
57-
},
58-
_ => None,
59-
}
40+
/// Returns `true` if expr contains a single break expression without a label or eub-expression.
41+
fn is_simple_break_expr(e: &Expr<'_>) -> bool {
42+
matches!(peel_blocks(e).kind, ExprKind::Break(dest, None) if dest.label.is_none())
6043
}
6144

62-
/// Returns `true` if expr contains a single break expr without destination label
63-
/// and
64-
/// passed expression. The expression may be within a block.
65-
fn is_simple_break_expr(expr: &Expr<'_>) -> bool {
66-
match expr.kind {
67-
ExprKind::Break(dest, ref passed_expr) if dest.label.is_none() && passed_expr.is_none() => true,
68-
ExprKind::Block(b, _) => extract_first_expr(b).map_or(false, is_simple_break_expr),
69-
_ => false,
45+
/// Removes any blocks containing only a single expression.
46+
fn peel_blocks<'tcx>(e: &'tcx Expr<'tcx>) -> &'tcx Expr<'tcx> {
47+
if let ExprKind::Block(b, _) = e.kind {
48+
match (b.stmts, b.expr) {
49+
([s], None) => {
50+
if let StmtKind::Expr(e) | StmtKind::Semi(e) = s.kind {
51+
peel_blocks(e)
52+
} else {
53+
e
54+
}
55+
},
56+
([], Some(e)) => peel_blocks(e),
57+
_ => e,
58+
}
59+
} else {
60+
e
7061
}
7162
}
7263

@@ -75,8 +66,13 @@ fn could_be_while_let<'tcx>(
7566
expr: &'tcx Expr<'_>,
7667
let_pat: &'tcx Pat<'_>,
7768
let_expr: &'tcx Expr<'_>,
69+
has_trailing_exprs: bool,
7870
) {
79-
if in_external_macro(cx.sess(), expr.span) {
71+
if has_trailing_exprs
72+
&& (type_needs_ordered_drop(cx, cx.typeck_results().expr_ty(let_expr))
73+
|| any_temporaries_need_ordered_drop(cx, let_expr))
74+
{
75+
// Switching to a `while let` loop will extend the lifetime of some values.
8076
return;
8177
}
8278

tests/ui/while_let_loop.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,29 @@ fn issue1948() {
117117
}
118118
};
119119
}
120+
121+
fn issue_7913(m: &std::sync::Mutex<Vec<u32>>) {
122+
// Don't lint. The lock shouldn't be held while printing.
123+
loop {
124+
let x = if let Some(x) = m.lock().unwrap().pop() {
125+
x
126+
} else {
127+
break;
128+
};
129+
130+
println!("{}", x);
131+
}
132+
}
133+
134+
fn issue_5715(mut m: core::cell::RefCell<Option<u32>>) {
135+
// Don't lint. The temporary from `borrow_mut` must be dropped before overwriting the `RefCell`.
136+
loop {
137+
let x = if let &mut Some(x) = &mut *m.borrow_mut() {
138+
x
139+
} else {
140+
break;
141+
};
142+
143+
m = core::cell::RefCell::new(Some(x + 1));
144+
}
145+
}

0 commit comments

Comments
 (0)