Skip to content

Extend escape analysis to arguments #523

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ pub struct EscapePass;
/// ```
declare_lint!(pub BOXED_LOCAL, Warn, "using Box<T> where unnecessary");

fn is_box(ty: ty::Ty) -> bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is only used in the true case, an if let ty::TyBox(..) { won't actually be too bad.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I extracted it to a function because 1. it is used in two places (for locals and for arguments) 2. we'd want it to match String and Vec in the future.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, ok then.

match ty.sty {
ty::TyBox(..) => true,
_ => false
}
}

struct EscapeDelegate<'a, 'tcx: 'a> {
cx: &'a LateContext<'a, 'tcx>,
set: NodeSet,
Expand Down Expand Up @@ -87,6 +94,12 @@ impl<'a, 'tcx: 'a> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {
}
fn matched_pat(&mut self, _: &Pat, _: cmt<'tcx>, _: MatchMode) {}
fn consume_pat(&mut self, consume_pat: &Pat, cmt: cmt<'tcx>, _: ConsumeMode) {
if self.cx.tcx.map.is_argument(consume_pat.id) {
if is_box(cmt.ty) {
self.set.insert(consume_pat.id);
}
return;
}
if let Categorization::Rvalue(..) = cmt.cat {
if let Some(Node::NodeStmt(st)) = self.cx
.tcx
Expand All @@ -96,7 +109,7 @@ impl<'a, 'tcx: 'a> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {
if let DeclLocal(ref loc) = decl.node {
if let Some(ref ex) = loc.init {
if let ExprBox(..) = ex.node {
if let ty::TyBox(..) = cmt.ty.sty {
if is_box(cmt.ty) {
// let x = box (...)
self.set.insert(consume_pat.id);
}
Expand Down
3 changes: 2 additions & 1 deletion tests/compile-fail/box_vec.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#![feature(plugin)]

#![plugin(clippy)]

#![deny(clippy)]
#![allow(boxed_local)]

pub fn test(foo: Box<Vec<bool>>) { //~ ERROR you seem to be trying to use `Box<Vec<T>>`
println!("{:?}", foo.get(0))
Expand Down
6 changes: 5 additions & 1 deletion tests/compile-fail/escape_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ fn warn_call() {
x.foo();
}

fn warn_arg(x: Box<A>) { //~ ERROR local variable
x.foo();
}

fn warn_rename_call() {
let x = box A;

Expand Down Expand Up @@ -78,4 +82,4 @@ fn warn_match() {
match &x { // not moved
ref y => ()
}
}
}