Skip to content

Commit

Permalink
Make as_ref suggestion a note
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Aug 16, 2022
1 parent 84ba228 commit e37565d
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 84 deletions.
14 changes: 6 additions & 8 deletions compiler/rustc_borrowck/src/diagnostics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1086,14 +1086,6 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
),
);
}
if is_option_or_result && maybe_reinitialized_locations_is_empty {
err.span_suggestion_verbose(
fn_call_span.shrink_to_lo(),
"consider calling `.as_ref()` or `.as_mut()` to borrow the type's contents",
"as_ref().",
Applicability::MachineApplicable,
);
}
// Avoid pointing to the same function in multiple different
// error messages.
if span != DUMMY_SP && self.fn_self_span_reported.insert(self_arg.span) {
Expand All @@ -1102,6 +1094,12 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
&format!("this function takes ownership of the receiver `self`, which moves {}", place_name)
);
}
if is_option_or_result && maybe_reinitialized_locations_is_empty {
err.span_label(
var_span,
"help: consider calling `.as_ref()` or `.as_mut()` to borrow the type's contents",
);
}
}
// Other desugarings takes &self, which cannot cause a move
_ => {}
Expand Down
5 changes: 1 addition & 4 deletions src/test/ui/borrowck/suggest-as-ref-on-mut-closure.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@ LL | cb.map(|cb| cb());
| ^^^--------------
| | |
| | `*cb` moved due to this method call
| help: consider calling `.as_ref()` or `.as_mut()` to borrow the type's contents
| move occurs because `*cb` has type `Option<&mut dyn FnMut()>`, which does not implement the `Copy` trait
|
note: this function takes ownership of the receiver `self`, which moves `*cb`
--> $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()` or `.as_mut()` to borrow the type's contents
|
LL | cb.as_ref().map(|cb| cb());
| +++++++++

error[E0596]: cannot borrow `*cb` as mutable, as it is behind a `&` reference
--> $DIR/suggest-as-ref-on-mut-closure.rs:12:26
Expand Down
13 changes: 0 additions & 13 deletions src/test/ui/suggestions/as-ref-2.fixed

This file was deleted.

2 changes: 0 additions & 2 deletions src/test/ui/suggestions/as-ref-2.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// run-rustfix

struct Struct;

fn bar(_: &Struct) -> Struct {
Expand Down
10 changes: 4 additions & 6 deletions src/test/ui/suggestions/as-ref-2.stderr
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
error[E0382]: use of moved value: `foo`
--> $DIR/as-ref-2.rs:12:14
--> $DIR/as-ref-2.rs:10: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
| --- ---------------- `foo` moved due to this method call
| |
| help: consider calling `.as_ref()` or `.as_mut()` to borrow the type's contents
LL | let _y = foo;
| ^^^ value used here after move
|
Expand All @@ -13,10 +15,6 @@ note: this function takes ownership of the receiver `self`, which moves `foo`
|
LL | pub const fn map<U, F>(self, f: F) -> Option<U>
| ^^^^
help: consider calling `.as_ref()` or `.as_mut()` to borrow the type's contents
|
LL | let _x: Option<Struct> = foo.as_ref().map(|s| bar(&s));
| +++++++++

error: aborting due to previous error

Expand Down
39 changes: 0 additions & 39 deletions src/test/ui/suggestions/option-content-move.fixed

This file was deleted.

2 changes: 0 additions & 2 deletions src/test/ui/suggestions/option-content-move.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//run-rustfix

pub struct LipogramCorpora {
selections: Vec<(char, Option<String>)>,
}
Expand Down
14 changes: 4 additions & 10 deletions src/test/ui/suggestions/option-content-move.stderr
Original file line number Diff line number Diff line change
@@ -1,38 +1,32 @@
error[E0507]: cannot move out of `selection.1` which is behind a shared reference
--> $DIR/option-content-move.rs:11:20
--> $DIR/option-content-move.rs:9:20
|
LL | if selection.1.unwrap().contains(selection.0) {
| ^^^^^^^^^^^ -------- `selection.1` moved due to this method call
| |
| help: consider calling `.as_ref()` or `.as_mut()` to borrow the type's contents
| move occurs because `selection.1` has type `Option<String>`, which does not implement the `Copy` trait
|
note: this function takes ownership of the receiver `self`, which moves `selection.1`
--> $SRC_DIR/core/src/option.rs:LL:COL
|
LL | pub const fn unwrap(self) -> T {
| ^^^^
help: consider calling `.as_ref()` or `.as_mut()` to borrow the type's contents
|
LL | if selection.1.as_ref().unwrap().contains(selection.0) {
| +++++++++

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

error: aborting due to 2 previous errors

Expand Down

0 comments on commit e37565d

Please sign in to comment.