Skip to content

Commit 9e621c2

Browse files
authored
Rollup merge of rust-lang#41640 - gaurikholkar:master, r=nikomatsakis
Consider changing to & for let bindings rust-lang#40402 This is a fix for rust-lang#40402 For the example ``` fn main() { let v = vec![String::from("oh no")]; let e = v[0]; } ``` It gives ``` error[E0507]: cannot move out of indexed content --> ex1.rs:4:13 | 4 | let e = v[0]; | ^^^^ cannot move out of indexed content | = help: consider changing to `&v[0]` error: aborting due to previous error ``` Another alternative is ``` error[E0507]: cannot move out of indexed content --> ex1.rs:4:13 | 4 | let e = v[0]; | ^^^^ consider changing to `&v[0]` error: aborting due to previous error ``` Also refer to rust-lang#41564 for more details. r? @nikomatsakis
2 parents cfff369 + 1c57bb4 commit 9e621c2

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/librustc_borrowck/borrowck/gather_loans/move_error.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,16 @@ 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+
err.span_suggestion(initializer.span,
83+
"consider using a reference instead",
84+
format!("&{}", snippet));
85+
}
7986
}
8087
_ => {
8188
for move_to in &error.move_to_places {

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ error[E0507]: cannot move out of indexed content
22
--> $DIR/issue-40402-1.rs:19:13
33
|
44
19 | let e = f.v[0];
5-
| ^^^^^^ cannot move out of indexed content
5+
| ^^^^^^
6+
| |
7+
| help: consider using a reference instead `&f.v[0]`
8+
| cannot move out of indexed content
69

710
error: aborting due to previous error
811

0 commit comments

Comments
 (0)