Skip to content

Commit d7ffaab

Browse files
authored
Merge pull request #2968 from phansch/first_an_ICE_and_then_some_ice_cream
Fix ICE with 'while let Some(..) = x.iter()'
2 parents 4dbc62b + 946340a commit d7ffaab

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

clippy_lints/src/loops.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -488,12 +488,18 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
488488
let iter_expr = &method_args[0];
489489
let lhs_constructor = last_path_segment(qpath);
490490
if method_path.ident.name == "next" && match_trait_method(cx, match_expr, &paths::ITERATOR)
491-
&& lhs_constructor.ident.name == "Some" && !is_refutable(cx, &pat_args[0])
492-
&& !is_iterator_used_after_while_let(cx, iter_expr)
493-
&& !is_nested(cx, expr, &method_args[0])
491+
&& lhs_constructor.ident.name == "Some" && (
492+
pat_args.is_empty()
493+
|| !is_refutable(cx, &pat_args[0])
494+
&& !is_iterator_used_after_while_let(cx, iter_expr)
495+
&& !is_nested(cx, expr, &method_args[0]))
494496
{
495497
let iterator = snippet(cx, method_args[0].span, "_");
496-
let loop_var = snippet(cx, pat_args[0].span, "_");
498+
let loop_var = if pat_args.is_empty() {
499+
"_".to_string()
500+
} else {
501+
snippet(cx, pat_args[0].span, "_").into_owned()
502+
};
497503
span_lint_and_sugg(
498504
cx,
499505
WHILE_LET_ON_ITERATOR,

tests/ui/while_loop.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,4 +201,13 @@ fn refutable() {
201201
while let Some(&value) = values.iter().next() {
202202
values.remove(&value);
203203
}
204+
205+
// This should not cause an ICE and suggest:
206+
//
207+
// for _ in values.iter() {}
208+
//
209+
// See #2965
210+
while let Some(..) = values.iter().next() {
211+
values.remove(&1);
212+
}
204213
}

tests/ui/while_loop.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,11 @@ error: this loop could be written as a `for` loop
105105
183 | while let Some(v) = y.next() { // use a for loop here
106106
| ^^^^^^^^ help: try: `for v in y { .. }`
107107

108-
error: aborting due to 11 previous errors
108+
error: this loop could be written as a `for` loop
109+
--> $DIR/while_loop.rs:210:26
110+
|
111+
210 | while let Some(..) = values.iter().next() {
112+
| ^^^^^^^^^^^^^^^^^^^^ help: try: `for _ in values.iter() { .. }`
113+
114+
error: aborting due to 12 previous errors
109115

0 commit comments

Comments
 (0)