1 use rustc::front::map::Node::{NodeExpr, NodeStmt};
3 use rustc::middle::expr_use_visitor::*;
5 use rustc::middle::mem_categorization::{cmt, Categorization};
6 use rustc::traits::ProjectionMode;
7 use rustc::ty::adjustment::AutoAdjustment;
9 use rustc::util::nodemap::NodeSet;
10 use rustc_front::hir::*;
11 use rustc_front::intravisit as visit;
12 use syntax::ast::NodeId;
13 use syntax::codemap::Span;
16 pub struct EscapePass;
18 /// **What it does:** This lint checks for usage of `Box<T>` where an unboxed `T` would work fine.
20 /// **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.
22 /// **Known problems:** None
28 /// let x = Box::new(1);
30 /// println!("{}", *x);
34 pub BOXED_LOCAL, Warn, "using Box<T> where unnecessary"
37 fn is_non_trait_box(ty: ty::Ty) -> bool {
39 ty::TyBox(ref inner) => !inner.is_trait(),
44 struct EscapeDelegate<'a, 'tcx: 'a> {
45 cx: &'a LateContext<'a, 'tcx>,
49 impl LintPass for EscapePass {
50 fn get_lints(&self) -> LintArray {
51 lint_array!(BOXED_LOCAL)
55 impl LateLintPass for EscapePass {
56 fn check_fn(&mut self, cx: &LateContext, _: visit::FnKind, decl: &FnDecl, body: &Block, _: Span, id: NodeId) {
57 let param_env = ty::ParameterEnvironment::for_item(cx.tcx, id);
58 let infcx = infer::new_infer_ctxt(cx.tcx, &cx.tcx.tables, Some(param_env), ProjectionMode::Any);
59 let mut v = EscapeDelegate {
64 let mut vis = ExprUseVisitor::new(&mut v, &infcx);
65 vis.walk_fn(decl, body);
70 cx.tcx.map.span(node),
71 "local variable doesn't need to be boxed here");
76 impl<'a, 'tcx: 'a> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {
77 fn consume(&mut self, _: NodeId, _: Span, cmt: cmt<'tcx>, mode: ConsumeMode) {
79 if let Categorization::Local(lid) = cmt.cat {
80 if self.set.contains(&lid) {
81 if let Move(DirectRefMove) = mode {
82 // moved out or in. clearly can't be localized
83 self.set.remove(&lid);
88 fn matched_pat(&mut self, _: &Pat, _: cmt<'tcx>, _: MatchMode) {}
89 fn consume_pat(&mut self, consume_pat: &Pat, cmt: cmt<'tcx>, _: ConsumeMode) {
90 let map = &self.cx.tcx.map;
91 if map.is_argument(consume_pat.id) {
92 // Skip closure arguments
93 if let Some(NodeExpr(..)) = map.find(map.get_parent_node(consume_pat.id)) {
96 if is_non_trait_box(cmt.ty) {
97 self.set.insert(consume_pat.id);
101 if let Categorization::Rvalue(..) = cmt.cat {
102 if let Some(NodeStmt(st)) = map.find(map.get_parent_node(cmt.id)) {
103 if let StmtDecl(ref decl, _) = st.node {
104 if let DeclLocal(ref loc) = decl.node {
105 if let Some(ref ex) = loc.init {
106 if let ExprBox(..) = ex.node {
107 if is_non_trait_box(cmt.ty) {
109 self.set.insert(consume_pat.id);
113 // TODO "foo".to_owned() and friends
120 if let Categorization::Local(lid) = cmt.cat {
121 if self.set.contains(&lid) {
122 // let y = x where x is known
123 // remove x, insert y
124 self.set.insert(consume_pat.id);
125 self.set.remove(&lid);
130 fn borrow(&mut self, borrow_id: NodeId, _: Span, cmt: cmt<'tcx>, _: ty::Region, _: ty::BorrowKind,
131 loan_cause: LoanCause) {
133 if let Categorization::Local(lid) = cmt.cat {
134 if self.set.contains(&lid) {
135 if let Some(&AutoAdjustment::AdjustDerefRef(adj)) = self.cx
141 if LoanCause::AutoRef == loan_cause {
143 if adj.autoderefs == 0 {
144 self.set.remove(&lid); // Used without autodereffing (i.e. x.clone())
147 span_bug!(cmt.span, "Unknown adjusted AutoRef");
149 } else if LoanCause::AddrOf == loan_cause {
151 if let Some(&AutoAdjustment::AdjustDerefRef(adj)) = self.cx
159 .get_parent_node(borrow_id)) {
160 if adj.autoderefs <= 1 {
161 // foo(&x) where no extra autoreffing is happening
162 self.set.remove(&lid);
166 } else if LoanCause::MatchDiscriminant == loan_cause {
167 self.set.remove(&lid); // `match x` can move
169 // do nothing for matches, etc. These can't escape
173 fn decl_without_init(&mut self, _: NodeId, _: Span) {}
174 fn mutate(&mut self, _: NodeId, _: Span, _: cmt<'tcx>, _: MutateMode) {}