mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-23 13:13:34 +00:00
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:
parent
fe75faa6ee
commit
205aa88921
4 changed files with 20 additions and 2 deletions
|
@ -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
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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`
|
||||
|
|
Loading…
Reference in a new issue