]> git.lizzy.rs Git - rust.git/commitdiff
Adding consider changing to & suggestion for let bindings
authorgaurikholkar <f2013002@goa.bits-pilani.ac.in>
Sat, 29 Apr 2017 12:39:45 +0000 (05:39 -0700)
committergaurikholkar <f2013002@goa.bits-pilani.ac.in>
Tue, 2 May 2017 12:44:55 +0000 (05:44 -0700)
src/librustc_borrowck/borrowck/gather_loans/move_error.rs
src/test/ui/issue-40402-ref-hints/issue-40402-1.stderr

index 9a72f3866a0e1c34579ccdbe53a2aa8590f8b897..fba4c0340b91ac2114fe077579012d7a83339e04 100644 (file)
@@ -73,9 +73,19 @@ fn report_move_errors<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>, errors: &Vec<Move
         let mut err = report_cannot_move_out_of(bccx, error.move_from.clone());
         let mut is_first_note = true;
         match error.move_to_places.get(0) {
-            Some(&MovePlace { pat_source: PatternSource::LetDecl(_), .. }) => {
+            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 {
index 5e743b6bd3fe7b2ec2188e1ca22077b7466c377d..c9d0f622da8af6f5eaa6c9fea0d3ffaea7c733e3 100644 (file)
@@ -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