Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/librustc_mir/borrow_check/diagnostics/move_errors.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use rustc::mir::*;
use rustc::ty;
use rustc_errors::{Applicability, DiagnosticBuilder};
use rustc_span::source_map::DesugaringKind;
use rustc_span::Span;

use crate::borrow_check::diagnostics::UseSpans;
Expand Down Expand Up @@ -397,6 +398,16 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
format!("{}.as_ref()", snippet),
Applicability::MaybeIncorrect,
);
} else if span.is_desugaring(DesugaringKind::ForLoop)
&& move_ty.starts_with("std::vec::Vec")
{
// FIXME: suggest for anything that implements `IntoIterator`.
err.span_suggestion(
span,
"consider iterating over a slice of the `Vec`'s content",
format!("&{}", snippet),
Applicability::MaybeIncorrect,
);
}
}
err
Expand Down
15 changes: 15 additions & 0 deletions src/test/ui/suggestions/for-i-in-vec.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// run-rustfix
#![allow(dead_code)]

struct Foo {
v: Vec<u32>,
}

impl Foo {
fn bar(&self) {
for _ in &self.v { //~ ERROR cannot move out of `self.v` which is behind a shared reference
}
}
}

fn main() {}
15 changes: 15 additions & 0 deletions src/test/ui/suggestions/for-i-in-vec.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// run-rustfix
#![allow(dead_code)]

struct Foo {
v: Vec<u32>,
}

impl Foo {
fn bar(&self) {
for _ in self.v { //~ ERROR cannot move out of `self.v` which is behind a shared reference
}
}
}

fn main() {}
12 changes: 12 additions & 0 deletions src/test/ui/suggestions/for-i-in-vec.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error[E0507]: cannot move out of `self.v` which is behind a shared reference
--> $DIR/for-i-in-vec.rs:10:18
|
LL | for _ in self.v {
| ^^^^^^
| |
| move occurs because `self.v` has type `std::vec::Vec<u32>`, which does not implement the `Copy` trait
| help: consider iterating over a slice of the `Vec`'s content: `&self.v`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0507`.