Skip to content

Commit e5b9afc

Browse files
authored
Rollup merge of rust-lang#100773 - WaffleLapkin:addasreftest, r=TaKO8Ki
add a ui test for `.as_ref` suggestion Closes rust-lang#90286
2 parents c4d57f5 + 622e425 commit e5b9afc

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// run-rustfix
2+
3+
fn _foo(opt: &Option<Box<i32>>) -> String {
4+
opt.as_ref().map(|x| x.to_string()).unwrap_or_else(String::new)
5+
//~^ cannot move out of `*opt` which is behind a shared reference
6+
}
7+
8+
fn main(){}
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// run-rustfix
2+
3+
fn _foo(opt: &Option<Box<i32>>) -> String {
4+
opt.map(|x| x.to_string()).unwrap_or_else(String::new)
5+
//~^ cannot move out of `*opt` which is behind a shared reference
6+
}
7+
8+
fn main(){}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error[E0507]: cannot move out of `*opt` which is behind a shared reference
2+
--> $DIR/option_as_ref.rs:4:5
3+
|
4+
LL | opt.map(|x| x.to_string()).unwrap_or_else(String::new)
5+
| ^^^^----------------------
6+
| | |
7+
| | `*opt` moved due to this method call
8+
| move occurs because `*opt` has type `Option<Box<i32>>`, which does not implement the `Copy` trait
9+
|
10+
note: this function takes ownership of the receiver `self`, which moves `*opt`
11+
--> $SRC_DIR/core/src/option.rs:LL:COL
12+
|
13+
LL | pub const fn map<U, F>(self, f: F) -> Option<U>
14+
| ^^^^
15+
help: consider calling `.as_ref()` to borrow the type's contents
16+
|
17+
LL | opt.as_ref().map(|x| x.to_string()).unwrap_or_else(String::new)
18+
| +++++++++
19+
20+
error: aborting due to previous error
21+
22+
For more information about this error, try `rustc --explain E0507`.

0 commit comments

Comments
 (0)