Rollup merge of #41640 - gaurikholkar:master, r=nikomatsakis
Consider changing to & for let bindings #40402 This is a fix for #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 #41564 for more details. r? @nikomatsakis
This commit is contained in:
commit
9e621c2da8
@ -73,9 +73,16 @@ 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) {
|
||||
err.span_suggestion(initializer.span,
|
||||
"consider using a reference instead",
|
||||
format!("&{}", snippet));
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
for move_to in &error.move_to_places {
|
||||
|
@ -2,7 +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 using a reference instead `&f.v[0]`
|
||||
| cannot move out of indexed content
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user