Skip to content

Commit 71f2de9

Browse files
committed
Auto merge of rust-lang#9002 - andylizi:fix-never-loop, r=Manishearth
Fix false positive for `never_loop` struct expression fields Fixes rust-lang#9001. changelog: [`never_loop`]: Now checks for `continue` in struct expression
2 parents 844c06a + a8370d4 commit 71f2de9

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

clippy_lints/src/loops/never_loop.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,20 @@ fn never_loop_expr(expr: &Expr<'_>, main_loop_id: HirId) -> NeverLoopResult {
117117
| ExprKind::Type(e, _)
118118
| ExprKind::Field(e, _)
119119
| ExprKind::AddrOf(_, _, e)
120-
| ExprKind::Struct(_, _, Some(e))
121120
| ExprKind::Repeat(e, _)
122121
| ExprKind::DropTemps(e) => never_loop_expr(e, main_loop_id),
123122
ExprKind::Let(let_expr) => never_loop_expr(let_expr.init, main_loop_id),
124123
ExprKind::Array(es) | ExprKind::MethodCall(_, es, _) | ExprKind::Tup(es) => {
125124
never_loop_expr_all(&mut es.iter(), main_loop_id)
126125
},
126+
ExprKind::Struct(_, fields, base) => {
127+
let fields = never_loop_expr_all(&mut fields.iter().map(|f| f.expr), main_loop_id);
128+
if let Some(base) = base {
129+
combine_both(fields, never_loop_expr(base, main_loop_id))
130+
} else {
131+
fields
132+
}
133+
},
127134
ExprKind::Call(e, es) => never_loop_expr_all(&mut once(e).chain(es.iter()), main_loop_id),
128135
ExprKind::Binary(_, e1, e2)
129136
| ExprKind::Assign(e1, e2, _)
@@ -180,8 +187,7 @@ fn never_loop_expr(expr: &Expr<'_>, main_loop_id: HirId) -> NeverLoopResult {
180187
| InlineAsmOperand::SymStatic { .. } => NeverLoopResult::Otherwise,
181188
})
182189
.fold(NeverLoopResult::Otherwise, combine_both),
183-
ExprKind::Struct(_, _, None)
184-
| ExprKind::Yield(_, _)
190+
ExprKind::Yield(_, _)
185191
| ExprKind::Closure(_, _, _, _, _)
186192
| ExprKind::Path(_)
187193
| ExprKind::ConstBlock(_)

tests/ui/never_loop.rs

+17
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,23 @@ pub fn test16() {
186186
}
187187
}
188188

189+
// Issue #9001: `continue` in struct expression fields
190+
pub fn test17() {
191+
struct Foo {
192+
f: (),
193+
}
194+
195+
let mut n = 0;
196+
let _ = loop {
197+
break Foo {
198+
f: if n < 5 {
199+
n += 1;
200+
continue;
201+
},
202+
};
203+
};
204+
}
205+
189206
fn main() {
190207
test1();
191208
test2();

0 commit comments

Comments
 (0)