Fix while_let_on_iterator

When the iterator is one field within a local correctly check for usages of the field
This commit is contained in:
Jason Newcomb 2021-07-31 09:32:54 -04:00
parent fe75faa6ee
commit 205aa88921
No known key found for this signature in database
GPG key ID: DA59E8643A37ED06
4 changed files with 20 additions and 2 deletions

View file

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

View file

@ -357,6 +357,15 @@ fn issue7510() {
println!("{}", it.0.next().unwrap());
}
fn exact_match_with_single_field() {
struct S<T>(T);
let mut s = S(0..10);
// Don't lint. `s.0` is used inside the loop.
while let Some(_) = s.0.next() {
let _ = &mut s.0;
}
}
fn main() {
let mut it = 0..20;
for _ in it {

View file

@ -357,6 +357,15 @@ fn issue7510() {
println!("{}", it.0.next().unwrap());
}
fn exact_match_with_single_field() {
struct S<T>(T);
let mut s = S(0..10);
// Don't lint. `s.0` is used inside the loop.
while let Some(_) = s.0.next() {
let _ = &mut s.0;
}
}
fn main() {
let mut it = 0..20;
while let Some(..) = it.next() {

View file

@ -123,7 +123,7 @@ LL | while let Some(x) = it.0.next() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for x in &mut *it.0`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:362:5
--> $DIR/while_let_on_iterator.rs:371:5
|
LL | while let Some(..) = it.next() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for _ in it`