Fix while_let_on_iterator

Reborrow mutable references rather then take a reference to them.
This commit is contained in:
Jason Newcomb 2021-07-30 22:59:20 -04:00
parent f6a5889ffa
commit fe75faa6ee
No known key found for this signature in database
GPG key ID: DA59E8643A37ED06
4 changed files with 71 additions and 4 deletions

View file

@ -334,6 +334,29 @@ fn issue7249() {
x();
}
fn issue7510() {
let mut it = 0..10;
let it = &mut it;
// Needs to reborrow `it` as the binding isn't mutable
for x in &mut *it {
if x % 2 == 0 {
break;
}
}
println!("{}", it.next().unwrap());
struct S<T>(T);
let mut it = 0..10;
let it = S(&mut it);
// Needs to reborrow `it.0` as the binding isn't mutable
for x in &mut *it.0 {
if x % 2 == 0 {
break;
}
}
println!("{}", it.0.next().unwrap());
}
fn main() {
let mut it = 0..20;
for _ in it {

View file

@ -334,6 +334,29 @@ fn issue7249() {
x();
}
fn issue7510() {
let mut it = 0..10;
let it = &mut it;
// Needs to reborrow `it` as the binding isn't mutable
while let Some(x) = it.next() {
if x % 2 == 0 {
break;
}
}
println!("{}", it.next().unwrap());
struct S<T>(T);
let mut it = 0..10;
let it = S(&mut it);
// Needs to reborrow `it.0` as the binding isn't mutable
while let Some(x) = it.0.next() {
if x % 2 == 0 {
break;
}
}
println!("{}", it.0.next().unwrap());
}
fn main() {
let mut it = 0..20;
while let Some(..) = it.next() {

View file

@ -111,10 +111,22 @@ LL | while let Some(x) = it.next() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for x in &mut it`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:339:5
--> $DIR/while_let_on_iterator.rs:341:5
|
LL | while let Some(x) = it.next() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for x in &mut *it`
error: this loop could be written as a `for` loop
--> $DIR/while_let_on_iterator.rs:352:5
|
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
|
LL | while let Some(..) = it.next() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `for _ in it`
error: aborting due to 19 previous errors
error: aborting due to 21 previous errors