Skip to content

Commit 478b7d9

Browse files
committed
Adding consider changing to & suggestion for let bindings
1 parent 50517d5 commit 478b7d9

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/librustc_borrowck/borrowck/gather_loans/move_error.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,19 @@ fn report_move_errors<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>, errors: &Vec<Move
7373
let mut err = report_cannot_move_out_of(bccx, error.move_from.clone());
7474
let mut is_first_note = true;
7575
match error.move_to_places.get(0) {
76-
Some(&MovePlace { pat_source: PatternSource::LetDecl(_), .. }) => {
76+
Some(&MovePlace { pat_source: PatternSource::LetDecl(ref e), .. }) => {
7777
// ignore patterns that are found at the top-level of a `let`;
7878
// see `get_pattern_source()` for details
79+
let initializer =
80+
e.init.as_ref().expect("should have an initializer to get an error");
81+
if let Ok(snippet) = bccx.tcx.sess.codemap().span_to_snippet(initializer.span) {
82+
if snippet.len() > 10 {
83+
err.help(&format!("consider borrowing this with `&`"));
84+
} else {
85+
err.help(&format!("consider changing to `&{}`", snippet));
86+
}
87+
}
88+
7989
}
8090
_ => {
8191
for move_to in &error.move_to_places {

src/test/ui/issue-40402-ref-hints/issue-40402-1.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ error[E0507]: cannot move out of indexed content
33
|
44
19 | let e = f.v[0];
55
| ^^^^^^ cannot move out of indexed content
6+
|
7+
= help: consider changing to `&f.v[0]`
68

79
error: aborting due to previous error
810

0 commit comments

Comments
 (0)