Suggest borrowing for loop head on move error
This commit is contained in:
parent
dec4c5201f
commit
274b7e49e0
|
@ -158,18 +158,6 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
|
|||
span,
|
||||
format!("value moved{} here, in previous iteration of loop", move_msg),
|
||||
);
|
||||
if Some(CompilerDesugaringKind::ForLoop) == span.compiler_desugaring_kind() {
|
||||
if let Ok(snippet) = self.infcx.tcx.sess.source_map()
|
||||
.span_to_snippet(span)
|
||||
{
|
||||
err.span_suggestion(
|
||||
move_span,
|
||||
"consider borrowing this to avoid moving it into the for loop",
|
||||
format!("&{}", snippet),
|
||||
Applicability::MaybeIncorrect,
|
||||
);
|
||||
}
|
||||
}
|
||||
is_loop_move = true;
|
||||
} else if move_site.traversed_back_edge {
|
||||
err.span_label(
|
||||
|
@ -185,7 +173,17 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
|
|||
&mut err,
|
||||
format!("variable moved due to use{}", move_spans.describe()),
|
||||
);
|
||||
};
|
||||
}
|
||||
if Some(CompilerDesugaringKind::ForLoop) == move_span.compiler_desugaring_kind() {
|
||||
if let Ok(snippet) = self.infcx.tcx.sess.source_map().span_to_snippet(span) {
|
||||
err.span_suggestion(
|
||||
move_span,
|
||||
"consider borrowing to avoid moving into the for loop",
|
||||
format!("&{}", snippet),
|
||||
Applicability::MaybeIncorrect,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
use_spans.var_span_label(
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
fn main() {
|
||||
let mut bad_letters = vec!['e', 't', 'o', 'i'];
|
||||
for l in bad_letters {
|
||||
// something here
|
||||
}
|
||||
bad_letters.push('s'); //~ ERROR borrow of moved value: `bad_letters`
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
error[E0382]: borrow of moved value: `bad_letters`
|
||||
--> $DIR/issue-61108.rs:6:5
|
||||
|
|
||||
LL | let mut bad_letters = vec!['e', 't', 'o', 'i'];
|
||||
| --------------- move occurs because `bad_letters` has type `std::vec::Vec<char>`, which does not implement the `Copy` trait
|
||||
LL | for l in bad_letters {
|
||||
| -----------
|
||||
| |
|
||||
| value moved here
|
||||
| help: consider borrowing to avoid moving into the for loop: `&bad_letters`
|
||||
...
|
||||
LL | bad_letters.push('s');
|
||||
| ^^^^^^^^^^^ value borrowed here after move
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
For more information about this error, try `rustc --explain E0382`.
|
|
@ -13,11 +13,10 @@ LL | let a = vec![1, 2, 3];
|
|||
| - move occurs because `a` has type `std::vec::Vec<i32>`, which does not implement the `Copy` trait
|
||||
LL | for i in &a {
|
||||
LL | for j in a {
|
||||
| ^ value moved here, in previous iteration of loop
|
||||
help: consider borrowing this to avoid moving it into the for loop
|
||||
|
|
||||
LL | for j in &a {
|
||||
| ^^
|
||||
| ^
|
||||
| |
|
||||
| value moved here, in previous iteration of loop
|
||||
| help: consider borrowing to avoid moving into the for loop: `&a`
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
||||
|
|
Loading…
Reference in New Issue