Skip to content

Commit

Permalink
Auto merge of #9002 - andylizi:fix-never-loop, r=Manishearth
Browse files Browse the repository at this point in the history
Fix false positive for `never_loop` struct expression fields

Fixes #9001.

changelog: [`never_loop`]: Now checks for `continue` in struct expression
  • Loading branch information
bors committed Jun 15, 2022
2 parents 844c06a + a8370d4 commit 71f2de9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
12 changes: 9 additions & 3 deletions clippy_lints/src/loops/never_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,20 @@ fn never_loop_expr(expr: &Expr<'_>, main_loop_id: HirId) -> NeverLoopResult {
| ExprKind::Type(e, _)
| ExprKind::Field(e, _)
| ExprKind::AddrOf(_, _, e)
| ExprKind::Struct(_, _, Some(e))
| ExprKind::Repeat(e, _)
| ExprKind::DropTemps(e) => never_loop_expr(e, main_loop_id),
ExprKind::Let(let_expr) => never_loop_expr(let_expr.init, main_loop_id),
ExprKind::Array(es) | ExprKind::MethodCall(_, es, _) | ExprKind::Tup(es) => {
never_loop_expr_all(&mut es.iter(), main_loop_id)
},
ExprKind::Struct(_, fields, base) => {
let fields = never_loop_expr_all(&mut fields.iter().map(|f| f.expr), main_loop_id);
if let Some(base) = base {
combine_both(fields, never_loop_expr(base, main_loop_id))
} else {
fields
}
},
ExprKind::Call(e, es) => never_loop_expr_all(&mut once(e).chain(es.iter()), main_loop_id),
ExprKind::Binary(_, e1, e2)
| ExprKind::Assign(e1, e2, _)
Expand Down Expand Up @@ -180,8 +187,7 @@ fn never_loop_expr(expr: &Expr<'_>, main_loop_id: HirId) -> NeverLoopResult {
| InlineAsmOperand::SymStatic { .. } => NeverLoopResult::Otherwise,
})
.fold(NeverLoopResult::Otherwise, combine_both),
ExprKind::Struct(_, _, None)
| ExprKind::Yield(_, _)
ExprKind::Yield(_, _)
| ExprKind::Closure(_, _, _, _, _)
| ExprKind::Path(_)
| ExprKind::ConstBlock(_)
Expand Down
17 changes: 17 additions & 0 deletions tests/ui/never_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,23 @@ pub fn test16() {
}
}

// Issue #9001: `continue` in struct expression fields
pub fn test17() {
struct Foo {
f: (),
}

let mut n = 0;
let _ = loop {
break Foo {
f: if n < 5 {
n += 1;
continue;
},
};
};
}

fn main() {
test1();
test2();
Expand Down

0 comments on commit 71f2de9

Please sign in to comment.