2 use rustc_front::hir::*;
3 use rustc_front::intravisit as visit;
4 use rustc::front::map::Node;
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
21 /// It is only necessary to allocate if you wish to move the box into something.
27 /// let x = Box::new(1);
29 /// println!("{}", *x);
32 declare_lint!(pub BOXED_LOCAL, Warn, "using Box<T> where unnecessary");
34 fn is_box(ty: ty::Ty) -> bool {
36 ty::TyBox(..) => true,
41 struct EscapeDelegate<'a, 'tcx: 'a> {
42 cx: &'a LateContext<'a, 'tcx>,
46 impl LintPass for EscapePass {
47 fn get_lints(&self) -> LintArray {
48 lint_array!(BOXED_LOCAL)
52 impl LateLintPass for EscapePass {
53 fn check_fn(&mut self,
60 let param_env = ty::ParameterEnvironment::for_item(cx.tcx, id);
61 let infcx = infer::new_infer_ctxt(cx.tcx, &cx.tcx.tables, Some(param_env), false);
62 let mut v = EscapeDelegate {
67 let mut vis = ExprUseVisitor::new(&mut v, &infcx);
68 vis.walk_fn(decl, body);
73 cx.tcx.map.span(node),
74 "local variable doesn't need to be boxed here");
79 impl<'a, 'tcx: 'a> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {
86 if let Categorization::Local(lid) = cmt.cat {
87 if self.set.contains(&lid) {
88 if let Move(DirectRefMove) = mode {
89 // moved out or in. clearly can't be localized
90 self.set.remove(&lid);
95 fn matched_pat(&mut self, _: &Pat, _: cmt<'tcx>, _: MatchMode) {}
96 fn consume_pat(&mut self, consume_pat: &Pat, cmt: cmt<'tcx>, _: ConsumeMode) {
97 if self.cx.tcx.map.is_argument(consume_pat.id) {
99 self.set.insert(consume_pat.id);
103 if let Categorization::Rvalue(..) = cmt.cat {
104 if let Some(Node::NodeStmt(st)) = self.cx
107 .find(self.cx.tcx.map.get_parent_node(cmt.id)) {
108 if let StmtDecl(ref decl, _) = st.node {
109 if let DeclLocal(ref loc) = decl.node {
110 if let Some(ref ex) = loc.init {
111 if let ExprBox(..) = ex.node {
114 self.set.insert(consume_pat.id);
118 // TODO "foo".to_owned() and friends
125 if let Categorization::Local(lid) = cmt.cat {
126 if self.set.contains(&lid) {
127 // let y = x where x is known
128 // remove x, insert y
129 self.set.insert(consume_pat.id);
130 self.set.remove(&lid);
141 loan_cause: LoanCause) {
143 if let Categorization::Local(lid) = cmt.cat {
144 if self.set.contains(&lid) {
145 if let Some(&AutoAdjustment::AdjustDerefRef(adj)) = self.cx
151 if LoanCause::AutoRef == loan_cause {
153 if adj.autoderefs <= 0 {
154 self.set.remove(&lid); // Used without autodereffing (i.e. x.clone())
157 self.cx.sess().span_bug(cmt.span, "Unknown adjusted AutoRef");
159 } else if LoanCause::AddrOf == loan_cause {
161 if let Some(&AutoAdjustment::AdjustDerefRef(adj)) =
162 self.cx.tcx.tables.borrow().adjustments
163 .get(&self.cx.tcx.map.get_parent_node(borrow_id)) {
164 if adj.autoderefs <= 1 {
165 // foo(&x) where no extra autoreffing is happening
166 self.set.remove(&lid);
170 } else if LoanCause::MatchDiscriminant == loan_cause {
171 self.set.remove(&lid); // `match x` can move
173 // do nothing for matches, etc. These can't escape
177 fn decl_without_init(&mut self, _: NodeId, _: Span) {}