forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#115308 - chenyukang:yukang-fix-62387-iter-m…
…ut, r=davidtwco suggest iter_mut() where trying to modify elements from .iter() Fixes rust-lang#115259 Fixes rust-lang#62387
- Loading branch information
Showing
10 changed files
with
274 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// run-rustfix | ||
#![allow(unused_mut)] | ||
#![allow(dead_code)] | ||
|
||
pub trait Layer { | ||
fn process(&mut self) -> u32; | ||
} | ||
|
||
pub struct State { | ||
layers: Vec<Box<dyn Layer>>, | ||
} | ||
|
||
impl State { | ||
pub fn process(&mut self) -> u32 { | ||
self.layers.iter_mut().fold(0, |result, mut layer| result + layer.process()) | ||
//~^ ERROR cannot borrow `**layer` as mutable, as it is behind a `&` reference | ||
} | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// run-rustfix | ||
#![allow(unused_mut)] | ||
#![allow(dead_code)] | ||
|
||
pub trait Layer { | ||
fn process(&mut self) -> u32; | ||
} | ||
|
||
pub struct State { | ||
layers: Vec<Box<dyn Layer>>, | ||
} | ||
|
||
impl State { | ||
pub fn process(&mut self) -> u32 { | ||
self.layers.iter().fold(0, |result, mut layer| result + layer.process()) | ||
//~^ ERROR cannot borrow `**layer` as mutable, as it is behind a `&` reference | ||
} | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
error[E0596]: cannot borrow `**layer` as mutable, as it is behind a `&` reference | ||
--> $DIR/issue-115259-suggest-iter-mut.rs:15:65 | ||
| | ||
LL | self.layers.iter().fold(0, |result, mut layer| result + layer.process()) | ||
| --------- ^^^^^ `layer` is a `&` reference, so the data it refers to cannot be borrowed as mutable | ||
| | | ||
| consider changing this binding's type to be: `&mut Box<dyn Layer>` | ||
| | ||
help: you may want to use `iter_mut` here | ||
| | ||
LL | self.layers.iter_mut().fold(0, |result, mut layer| result + layer.process()) | ||
| ~~~~~~~~ | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0596`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// run-rustfix | ||
#![allow(unused_mut)] | ||
#![allow(dead_code)] | ||
use std::path::PathBuf; | ||
|
||
#[derive(Clone)] | ||
struct Container { | ||
things: Vec<PathBuf>, | ||
} | ||
|
||
impl Container { | ||
fn things(&mut self) -> &[PathBuf] { | ||
&self.things | ||
} | ||
} | ||
|
||
// contains containers | ||
struct ContainerContainer { | ||
contained: Vec<Container>, | ||
} | ||
|
||
impl ContainerContainer { | ||
fn contained(&self) -> &[Container] { | ||
&self.contained | ||
} | ||
|
||
fn all_the_things(&mut self) -> &[PathBuf] { | ||
let mut vec = self.contained.clone(); | ||
let _a = | ||
vec.iter_mut().flat_map(|container| container.things()).cloned().collect::<Vec<PathBuf>>(); | ||
//~^ ERROR cannot borrow `*container` as mutable, as it is behind a `&` reference | ||
unimplemented!(); | ||
} | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// run-rustfix | ||
#![allow(unused_mut)] | ||
#![allow(dead_code)] | ||
use std::path::PathBuf; | ||
|
||
#[derive(Clone)] | ||
struct Container { | ||
things: Vec<PathBuf>, | ||
} | ||
|
||
impl Container { | ||
fn things(&mut self) -> &[PathBuf] { | ||
&self.things | ||
} | ||
} | ||
|
||
// contains containers | ||
struct ContainerContainer { | ||
contained: Vec<Container>, | ||
} | ||
|
||
impl ContainerContainer { | ||
fn contained(&self) -> &[Container] { | ||
&self.contained | ||
} | ||
|
||
fn all_the_things(&mut self) -> &[PathBuf] { | ||
let mut vec = self.contained.clone(); | ||
let _a = | ||
vec.iter().flat_map(|container| container.things()).cloned().collect::<Vec<PathBuf>>(); | ||
//~^ ERROR cannot borrow `*container` as mutable, as it is behind a `&` reference | ||
unimplemented!(); | ||
} | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
error[E0596]: cannot borrow `*container` as mutable, as it is behind a `&` reference | ||
--> $DIR/issue-62387-suggest-iter-mut-2.rs:30:45 | ||
| | ||
LL | vec.iter().flat_map(|container| container.things()).cloned().collect::<Vec<PathBuf>>(); | ||
| --------- ^^^^^^^^^ `container` is a `&` reference, so the data it refers to cannot be borrowed as mutable | ||
| | | ||
| consider changing this binding's type to be: `&mut Container` | ||
| | ||
help: you may want to use `iter_mut` here | ||
| | ||
LL | vec.iter_mut().flat_map(|container| container.things()).cloned().collect::<Vec<PathBuf>>(); | ||
| ~~~~~~~~ | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0596`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// run-rustfix | ||
#![allow(unused_mut)] | ||
#![allow(dead_code)] | ||
|
||
#[derive(Debug)] | ||
struct A { | ||
a: i32, | ||
} | ||
|
||
impl A { | ||
fn double(&mut self) { | ||
self.a += self.a | ||
} | ||
} | ||
|
||
fn baz() { | ||
let mut v = [A { a: 4 }]; | ||
v.iter_mut().for_each(|a| a.double()); | ||
//~^ ERROR cannot borrow `*a` as mutable, as it is behind a `&` reference | ||
println!("{:?}", v); | ||
} | ||
|
||
fn bar() { | ||
let mut v = [A { a: 4 }]; | ||
v.iter_mut().rev().rev().for_each(|a| a.double()); | ||
//~^ ERROR cannot borrow `*a` as mutable, as it is behind a `&` reference | ||
println!("{:?}", v); | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// run-rustfix | ||
#![allow(unused_mut)] | ||
#![allow(dead_code)] | ||
|
||
#[derive(Debug)] | ||
struct A { | ||
a: i32, | ||
} | ||
|
||
impl A { | ||
fn double(&mut self) { | ||
self.a += self.a | ||
} | ||
} | ||
|
||
fn baz() { | ||
let mut v = [A { a: 4 }]; | ||
v.iter().for_each(|a| a.double()); | ||
//~^ ERROR cannot borrow `*a` as mutable, as it is behind a `&` reference | ||
println!("{:?}", v); | ||
} | ||
|
||
fn bar() { | ||
let mut v = [A { a: 4 }]; | ||
v.iter().rev().rev().for_each(|a| a.double()); | ||
//~^ ERROR cannot borrow `*a` as mutable, as it is behind a `&` reference | ||
println!("{:?}", v); | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
error[E0596]: cannot borrow `*a` as mutable, as it is behind a `&` reference | ||
--> $DIR/issue-62387-suggest-iter-mut.rs:18:27 | ||
| | ||
LL | v.iter().for_each(|a| a.double()); | ||
| - ^ `a` is a `&` reference, so the data it refers to cannot be borrowed as mutable | ||
| | | ||
| consider changing this binding's type to be: `&mut A` | ||
| | ||
help: you may want to use `iter_mut` here | ||
| | ||
LL | v.iter_mut().for_each(|a| a.double()); | ||
| ~~~~~~~~ | ||
|
||
error[E0596]: cannot borrow `*a` as mutable, as it is behind a `&` reference | ||
--> $DIR/issue-62387-suggest-iter-mut.rs:25:39 | ||
| | ||
LL | v.iter().rev().rev().for_each(|a| a.double()); | ||
| - ^ `a` is a `&` reference, so the data it refers to cannot be borrowed as mutable | ||
| | | ||
| consider changing this binding's type to be: `&mut A` | ||
| | ||
help: you may want to use `iter_mut` here | ||
| | ||
LL | v.iter_mut().rev().rev().for_each(|a| a.double()); | ||
| ~~~~~~~~ | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0596`. |