From 84ba2289fdd3c41ebbef53de3fdb5035f2e73e60 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Sat, 6 Aug 2022 00:17:16 +0000 Subject: [PATCH 1/2] Suggest as_ref or as_mut --- compiler/rustc_borrowck/src/diagnostics/mod.rs | 2 +- src/test/ui/borrowck/suggest-as-ref-on-mut-closure.stderr | 2 +- src/test/ui/suggestions/as-ref-2.stderr | 2 +- src/test/ui/suggestions/option-content-move.stderr | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_borrowck/src/diagnostics/mod.rs b/compiler/rustc_borrowck/src/diagnostics/mod.rs index 098e8de9420fb..cc5a3e16c963e 100644 --- a/compiler/rustc_borrowck/src/diagnostics/mod.rs +++ b/compiler/rustc_borrowck/src/diagnostics/mod.rs @@ -1089,7 +1089,7 @@ 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()` to borrow the type's contents", + "consider calling `.as_ref()` or `.as_mut()` to borrow the type's contents", "as_ref().", Applicability::MachineApplicable, ); diff --git a/src/test/ui/borrowck/suggest-as-ref-on-mut-closure.stderr b/src/test/ui/borrowck/suggest-as-ref-on-mut-closure.stderr index af26169c80681..17546f73836cf 100644 --- a/src/test/ui/borrowck/suggest-as-ref-on-mut-closure.stderr +++ b/src/test/ui/borrowck/suggest-as-ref-on-mut-closure.stderr @@ -12,7 +12,7 @@ note: this function takes ownership of the receiver `self`, which moves `*cb` | LL | pub const fn map(self, f: F) -> Option | ^^^^ -help: consider calling `.as_ref()` to borrow the type's contents +help: consider calling `.as_ref()` or `.as_mut()` to borrow the type's contents | LL | cb.as_ref().map(|cb| cb()); | +++++++++ diff --git a/src/test/ui/suggestions/as-ref-2.stderr b/src/test/ui/suggestions/as-ref-2.stderr index 3c9d0f72abe0c..08b9fb1abd796 100644 --- a/src/test/ui/suggestions/as-ref-2.stderr +++ b/src/test/ui/suggestions/as-ref-2.stderr @@ -13,7 +13,7 @@ note: this function takes ownership of the receiver `self`, which moves `foo` | LL | pub const fn map(self, f: F) -> Option | ^^^^ -help: consider calling `.as_ref()` to borrow the type's contents +help: consider calling `.as_ref()` or `.as_mut()` to borrow the type's contents | LL | let _x: Option = foo.as_ref().map(|s| bar(&s)); | +++++++++ diff --git a/src/test/ui/suggestions/option-content-move.stderr b/src/test/ui/suggestions/option-content-move.stderr index fccfbe1d744c2..d008971e4384e 100644 --- a/src/test/ui/suggestions/option-content-move.stderr +++ b/src/test/ui/suggestions/option-content-move.stderr @@ -11,7 +11,7 @@ note: this function takes ownership of the receiver `self`, which moves `selecti | LL | pub const fn unwrap(self) -> T { | ^^^^ -help: consider calling `.as_ref()` to borrow the type's contents +help: consider calling `.as_ref()` or `.as_mut()` to borrow the type's contents | LL | if selection.1.as_ref().unwrap().contains(selection.0) { | +++++++++ @@ -29,7 +29,7 @@ note: this function takes ownership of the receiver `self`, which moves `selecti | LL | pub fn unwrap(self) -> T | ^^^^ -help: consider calling `.as_ref()` to borrow the type's contents +help: consider calling `.as_ref()` or `.as_mut()` to borrow the type's contents | LL | if selection.1.as_ref().unwrap().contains(selection.0) { | +++++++++ From e37565d2db8d7cf92a6d1df535343d4b2881fc35 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Tue, 16 Aug 2022 03:02:04 +0000 Subject: [PATCH 2/2] Make as_ref suggestion a note --- .../rustc_borrowck/src/diagnostics/mod.rs | 14 +++---- .../suggest-as-ref-on-mut-closure.stderr | 5 +-- src/test/ui/suggestions/as-ref-2.fixed | 13 ------- src/test/ui/suggestions/as-ref-2.rs | 2 - src/test/ui/suggestions/as-ref-2.stderr | 10 ++--- .../ui/suggestions/option-content-move.fixed | 39 ------------------- .../ui/suggestions/option-content-move.rs | 2 - .../ui/suggestions/option-content-move.stderr | 14 ++----- 8 files changed, 15 insertions(+), 84 deletions(-) delete mode 100644 src/test/ui/suggestions/as-ref-2.fixed delete mode 100644 src/test/ui/suggestions/option-content-move.fixed diff --git a/compiler/rustc_borrowck/src/diagnostics/mod.rs b/compiler/rustc_borrowck/src/diagnostics/mod.rs index cc5a3e16c963e..683084cf09d44 100644 --- a/compiler/rustc_borrowck/src/diagnostics/mod.rs +++ b/compiler/rustc_borrowck/src/diagnostics/mod.rs @@ -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) { @@ -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 _ => {} diff --git a/src/test/ui/borrowck/suggest-as-ref-on-mut-closure.stderr b/src/test/ui/borrowck/suggest-as-ref-on-mut-closure.stderr index 17546f73836cf..b1af090aec2b0 100644 --- a/src/test/ui/borrowck/suggest-as-ref-on-mut-closure.stderr +++ b/src/test/ui/borrowck/suggest-as-ref-on-mut-closure.stderr @@ -5,6 +5,7 @@ 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` @@ -12,10 +13,6 @@ note: this function takes ownership of the receiver `self`, which moves `*cb` | LL | pub const fn map(self, f: F) -> Option | ^^^^ -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 diff --git a/src/test/ui/suggestions/as-ref-2.fixed b/src/test/ui/suggestions/as-ref-2.fixed deleted file mode 100644 index 13bbb233f3986..0000000000000 --- a/src/test/ui/suggestions/as-ref-2.fixed +++ /dev/null @@ -1,13 +0,0 @@ -// run-rustfix - -struct Struct; - -fn bar(_: &Struct) -> Struct { - Struct -} - -fn main() { - let foo = Some(Struct); - let _x: Option = foo.as_ref().map(|s| bar(&s)); - let _y = foo; //~ERROR use of moved value: `foo` -} diff --git a/src/test/ui/suggestions/as-ref-2.rs b/src/test/ui/suggestions/as-ref-2.rs index 74d61cdd95f8d..b22f409b44a93 100644 --- a/src/test/ui/suggestions/as-ref-2.rs +++ b/src/test/ui/suggestions/as-ref-2.rs @@ -1,5 +1,3 @@ -// run-rustfix - struct Struct; fn bar(_: &Struct) -> Struct { diff --git a/src/test/ui/suggestions/as-ref-2.stderr b/src/test/ui/suggestions/as-ref-2.stderr index 08b9fb1abd796..e15e45d86b992 100644 --- a/src/test/ui/suggestions/as-ref-2.stderr +++ b/src/test/ui/suggestions/as-ref-2.stderr @@ -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`, which does not implement the `Copy` trait LL | let _x: Option = 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 | @@ -13,10 +15,6 @@ note: this function takes ownership of the receiver `self`, which moves `foo` | LL | pub const fn map(self, f: F) -> Option | ^^^^ -help: consider calling `.as_ref()` or `.as_mut()` to borrow the type's contents - | -LL | let _x: Option = foo.as_ref().map(|s| bar(&s)); - | +++++++++ error: aborting due to previous error diff --git a/src/test/ui/suggestions/option-content-move.fixed b/src/test/ui/suggestions/option-content-move.fixed deleted file mode 100644 index ba16bcc8a336d..0000000000000 --- a/src/test/ui/suggestions/option-content-move.fixed +++ /dev/null @@ -1,39 +0,0 @@ -//run-rustfix - -pub struct LipogramCorpora { - selections: Vec<(char, Option)>, -} - -impl LipogramCorpora { - pub fn validate_all(&mut self) -> Result<(), char> { - for selection in &self.selections { - if selection.1.is_some() { - if selection.1.as_ref().unwrap().contains(selection.0) { - //~^ ERROR cannot move out of `selection.1` - return Err(selection.0); - } - } - } - Ok(()) - } -} - -pub struct LipogramCorpora2 { - selections: Vec<(char, Result)>, -} - -impl LipogramCorpora2 { - pub fn validate_all(&mut self) -> Result<(), char> { - for selection in &self.selections { - if selection.1.is_ok() { - if selection.1.as_ref().unwrap().contains(selection.0) { - //~^ ERROR cannot move out of `selection.1` - return Err(selection.0); - } - } - } - Ok(()) - } -} - -fn main() {} diff --git a/src/test/ui/suggestions/option-content-move.rs b/src/test/ui/suggestions/option-content-move.rs index ef38f114eca55..46c895b95f536 100644 --- a/src/test/ui/suggestions/option-content-move.rs +++ b/src/test/ui/suggestions/option-content-move.rs @@ -1,5 +1,3 @@ -//run-rustfix - pub struct LipogramCorpora { selections: Vec<(char, Option)>, } diff --git a/src/test/ui/suggestions/option-content-move.stderr b/src/test/ui/suggestions/option-content-move.stderr index d008971e4384e..a6f1ebc975fd5 100644 --- a/src/test/ui/suggestions/option-content-move.stderr +++ b/src/test/ui/suggestions/option-content-move.stderr @@ -1,9 +1,10 @@ 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`, which does not implement the `Copy` trait | note: this function takes ownership of the receiver `self`, which moves `selection.1` @@ -11,17 +12,14 @@ note: this function takes ownership of the receiver `self`, which moves `selecti | 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`, which does not implement the `Copy` trait | note: this function takes ownership of the receiver `self`, which moves `selection.1` @@ -29,10 +27,6 @@ note: this function takes ownership of the receiver `self`, which moves `selecti | 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