diff options
| author | Corey Farwell <coreyf@rwell.org> | 2017-05-02 21:21:19 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-05-02 21:21:19 -0400 |
| commit | 9e621c2da8b96b4cfdca208dea21a2888f00d9e0 (patch) | |
| tree | 1078209707d72f113bbfd4a5989e7776760aeb61 | |
| parent | cfff3699aa33e0de8000b93146a0e218208a39c1 (diff) | |
| parent | 1c57bb4219523c38a2fcaf609c845ef77b6f2240 (diff) | |
| download | rust-9e621c2da8b96b4cfdca208dea21a2888f00d9e0.tar.gz rust-9e621c2da8b96b4cfdca208dea21a2888f00d9e0.zip | |
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
| -rw-r--r-- | src/librustc_borrowck/borrowck/gather_loans/move_error.rs | 9 | ||||
| -rw-r--r-- | src/test/ui/issue-40402-ref-hints/issue-40402-1.stderr | 5 |
2 files changed, 12 insertions, 2 deletions
diff --git a/src/librustc_borrowck/borrowck/gather_loans/move_error.rs b/src/librustc_borrowck/borrowck/gather_loans/move_error.rs index 9a72f3866a0..b7ce9d98233 100644 --- a/src/librustc_borrowck/borrowck/gather_loans/move_error.rs +++ b/src/librustc_borrowck/borrowck/gather_loans/move_error.rs @@ -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 { 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 5e743b6bd3f..de110ac12b7 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,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 |
