Skip to content

Commit 3d31c21

Browse files
committed
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
2 parents 75bfa29 + 6937d55 commit 3d31c21

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

clippy_lints/src/escape.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
102102
impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {
103103
fn consume(&mut self, _: HirId, _: Span, cmt: &cmt_<'tcx>, mode: ConsumeMode) {
104104
if let Categorization::Local(lid) = cmt.cat {
105-
if let Move(DirectRefMove) = mode {
106-
// Moved out or in. Clearly can't be localized.
105+
if let Move(DirectRefMove) | Move(CaptureMove) = mode {
106+
// moved out or in. clearly can't be localized
107107
self.set.remove(&lid);
108108
}
109109
}

tests/ui/escape_analysis.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,23 @@ trait MyTrait {
148148
impl<T> MyTrait for Box<T> {
149149
fn do_sth(self) {}
150150
}
151+
152+
// Issue #3739 - capture in closures
153+
mod issue_3739 {
154+
use super::A;
155+
156+
fn consume<T>(_: T) {}
157+
fn borrow<T>(_: &T) {}
158+
159+
fn closure_consume(x: Box<A>) {
160+
let _ = move || {
161+
consume(x);
162+
};
163+
}
164+
165+
fn closure_borrow(x: Box<A>) {
166+
let _ = || {
167+
borrow(&x);
168+
};
169+
}
170+
}

tests/ui/escape_analysis.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,11 @@ error: local variable doesn't need to be boxed here
1212
LL | pub fn new(_needs_name: Box<PeekableSeekable<&()>>) -> () {}
1313
| ^^^^^^^^^^^
1414

15-
error: aborting due to 2 previous errors
15+
error: local variable doesn't need to be boxed here
16+
--> $DIR/escape_analysis.rs:165:23
17+
|
18+
LL | fn closure_borrow(x: Box<A>) {
19+
| ^
20+
21+
error: aborting due to 3 previous errors
1622

0 commit comments

Comments
 (0)