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:
commit
ed2a5115da
4 changed files with 188 additions and 76 deletions
39
src/test/ui/suggestions/option-content-move.fixed
Normal file
39
src/test/ui/suggestions/option-content-move.fixed
Normal 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() {}
|
||||
39
src/test/ui/suggestions/option-content-move.rs
Normal file
39
src/test/ui/suggestions/option-content-move.rs
Normal 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() {}
|
||||
21
src/test/ui/suggestions/option-content-move.stderr
Normal file
21
src/test/ui/suggestions/option-content-move.stderr
Normal 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`.
|
||||
Loading…
Add table
Add a link
Reference in a new issue