Auto merge of #3794 - mikerite:fix-3739, r=phansch

Fix `boxed_local` suggestion

Don't warn about an argument that is moved into a closure.

ExprUseVisitor doesn't walk into nested bodies so use a new
visitor that collects the variables that are moved into closures.

Fixes #3739
This commit is contained in:
bors 2019-03-12 07:43:14 +00:00
commit 3d31c2157a
3 changed files with 29 additions and 3 deletions

View File

@ -102,8 +102,8 @@ fn check_fn(
impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {
fn consume(&mut self, _: HirId, _: Span, cmt: &cmt_<'tcx>, mode: ConsumeMode) {
if let Categorization::Local(lid) = cmt.cat {
if let Move(DirectRefMove) = mode {
// Moved out or in. Clearly can't be localized.
if let Move(DirectRefMove) | Move(CaptureMove) = mode {
// moved out or in. clearly can't be localized
self.set.remove(&lid);
}
}

View File

@ -148,3 +148,23 @@ trait MyTrait {
impl<T> MyTrait for Box<T> {
fn do_sth(self) {}
}
// Issue #3739 - capture in closures
mod issue_3739 {
use super::A;
fn consume<T>(_: T) {}
fn borrow<T>(_: &T) {}
fn closure_consume(x: Box<A>) {
let _ = move || {
consume(x);
};
}
fn closure_borrow(x: Box<A>) {
let _ = || {
borrow(&x);
};
}
}

View File

@ -12,5 +12,11 @@ error: local variable doesn't need to be boxed here
LL | pub fn new(_needs_name: Box<PeekableSeekable<&()>>) -> () {}
| ^^^^^^^^^^^
error: aborting due to 2 previous errors
error: local variable doesn't need to be boxed here
--> $DIR/escape_analysis.rs:165:23
|
LL | fn closure_borrow(x: Box<A>) {
| ^
error: aborting due to 3 previous errors