Skip to content

Commit 7cf94ad

Browse files
committed
for_loop_over_fallibles: suggest while let loop
1 parent b661157 commit 7cf94ad

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

compiler/rustc_lint/src/for_loop_over_fallibles.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,16 @@ impl<'tcx> LateLintPass<'tcx> for ForLoopOverFallibles {
8484
"",
8585
Applicability::MaybeIncorrect
8686
);
87+
} else {
88+
warn.multipart_suggestion_verbose(
89+
format!("to check pattern in a loop use `while let`"),
90+
vec![
91+
// NB can't use `until` here because `expr.span` and `pat.span` have different syntax contexts
92+
(expr.span.with_hi(pat.span.lo()), format!("while let {var}(")),
93+
(pat.span.between(arg.span), format!(") = ")),
94+
],
95+
Applicability::MaybeIncorrect
96+
);
8797
}
8898

8999
warn.multipart_suggestion_verbose(

0 commit comments

Comments
 (0)