Skip to content

Commit

Permalink
Merge consume and consume_pat in escape analysis
Browse files Browse the repository at this point in the history
FIXME: This doesn't work and probably needs a rewrite of the lint

See #4628 (comment)
  • Loading branch information
flip1995 committed Oct 5, 2019
1 parent c420b07 commit b7d4735
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 67 deletions.
56 changes: 11 additions & 45 deletions clippy_lints/src/escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,44 +120,27 @@ impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {
self.set.remove(&lid);
}
}
}
fn consume_pat(&mut self, consume_pat: &Pat, cmt: &cmt_<'tcx>, _: ConsumeMode) {
let map = &self.cx.tcx.hir();
if is_argument(map, consume_pat.hir_id) {
if is_argument(map, cmt.hir_id) {
// Skip closure arguments
let parent_id = map.get_parent_node(consume_pat.hir_id);
let parent_id = map.get_parent_node(cmt.hir_id);
if let Some(Node::Expr(..)) = map.find(map.get_parent_node(parent_id)) {
return;
}

if is_non_trait_box(cmt.ty) && !self.is_large_box(cmt.ty) {
self.set.insert(consume_pat.hir_id);
self.set.insert(cmt.hir_id);
}
return;
}
if let Categorization::Rvalue = cmt.cat {
if let Some(Node::Stmt(st)) = map.find(map.get_parent_node(cmt.hir_id)) {
if let StmtKind::Local(ref loc) = st.kind {
if let Some(ref ex) = loc.init {
if let ExprKind::Box(..) = ex.kind {
if is_non_trait_box(cmt.ty) && !self.is_large_box(cmt.ty) {
// let x = box (...)
self.set.insert(consume_pat.hir_id);
}
// TODO Box::new
// TODO vec![]
// TODO "foo".to_owned() and friends
}
}
}
}
}
if let Categorization::Local(lid) = cmt.cat {
if self.set.contains(&lid) {
// let y = x where x is known
// remove x, insert y
self.set.insert(consume_pat.hir_id);
self.set.remove(&lid);
if let Some(Node::Binding(_)) = map.find(cmt.hir_id) {
if self.set.contains(&lid) {
// let y = x where x is known
// remove x, insert y
self.set.insert(cmt.hir_id);
self.set.remove(&lid);
}
}
}
}
Expand All @@ -166,26 +149,9 @@ impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {
&mut self,
cmt: &cmt_<'tcx>,
_: ty::BorrowKind,
loan_cause: LoanCause,
) {
if let Categorization::Local(lid) = cmt.cat {
match loan_cause {
// `x.foo()`
// Used without autoderef-ing (i.e., `x.clone()`).
LoanCause::AutoRef |

// `&x`
// `foo(&x)` where no extra autoref-ing is happening.
LoanCause::AddrOf |

// `match x` can move.
LoanCause::MatchDiscriminant => {
self.set.remove(&lid);
}

// Do nothing for matches, etc. These can't escape.
_ => {}
}
self.set.remove(&lid);
}
}

Expand Down
22 changes: 0 additions & 22 deletions tests/ui/escape_analysis.stderr
Original file line number Diff line number Diff line change
@@ -1,22 +0,0 @@
error: local variable doesn't need to be boxed here
--> $DIR/escape_analysis.rs:39:13
|
LL | fn warn_arg(x: Box<A>) {
| ^
|
= note: `-D clippy::boxed-local` implied by `-D warnings`

error: local variable doesn't need to be boxed here
--> $DIR/escape_analysis.rs:130:12
|
LL | pub fn new(_needs_name: Box<PeekableSeekable<&()>>) -> () {}
| ^^^^^^^^^^^

error: local variable doesn't need to be boxed here
--> $DIR/escape_analysis.rs:170:23
|
LL | fn closure_borrow(x: Box<A>) {
| ^

error: aborting due to 3 previous errors

0 comments on commit b7d4735

Please sign in to comment.