Auto merge of #61147 - estebank:suggest-as-ref, r=oli-obk

When encountering move error on an `Option`, suggest using `as_ref`

Fix #61109, cc #15457.
This commit is contained in:
bors 2019-05-27 01:10:22 +00:00
commit ed2a5115da
4 changed files with 188 additions and 76 deletions

View file

@ -0,0 +1,39 @@
//run-rustfix
pub struct LipogramCorpora {
selections: Vec<(char, Option<String>)>,
}
impl LipogramCorpora {
pub fn validate_all(&mut self) -> Result<(), char> {
for selection in &self.selections {
if selection.1.is_some() {
if selection.1.as_ref().unwrap().contains(selection.0) {
//~^ ERROR cannot move out of borrowed content
return Err(selection.0);
}
}
}
Ok(())
}
}
pub struct LipogramCorpora2 {
selections: Vec<(char, Result<String, String>)>,
}
impl LipogramCorpora2 {
pub fn validate_all(&mut self) -> Result<(), char> {
for selection in &self.selections {
if selection.1.is_ok() {
if selection.1.as_ref().unwrap().contains(selection.0) {
//~^ ERROR cannot move out of borrowed content
return Err(selection.0);
}
}
}
Ok(())
}
}
fn main() {}

View file

@ -0,0 +1,39 @@
//run-rustfix
pub struct LipogramCorpora {
selections: Vec<(char, Option<String>)>,
}
impl LipogramCorpora {
pub fn validate_all(&mut self) -> Result<(), char> {
for selection in &self.selections {
if selection.1.is_some() {
if selection.1.unwrap().contains(selection.0) {
//~^ ERROR cannot move out of borrowed content
return Err(selection.0);
}
}
}
Ok(())
}
}
pub struct LipogramCorpora2 {
selections: Vec<(char, Result<String, String>)>,
}
impl LipogramCorpora2 {
pub fn validate_all(&mut self) -> Result<(), char> {
for selection in &self.selections {
if selection.1.is_ok() {
if selection.1.unwrap().contains(selection.0) {
//~^ ERROR cannot move out of borrowed content
return Err(selection.0);
}
}
}
Ok(())
}
}
fn main() {}

View file

@ -0,0 +1,21 @@
error[E0507]: cannot move out of borrowed content
--> $DIR/option-content-move.rs:11:20
|
LL | if selection.1.unwrap().contains(selection.0) {
| ^^^^^^^^^^^
| |
| cannot move out of borrowed content
| help: consider borrowing the `Option`'s content: `selection.1.as_ref()`
error[E0507]: cannot move out of borrowed content
--> $DIR/option-content-move.rs:29:20
|
LL | if selection.1.unwrap().contains(selection.0) {
| ^^^^^^^^^^^
| |
| cannot move out of borrowed content
| help: consider borrowing the `Result`'s content: `selection.1.as_ref()`
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0507`.