Auto merge of #9002 - andylizi:fix-never-loop, r=Manishearth

Fix false positive for `never_loop` struct expression fields

Fixes #9001.

changelog: [`never_loop`]: Now checks for `continue` in struct expression
This commit is contained in:
bors 2022-06-15 18:36:32 +00:00
commit 71f2de96ee
2 changed files with 26 additions and 3 deletions

View file

@ -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, _)
@ -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(_)

View file

@ -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();