Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[never_loop]: Fix FP with let..else statements. #9496

Merged
merged 1 commit into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions clippy_lints/src/loops/never_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub(super) fn check(
}
}

#[derive(Copy, Clone)]
enum NeverLoopResult {
// A break/return always get triggered but not necessarily for the main loop.
AlwaysBreak,
Expand All @@ -51,8 +52,8 @@ enum NeverLoopResult {
}

#[must_use]
fn absorb_break(arg: &NeverLoopResult) -> NeverLoopResult {
match *arg {
fn absorb_break(arg: NeverLoopResult) -> NeverLoopResult {
match arg {
NeverLoopResult::AlwaysBreak | NeverLoopResult::Otherwise => NeverLoopResult::Otherwise,
NeverLoopResult::MayContinueMainLoop => NeverLoopResult::MayContinueMainLoop,
}
Expand Down Expand Up @@ -92,19 +93,29 @@ fn combine_branches(b1: NeverLoopResult, b2: NeverLoopResult) -> NeverLoopResult
}

fn never_loop_block(block: &Block<'_>, main_loop_id: HirId) -> NeverLoopResult {
let mut iter = block.stmts.iter().filter_map(stmt_to_expr).chain(block.expr);
let mut iter = block
.stmts
.iter()
.filter_map(stmt_to_expr)
.chain(block.expr.map(|expr| (expr, None)));
never_loop_expr_seq(&mut iter, main_loop_id)
}

fn never_loop_expr_seq<'a, T: Iterator<Item = &'a Expr<'a>>>(es: &mut T, main_loop_id: HirId) -> NeverLoopResult {
es.map(|e| never_loop_expr(e, main_loop_id))
.fold(NeverLoopResult::Otherwise, combine_seq)
fn never_loop_expr_seq<'a, T: Iterator<Item = (&'a Expr<'a>, Option<&'a Block<'a>>)>>(
es: &mut T,
main_loop_id: HirId,
) -> NeverLoopResult {
es.map(|(e, els)| {
let e = never_loop_expr(e, main_loop_id);
els.map_or(e, |els| combine_branches(e, never_loop_block(els, main_loop_id)))
})
.fold(NeverLoopResult::Otherwise, combine_seq)
}

fn stmt_to_expr<'tcx>(stmt: &Stmt<'tcx>) -> Option<&'tcx Expr<'tcx>> {
fn stmt_to_expr<'tcx>(stmt: &Stmt<'tcx>) -> Option<(&'tcx Expr<'tcx>, Option<&'tcx Block<'tcx>>)> {
match stmt.kind {
StmtKind::Semi(e, ..) | StmtKind::Expr(e, ..) => Some(e),
StmtKind::Local(local) => local.init,
StmtKind::Semi(e, ..) | StmtKind::Expr(e, ..) => Some((e, None)),
StmtKind::Local(local) => local.init.map(|init| (init, local.els)),
StmtKind::Item(..) => None,
}
}
Expand Down Expand Up @@ -139,7 +150,7 @@ fn never_loop_expr(expr: &Expr<'_>, main_loop_id: HirId) -> NeverLoopResult {
| ExprKind::Index(e1, e2) => never_loop_expr_all(&mut [e1, e2].iter().copied(), main_loop_id),
ExprKind::Loop(b, _, _, _) => {
// Break can come from the inner loop so remove them.
absorb_break(&never_loop_block(b, main_loop_id))
absorb_break(never_loop_block(b, main_loop_id))
},
ExprKind::If(e, e2, e3) => {
let e1 = never_loop_expr(e, main_loop_id);
Expand Down
27 changes: 27 additions & 0 deletions tests/ui/never_loop.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![feature(let_else)]
#![allow(
clippy::single_match,
unused_assignments,
Expand Down Expand Up @@ -203,6 +204,32 @@ pub fn test17() {
};
}

// Issue #9356: `continue` in else branch of let..else
pub fn test18() {
let x = Some(0);
let y = 0;
// might loop
let _ = loop {
let Some(x) = x else {
if y > 0 {
continue;
} else {
return;
}
};

break x;
};
// never loops
let _ = loop {
let Some(x) = x else {
return;
};

break x;
};
}

fn main() {
test1();
test2();
Expand Down
33 changes: 23 additions & 10 deletions tests/ui/never_loop.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: this loop never actually loops
--> $DIR/never_loop.rs:10:5
--> $DIR/never_loop.rs:11:5
|
LL | / loop {
LL | | // clippy::never_loop
Expand All @@ -13,7 +13,7 @@ LL | | }
= note: `#[deny(clippy::never_loop)]` on by default

error: this loop never actually loops
--> $DIR/never_loop.rs:32:5
--> $DIR/never_loop.rs:33:5
|
LL | / loop {
LL | | // never loops
Expand All @@ -23,7 +23,7 @@ LL | | }
| |_____^

error: this loop never actually loops
--> $DIR/never_loop.rs:52:5
--> $DIR/never_loop.rs:53:5
|
LL | / loop {
LL | | // never loops
Expand All @@ -35,7 +35,7 @@ LL | | }
| |_____^

error: this loop never actually loops
--> $DIR/never_loop.rs:54:9
--> $DIR/never_loop.rs:55:9
|
LL | / while i == 0 {
LL | | // never loops
Expand All @@ -44,7 +44,7 @@ LL | | }
| |_________^

error: this loop never actually loops
--> $DIR/never_loop.rs:66:9
--> $DIR/never_loop.rs:67:9
|
LL | / loop {
LL | | // never loops
Expand All @@ -56,7 +56,7 @@ LL | | }
| |_________^

error: this loop never actually loops
--> $DIR/never_loop.rs:102:5
--> $DIR/never_loop.rs:103:5
|
LL | / while let Some(y) = x {
LL | | // never loops
Expand All @@ -65,7 +65,7 @@ LL | | }
| |_____^

error: this loop never actually loops
--> $DIR/never_loop.rs:109:5
--> $DIR/never_loop.rs:110:5
|
LL | / for x in 0..10 {
LL | | // never loops
Expand All @@ -82,7 +82,7 @@ LL | if let Some(x) = (0..10).next() {
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

error: this loop never actually loops
--> $DIR/never_loop.rs:157:5
--> $DIR/never_loop.rs:158:5
|
LL | / 'outer: while a {
LL | | // never loops
Expand All @@ -94,12 +94,25 @@ LL | | }
| |_____^

error: this loop never actually loops
--> $DIR/never_loop.rs:172:9
--> $DIR/never_loop.rs:173:9
|
LL | / while false {
LL | | break 'label;
LL | | }
| |_________^

error: aborting due to 9 previous errors
error: this loop never actually loops
--> $DIR/never_loop.rs:224:13
|
LL | let _ = loop {
| _____________^
LL | | let Some(x) = x else {
LL | | return;
LL | | };
LL | |
LL | | break x;
LL | | };
| |_____^

error: aborting due to 10 previous errors