Skip to content

Commit

Permalink
add a ui test for .as_ref suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
WaffleLapkin committed Aug 19, 2022
1 parent 6c943ba commit 622e425
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/test/ui/suggestions/option_as_ref.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// run-rustfix

fn _foo(opt: &Option<Box<i32>>) -> String {
opt.as_ref().map(|x| x.to_string()).unwrap_or_else(String::new)
//~^ cannot move out of `*opt` which is behind a shared reference
}

fn main(){}
8 changes: 8 additions & 0 deletions src/test/ui/suggestions/option_as_ref.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// run-rustfix

fn _foo(opt: &Option<Box<i32>>) -> String {
opt.map(|x| x.to_string()).unwrap_or_else(String::new)
//~^ cannot move out of `*opt` which is behind a shared reference
}

fn main(){}
22 changes: 22 additions & 0 deletions src/test/ui/suggestions/option_as_ref.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
error[E0507]: cannot move out of `*opt` which is behind a shared reference
--> $DIR/option_as_ref.rs:4:5
|
LL | opt.map(|x| x.to_string()).unwrap_or_else(String::new)
| ^^^^----------------------
| | |
| | `*opt` moved due to this method call
| move occurs because `*opt` has type `Option<Box<i32>>`, which does not implement the `Copy` trait
|
note: this function takes ownership of the receiver `self`, which moves `*opt`
--> $SRC_DIR/core/src/option.rs:LL:COL
|
LL | pub const fn map<U, F>(self, f: F) -> Option<U>
| ^^^^
help: consider calling `.as_ref()` to borrow the type's contents
|
LL | opt.as_ref().map(|x| x.to_string()).unwrap_or_else(String::new)
| +++++++++

error: aborting due to previous error

For more information about this error, try `rustc --explain E0507`.

0 comments on commit 622e425

Please sign in to comment.