2 use rustc::front::map::Node::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. It is `Warn` by default.
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);
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, cx: &LateContext, _: visit::FnKind, decl: &FnDecl, body: &Block, _: Span, id: NodeId) {
54 let param_env = ty::ParameterEnvironment::for_item(cx.tcx, id);
55 let infcx = infer::new_infer_ctxt(cx.tcx, &cx.tcx.tables, Some(param_env));
56 let mut v = EscapeDelegate {
61 let mut vis = ExprUseVisitor::new(&mut v, &infcx);
62 vis.walk_fn(decl, body);
67 cx.tcx.map.span(node),
68 "local variable doesn't need to be boxed here");
73 impl<'a, 'tcx: 'a> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {
74 fn consume(&mut self, _: NodeId, _: Span, cmt: cmt<'tcx>, mode: ConsumeMode) {
76 if let Categorization::Local(lid) = cmt.cat {
77 if self.set.contains(&lid) {
78 if let Move(DirectRefMove) = mode {
79 // moved out or in. clearly can't be localized
80 self.set.remove(&lid);
85 fn matched_pat(&mut self, _: &Pat, _: cmt<'tcx>, _: MatchMode) {}
86 fn consume_pat(&mut self, consume_pat: &Pat, cmt: cmt<'tcx>, _: ConsumeMode) {
87 if self.cx.tcx.map.is_argument(consume_pat.id) {
89 self.set.insert(consume_pat.id);
93 if let Categorization::Rvalue(..) = cmt.cat {
94 if let Some(NodeStmt(st)) = self.cx
97 .find(self.cx.tcx.map.get_parent_node(cmt.id)) {
98 if let StmtDecl(ref decl, _) = st.node {
99 if let DeclLocal(ref loc) = decl.node {
100 if let Some(ref ex) = loc.init {
101 if let ExprBox(..) = ex.node {
104 self.set.insert(consume_pat.id);
108 // TODO "foo".to_owned() and friends
115 if let Categorization::Local(lid) = cmt.cat {
116 if self.set.contains(&lid) {
117 // let y = x where x is known
118 // remove x, insert y
119 self.set.insert(consume_pat.id);
120 self.set.remove(&lid);
125 fn borrow(&mut self, borrow_id: NodeId, _: Span, cmt: cmt<'tcx>, _: ty::Region, _: ty::BorrowKind,
126 loan_cause: LoanCause) {
128 if let Categorization::Local(lid) = cmt.cat {
129 if self.set.contains(&lid) {
130 if let Some(&AutoAdjustment::AdjustDerefRef(adj)) = self.cx
136 if LoanCause::AutoRef == loan_cause {
138 if adj.autoderefs <= 0 {
139 self.set.remove(&lid); // Used without autodereffing (i.e. x.clone())
142 self.cx.sess().span_bug(cmt.span, "Unknown adjusted AutoRef");
144 } else if LoanCause::AddrOf == loan_cause {
146 if let Some(&AutoAdjustment::AdjustDerefRef(adj)) = self.cx
154 .get_parent_node(borrow_id)) {
155 if adj.autoderefs <= 1 {
156 // foo(&x) where no extra autoreffing is happening
157 self.set.remove(&lid);
161 } else if LoanCause::MatchDiscriminant == loan_cause {
162 self.set.remove(&lid); // `match x` can move
164 // do nothing for matches, etc. These can't escape
168 fn decl_without_init(&mut self, _: NodeId, _: Span) {}
169 fn mutate(&mut self, _: NodeId, _: Span, _: cmt<'tcx>, _: MutateMode) {}