Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ICE with 'while let Some(..) = x.iter()' #2968

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 10 additions & 4 deletions clippy_lints/src/loops.rs
Expand Up @@ -488,12 +488,18 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
let iter_expr = &method_args[0];
let lhs_constructor = last_path_segment(qpath);
if method_path.ident.name == "next" && match_trait_method(cx, match_expr, &paths::ITERATOR)
&& lhs_constructor.ident.name == "Some" && !is_refutable(cx, &pat_args[0])
&& !is_iterator_used_after_while_let(cx, iter_expr)
&& !is_nested(cx, expr, &method_args[0])
&& lhs_constructor.ident.name == "Some" && (
pat_args.is_empty()
|| !is_refutable(cx, &pat_args[0])
&& !is_iterator_used_after_while_let(cx, iter_expr)
&& !is_nested(cx, expr, &method_args[0]))
{
let iterator = snippet(cx, method_args[0].span, "_");
let loop_var = snippet(cx, pat_args[0].span, "_");
let loop_var = if pat_args.is_empty() {
"_".to_string()
} else {
snippet(cx, pat_args[0].span, "_").into_owned()
};
span_lint_and_sugg(
cx,
WHILE_LET_ON_ITERATOR,
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/while_loop.rs
Expand Up @@ -201,4 +201,13 @@ fn refutable() {
while let Some(&value) = values.iter().next() {
values.remove(&value);
}

// This should not cause an ICE and suggest:
//
// for _ in values.iter() {}
//
// See #2965
while let Some(..) = values.iter().next() {
values.remove(&1);
}
}
8 changes: 7 additions & 1 deletion tests/ui/while_loop.stderr
Expand Up @@ -105,5 +105,11 @@ error: this loop could be written as a `for` loop
183 | while let Some(v) = y.next() { // use a for loop here
| ^^^^^^^^ help: try: `for v in y { .. }`

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

error: aborting due to 12 previous errors