Skip to content

Commit 205aa88

Browse files
committed
Fix while_let_on_iterator
When the iterator is one field within a local correctly check for usages of the field
1 parent fe75faa commit 205aa88

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

clippy_lints/src/loops/while_let_on_iterator.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ fn is_expr_same_child_or_parent_field(cx: &LateContext<'_>, expr: &Expr<'_>, fie
146146
match expr.kind {
147147
ExprKind::Field(base, name) => {
148148
if let Some((head_field, tail_fields)) = fields.split_first() {
149-
if name.name == *head_field && is_expr_same_field(cx, base, fields, path_res) {
149+
if name.name == *head_field && is_expr_same_field(cx, base, tail_fields, path_res) {
150150
return true;
151151
}
152152
// Check if the expression is a parent field

tests/ui/while_let_on_iterator.fixed

+9
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,15 @@ fn issue7510() {
357357
println!("{}", it.0.next().unwrap());
358358
}
359359

360+
fn exact_match_with_single_field() {
361+
struct S<T>(T);
362+
let mut s = S(0..10);
363+
// Don't lint. `s.0` is used inside the loop.
364+
while let Some(_) = s.0.next() {
365+
let _ = &mut s.0;
366+
}
367+
}
368+
360369
fn main() {
361370
let mut it = 0..20;
362371
for _ in it {

tests/ui/while_let_on_iterator.rs

+9
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,15 @@ fn issue7510() {
357357
println!("{}", it.0.next().unwrap());
358358
}
359359

360+
fn exact_match_with_single_field() {
361+
struct S<T>(T);
362+
let mut s = S(0..10);
363+
// Don't lint. `s.0` is used inside the loop.
364+
while let Some(_) = s.0.next() {
365+
let _ = &mut s.0;
366+
}
367+
}
368+
360369
fn main() {
361370
let mut it = 0..20;
362371
while let Some(..) = it.next() {

tests/ui/while_let_on_iterator.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ LL | while let Some(x) = it.0.next() {
123123
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for x in &mut *it.0`
124124

125125
error: this loop could be written as a `for` loop
126-
--> $DIR/while_let_on_iterator.rs:362:5
126+
--> $DIR/while_let_on_iterator.rs:371:5
127127
|
128128
LL | while let Some(..) = it.next() {
129129
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for _ in it`

0 commit comments

Comments
 (0)