From 478b7d9a7a23dbbbe8972c1dd8fc4a38598f145a Mon Sep 17 00:00:00 2001 From: gaurikholkar Date: Sat, 29 Apr 2017 05:39:45 -0700 Subject: [PATCH 1/2] Adding consider changing to & suggestion for let bindings --- .../borrowck/gather_loans/move_error.rs | 12 +++++++++++- .../ui/issue-40402-ref-hints/issue-40402-1.stderr | 2 ++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/librustc_borrowck/borrowck/gather_loans/move_error.rs b/src/librustc_borrowck/borrowck/gather_loans/move_error.rs index 9a72f3866a0e1..fba4c0340b91a 100644 --- a/src/librustc_borrowck/borrowck/gather_loans/move_error.rs +++ b/src/librustc_borrowck/borrowck/gather_loans/move_error.rs @@ -73,9 +73,19 @@ fn report_move_errors<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>, errors: &Vec { + Some(&MovePlace { pat_source: PatternSource::LetDecl(ref e), .. }) => { // ignore patterns that are found at the top-level of a `let`; // see `get_pattern_source()` for details + let initializer = + e.init.as_ref().expect("should have an initializer to get an error"); + if let Ok(snippet) = bccx.tcx.sess.codemap().span_to_snippet(initializer.span) { + if snippet.len() > 10 { + err.help(&format!("consider borrowing this with `&`")); + } else { + err.help(&format!("consider changing to `&{}`", snippet)); + } + } + } _ => { for move_to in &error.move_to_places { diff --git a/src/test/ui/issue-40402-ref-hints/issue-40402-1.stderr b/src/test/ui/issue-40402-ref-hints/issue-40402-1.stderr index 5e743b6bd3fe7..c9d0f622da8af 100644 --- a/src/test/ui/issue-40402-ref-hints/issue-40402-1.stderr +++ b/src/test/ui/issue-40402-ref-hints/issue-40402-1.stderr @@ -3,6 +3,8 @@ error[E0507]: cannot move out of indexed content | 19 | let e = f.v[0]; | ^^^^^^ cannot move out of indexed content + | + = help: consider changing to `&f.v[0]` error: aborting due to previous error From 1c57bb4219523c38a2fcaf609c845ef77b6f2240 Mon Sep 17 00:00:00 2001 From: gaurikholkar Date: Mon, 1 May 2017 22:23:53 -0700 Subject: [PATCH 2/2] Using a span_suggestion to display use & hint --- .../borrowck/gather_loans/move_error.rs | 9 +++------ src/test/ui/issue-40402-ref-hints/issue-40402-1.stderr | 7 ++++--- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/librustc_borrowck/borrowck/gather_loans/move_error.rs b/src/librustc_borrowck/borrowck/gather_loans/move_error.rs index fba4c0340b91a..b7ce9d982331c 100644 --- a/src/librustc_borrowck/borrowck/gather_loans/move_error.rs +++ b/src/librustc_borrowck/borrowck/gather_loans/move_error.rs @@ -79,13 +79,10 @@ fn report_move_errors<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>, errors: &Vec 10 { - err.help(&format!("consider borrowing this with `&`")); - } else { - err.help(&format!("consider changing to `&{}`", snippet)); - } + err.span_suggestion(initializer.span, + "consider using a reference instead", + format!("&{}", snippet)); } - } _ => { for move_to in &error.move_to_places { diff --git a/src/test/ui/issue-40402-ref-hints/issue-40402-1.stderr b/src/test/ui/issue-40402-ref-hints/issue-40402-1.stderr index c9d0f622da8af..de110ac12b703 100644 --- a/src/test/ui/issue-40402-ref-hints/issue-40402-1.stderr +++ b/src/test/ui/issue-40402-ref-hints/issue-40402-1.stderr @@ -2,9 +2,10 @@ error[E0507]: cannot move out of indexed content --> $DIR/issue-40402-1.rs:19:13 | 19 | let e = f.v[0]; - | ^^^^^^ cannot move out of indexed content - | - = help: consider changing to `&f.v[0]` + | ^^^^^^ + | | + | help: consider using a reference instead `&f.v[0]` + | cannot move out of indexed content error: aborting due to previous error