-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #84353 - estebank:as-ref-mir, r=davidtwco
Suggest `.as_ref()` on borrow error involving `Option`/`Result` When encountering a E0382 borrow error involving an `Option` or `Result` provide a suggestion to use `.as_ref()` on the prior move location to avoid the move. Fix #84165.
- Loading branch information
Showing
7 changed files
with
134 additions
and
62 deletions.
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
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,13 @@ | ||
// run-rustfix | ||
|
||
struct Struct; | ||
|
||
fn bar(_: &Struct) -> Struct { | ||
Struct | ||
} | ||
|
||
fn main() { | ||
let foo = Some(Struct); | ||
let _x: Option<Struct> = foo.as_ref().map(|s| bar(&s)); | ||
let _y = foo; //~ERROR use of moved value: `foo` | ||
} |
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,13 @@ | ||
// run-rustfix | ||
|
||
struct Struct; | ||
|
||
fn bar(_: &Struct) -> Struct { | ||
Struct | ||
} | ||
|
||
fn main() { | ||
let foo = Some(Struct); | ||
let _x: Option<Struct> = foo.map(|s| bar(&s)); | ||
let _y = foo; //~ERROR use of moved value: `foo` | ||
} |
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,23 @@ | ||
error[E0382]: use of moved value: `foo` | ||
--> $DIR/as-ref-2.rs:12:14 | ||
| | ||
LL | let foo = Some(Struct); | ||
| --- move occurs because `foo` has type `Option<Struct>`, which does not implement the `Copy` trait | ||
LL | let _x: Option<Struct> = foo.map(|s| bar(&s)); | ||
| ---------------- `foo` moved due to this method call | ||
LL | let _y = foo; | ||
| ^^^ value used here after move | ||
| | ||
note: this function takes ownership of the receiver `self`, which moves `foo` | ||
--> $SRC_DIR/core/src/option.rs:LL:COL | ||
| | ||
LL | pub fn map<U, F: FnOnce(T) -> U>(self, f: F) -> Option<U> { | ||
| ^^^^ | ||
help: consider calling `.as_ref()` to borrow the type's contents | ||
| | ||
LL | let _x: Option<Struct> = foo.as_ref().map(|s| bar(&s)); | ||
| ^^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0382`. |
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 |
---|---|---|
@@ -1,25 +1,20 @@ | ||
struct Foo; | ||
|
||
fn takes_ref(_: &Foo) {} | ||
|
||
fn main() { | ||
let ref opt = Some(Foo); | ||
opt.map(|arg| takes_ref(arg)); | ||
//~^ ERROR mismatched types [E0308] | ||
opt.and_then(|arg| Some(takes_ref(arg))); | ||
//~^ ERROR mismatched types [E0308] | ||
let ref opt: Result<_, ()> = Ok(Foo); | ||
opt.map(|arg| takes_ref(arg)); | ||
//~^ ERROR mismatched types [E0308] | ||
opt.and_then(|arg| Ok(takes_ref(arg))); | ||
//~^ ERROR mismatched types [E0308] | ||
let x: &Option<usize> = &Some(3); | ||
let y: Option<&usize> = x; | ||
//~^ ERROR mismatched types [E0308] | ||
let x: &Result<usize, usize> = &Ok(3); | ||
let y: Result<&usize, &usize> = x; | ||
//~^ ERROR mismatched types [E0308] | ||
// note: do not suggest because of `E: usize` | ||
let x: &Result<usize, usize> = &Ok(3); | ||
let y: Result<&usize, usize> = x; | ||
//~^ ERROR mismatched types [E0308] | ||
let ref opt = Some(Foo); | ||
opt.map(|arg| takes_ref(arg)); //~ ERROR mismatched types [E0308] | ||
opt.and_then(|arg| Some(takes_ref(arg))); //~ ERROR mismatched types [E0308] | ||
let ref opt: Result<_, ()> = Ok(Foo); | ||
opt.map(|arg| takes_ref(arg)); //~ ERROR mismatched types [E0308] | ||
opt.and_then(|arg| Ok(takes_ref(arg))); //~ ERROR mismatched types [E0308] | ||
let x: &Option<usize> = &Some(3); | ||
let y: Option<&usize> = x; //~ ERROR mismatched types [E0308] | ||
let x: &Result<usize, usize> = &Ok(3); | ||
let y: Result<&usize, &usize> = x; | ||
//~^ ERROR mismatched types [E0308] | ||
// note: do not suggest because of `E: usize` | ||
let x: &Result<usize, usize> = &Ok(3); | ||
let y: Result<&usize, usize> = x; //~ ERROR mismatched types [E0308] | ||
} |
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