Suggest cloning captured binding in move closure
```
error[E0507]: cannot move out of `bar`, a captured variable in an `FnMut` closure
--> $DIR/borrowck-move-by-capture.rs:9:29
|
LL | let bar: Box<_> = Box::new(3);
| --- captured outer variable
LL | let _g = to_fn_mut(|| {
| -- captured by this `FnMut` closure
LL | let _h = to_fn_once(move || -> isize { *bar });
| ^^^^^^^^^^^^^^^^ ----
| | |
| | variable moved due to use in closure
| | move occurs because `bar` has type `Box<isize>`, which does not implement the `Copy` trait
| `bar` is moved here
|
help: clone the value before moving it into the closure
|
LL ~ let value = bar.clone();
LL ~ let _h = to_fn_once(move || -> isize { value });
|
```
This commit is contained in:
@@ -50,12 +50,13 @@ pub struct FindExprBySpan<'hir> {
|
||||
pub span: Span,
|
||||
pub result: Option<&'hir hir::Expr<'hir>>,
|
||||
pub ty_result: Option<&'hir hir::Ty<'hir>>,
|
||||
pub include_closures: bool,
|
||||
pub tcx: TyCtxt<'hir>,
|
||||
}
|
||||
|
||||
impl<'hir> FindExprBySpan<'hir> {
|
||||
pub fn new(span: Span, tcx: TyCtxt<'hir>) -> Self {
|
||||
Self { span, result: None, ty_result: None, tcx }
|
||||
Self { span, result: None, ty_result: None, tcx, include_closures: false }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,9 +71,17 @@ impl<'v> Visitor<'v> for FindExprBySpan<'v> {
|
||||
if self.span == ex.span {
|
||||
self.result = Some(ex);
|
||||
} else {
|
||||
if let hir::ExprKind::Closure(..) = ex.kind
|
||||
&& self.include_closures
|
||||
&& let closure_header_sp = self.span.with_hi(ex.span.hi())
|
||||
&& closure_header_sp == ex.span
|
||||
{
|
||||
self.result = Some(ex);
|
||||
}
|
||||
hir::intravisit::walk_expr(self, ex);
|
||||
}
|
||||
}
|
||||
|
||||
fn visit_ty(&mut self, ty: &'v hir::Ty<'v>) {
|
||||
if self.span == ty.span {
|
||||
self.ty_result = Some(ty);
|
||||
|
||||
Reference in New Issue
Block a user