Skip to content

Commit e37565d

Browse files
Make as_ref suggestion a note
1 parent 84ba228 commit e37565d

File tree

8 files changed

+15
-84
lines changed

8 files changed

+15
-84
lines changed

compiler/rustc_borrowck/src/diagnostics/mod.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -1086,14 +1086,6 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
10861086
),
10871087
);
10881088
}
1089-
if is_option_or_result && maybe_reinitialized_locations_is_empty {
1090-
err.span_suggestion_verbose(
1091-
fn_call_span.shrink_to_lo(),
1092-
"consider calling `.as_ref()` or `.as_mut()` to borrow the type's contents",
1093-
"as_ref().",
1094-
Applicability::MachineApplicable,
1095-
);
1096-
}
10971089
// Avoid pointing to the same function in multiple different
10981090
// error messages.
10991091
if span != DUMMY_SP && self.fn_self_span_reported.insert(self_arg.span) {
@@ -1102,6 +1094,12 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
11021094
&format!("this function takes ownership of the receiver `self`, which moves {}", place_name)
11031095
);
11041096
}
1097+
if is_option_or_result && maybe_reinitialized_locations_is_empty {
1098+
err.span_label(
1099+
var_span,
1100+
"help: consider calling `.as_ref()` or `.as_mut()` to borrow the type's contents",
1101+
);
1102+
}
11051103
}
11061104
// Other desugarings takes &self, which cannot cause a move
11071105
_ => {}

src/test/ui/borrowck/suggest-as-ref-on-mut-closure.stderr

+1-4
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,14 @@ LL | cb.map(|cb| cb());
55
| ^^^--------------
66
| | |
77
| | `*cb` moved due to this method call
8+
| help: consider calling `.as_ref()` or `.as_mut()` to borrow the type's contents
89
| move occurs because `*cb` has type `Option<&mut dyn FnMut()>`, which does not implement the `Copy` trait
910
|
1011
note: this function takes ownership of the receiver `self`, which moves `*cb`
1112
--> $SRC_DIR/core/src/option.rs:LL:COL
1213
|
1314
LL | pub const fn map<U, F>(self, f: F) -> Option<U>
1415
| ^^^^
15-
help: consider calling `.as_ref()` or `.as_mut()` to borrow the type's contents
16-
|
17-
LL | cb.as_ref().map(|cb| cb());
18-
| +++++++++
1916

2017
error[E0596]: cannot borrow `*cb` as mutable, as it is behind a `&` reference
2118
--> $DIR/suggest-as-ref-on-mut-closure.rs:12:26

src/test/ui/suggestions/as-ref-2.fixed

-13
This file was deleted.

src/test/ui/suggestions/as-ref-2.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// run-rustfix
2-
31
struct Struct;
42

53
fn bar(_: &Struct) -> Struct {

src/test/ui/suggestions/as-ref-2.stderr

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
error[E0382]: use of moved value: `foo`
2-
--> $DIR/as-ref-2.rs:12:14
2+
--> $DIR/as-ref-2.rs:10:14
33
|
44
LL | let foo = Some(Struct);
55
| --- move occurs because `foo` has type `Option<Struct>`, which does not implement the `Copy` trait
66
LL | let _x: Option<Struct> = foo.map(|s| bar(&s));
7-
| ---------------- `foo` moved due to this method call
7+
| --- ---------------- `foo` moved due to this method call
8+
| |
9+
| help: consider calling `.as_ref()` or `.as_mut()` to borrow the type's contents
810
LL | let _y = foo;
911
| ^^^ value used here after move
1012
|
@@ -13,10 +15,6 @@ note: this function takes ownership of the receiver `self`, which moves `foo`
1315
|
1416
LL | pub const fn map<U, F>(self, f: F) -> Option<U>
1517
| ^^^^
16-
help: consider calling `.as_ref()` or `.as_mut()` to borrow the type's contents
17-
|
18-
LL | let _x: Option<Struct> = foo.as_ref().map(|s| bar(&s));
19-
| +++++++++
2018

2119
error: aborting due to previous error
2220

src/test/ui/suggestions/option-content-move.fixed

-39
This file was deleted.

src/test/ui/suggestions/option-content-move.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
//run-rustfix
2-
31
pub struct LipogramCorpora {
42
selections: Vec<(char, Option<String>)>,
53
}

src/test/ui/suggestions/option-content-move.stderr

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,32 @@
11
error[E0507]: cannot move out of `selection.1` which is behind a shared reference
2-
--> $DIR/option-content-move.rs:11:20
2+
--> $DIR/option-content-move.rs:9:20
33
|
44
LL | if selection.1.unwrap().contains(selection.0) {
55
| ^^^^^^^^^^^ -------- `selection.1` moved due to this method call
66
| |
7+
| help: consider calling `.as_ref()` or `.as_mut()` to borrow the type's contents
78
| move occurs because `selection.1` has type `Option<String>`, which does not implement the `Copy` trait
89
|
910
note: this function takes ownership of the receiver `self`, which moves `selection.1`
1011
--> $SRC_DIR/core/src/option.rs:LL:COL
1112
|
1213
LL | pub const fn unwrap(self) -> T {
1314
| ^^^^
14-
help: consider calling `.as_ref()` or `.as_mut()` to borrow the type's contents
15-
|
16-
LL | if selection.1.as_ref().unwrap().contains(selection.0) {
17-
| +++++++++
1815

1916
error[E0507]: cannot move out of `selection.1` which is behind a shared reference
20-
--> $DIR/option-content-move.rs:29:20
17+
--> $DIR/option-content-move.rs:27:20
2118
|
2219
LL | if selection.1.unwrap().contains(selection.0) {
2320
| ^^^^^^^^^^^ -------- `selection.1` moved due to this method call
2421
| |
22+
| help: consider calling `.as_ref()` or `.as_mut()` to borrow the type's contents
2523
| move occurs because `selection.1` has type `Result<String, String>`, which does not implement the `Copy` trait
2624
|
2725
note: this function takes ownership of the receiver `self`, which moves `selection.1`
2826
--> $SRC_DIR/core/src/result.rs:LL:COL
2927
|
3028
LL | pub fn unwrap(self) -> T
3129
| ^^^^
32-
help: consider calling `.as_ref()` or `.as_mut()` to borrow the type's contents
33-
|
34-
LL | if selection.1.as_ref().unwrap().contains(selection.0) {
35-
| +++++++++
3630

3731
error: aborting due to 2 previous errors
3832

0 commit comments

Comments
 (0)