2 use rustc::front::map::Node::{NodeExpr, NodeStmt};
3 use rustc_front::hir::*;
4 use rustc_front::intravisit as visit;
6 use rustc::middle::ty::adjustment::AutoAdjustment;
7 use rustc::middle::expr_use_visitor::*;
8 use rustc::middle::infer;
9 use rustc::middle::mem_categorization::{cmt, Categorization};
10 use rustc::util::nodemap::NodeSet;
11 use syntax::ast::NodeId;
12 use syntax::codemap::Span;
15 pub struct EscapePass;
17 /// **What it does:** This lint checks for usage of `Box<T>` where an unboxed `T` would work fine.
19 /// **Why is this bad?** This is an unnecessary allocation, and bad for performance. It is only necessary to allocate if you wish to move the box into something.
21 /// **Known problems:** None
27 /// let x = Box::new(1);
29 /// println!("{}", *x);
33 pub BOXED_LOCAL, Warn, "using Box<T> where unnecessary"
36 fn is_non_trait_box(ty: ty::Ty) -> bool {
38 ty::TyBox(ref inner) => !inner.is_trait(),
43 struct EscapeDelegate<'a, 'tcx: 'a> {
44 cx: &'a LateContext<'a, 'tcx>,
48 impl LintPass for EscapePass {
49 fn get_lints(&self) -> LintArray {
50 lint_array!(BOXED_LOCAL)
54 impl LateLintPass for EscapePass {
55 fn check_fn(&mut self, cx: &LateContext, _: visit::FnKind, decl: &FnDecl, body: &Block, _: Span, id: NodeId) {
56 let param_env = ty::ParameterEnvironment::for_item(cx.tcx, id);
57 let infcx = infer::new_infer_ctxt(cx.tcx, &cx.tcx.tables, Some(param_env));
58 let mut v = EscapeDelegate {
63 let mut vis = ExprUseVisitor::new(&mut v, &infcx);
64 vis.walk_fn(decl, body);
69 cx.tcx.map.span(node),
70 "local variable doesn't need to be boxed here");
75 impl<'a, 'tcx: 'a> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {
76 fn consume(&mut self, _: NodeId, _: Span, cmt: cmt<'tcx>, mode: ConsumeMode) {
78 if let Categorization::Local(lid) = cmt.cat {
79 if self.set.contains(&lid) {
80 if let Move(DirectRefMove) = mode {
81 // moved out or in. clearly can't be localized
82 self.set.remove(&lid);
87 fn matched_pat(&mut self, _: &Pat, _: cmt<'tcx>, _: MatchMode) {}
88 fn consume_pat(&mut self, consume_pat: &Pat, cmt: cmt<'tcx>, _: ConsumeMode) {
89 let map = &self.cx.tcx.map;
90 if map.is_argument(consume_pat.id) {
91 // Skip closure arguments
92 if let Some(NodeExpr(..)) = map.find(map.get_parent_node(consume_pat.id)) {
95 if is_non_trait_box(cmt.ty) {
96 self.set.insert(consume_pat.id);
100 if let Categorization::Rvalue(..) = cmt.cat {
101 if let Some(NodeStmt(st)) = map.find(map.get_parent_node(cmt.id)) {
102 if let StmtDecl(ref decl, _) = st.node {
103 if let DeclLocal(ref loc) = decl.node {
104 if let Some(ref ex) = loc.init {
105 if let ExprBox(..) = ex.node {
106 if is_non_trait_box(cmt.ty) {
108 self.set.insert(consume_pat.id);
112 // TODO "foo".to_owned() and friends
119 if let Categorization::Local(lid) = cmt.cat {
120 if self.set.contains(&lid) {
121 // let y = x where x is known
122 // remove x, insert y
123 self.set.insert(consume_pat.id);
124 self.set.remove(&lid);
129 fn borrow(&mut self, borrow_id: NodeId, _: Span, cmt: cmt<'tcx>, _: ty::Region, _: ty::BorrowKind,
130 loan_cause: LoanCause) {
132 if let Categorization::Local(lid) = cmt.cat {
133 if self.set.contains(&lid) {
134 if let Some(&AutoAdjustment::AdjustDerefRef(adj)) = self.cx
140 if LoanCause::AutoRef == loan_cause {
142 if adj.autoderefs == 0 {
143 self.set.remove(&lid); // Used without autodereffing (i.e. x.clone())
146 self.cx.sess().span_bug(cmt.span, "Unknown adjusted AutoRef");
148 } else if LoanCause::AddrOf == loan_cause {
150 if let Some(&AutoAdjustment::AdjustDerefRef(adj)) = self.cx
158 .get_parent_node(borrow_id)) {
159 if adj.autoderefs <= 1 {
160 // foo(&x) where no extra autoreffing is happening
161 self.set.remove(&lid);
165 } else if LoanCause::MatchDiscriminant == loan_cause {
166 self.set.remove(&lid); // `match x` can move
168 // do nothing for matches, etc. These can't escape
172 fn decl_without_init(&mut self, _: NodeId, _: Span) {}
173 fn mutate(&mut self, _: NodeId, _: Span, _: cmt<'tcx>, _: MutateMode) {}