2 use rustc::hir::intravisit as visit;
3 use rustc::hir::map::Node::{NodeExpr, NodeStmt};
5 use rustc::middle::expr_use_visitor::*;
6 use rustc::middle::mem_categorization::{cmt, Categorization};
7 use rustc::ty::adjustment::AutoAdjustment;
9 use rustc::util::nodemap::NodeSet;
10 use syntax::ast::NodeId;
11 use syntax::codemap::Span;
16 /// **What it does:** This lint checks for usage of `Box<T>` where an unboxed `T` would work fine.
18 /// **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.
20 /// **Known problems:** None
26 /// let x = Box::new(1);
28 /// println!("{}", *x);
32 pub BOXED_LOCAL, Warn, "using `Box<T>` where unnecessary"
35 fn is_non_trait_box(ty: ty::Ty) -> bool {
37 ty::TyBox(ref inner) => !inner.is_trait(),
42 struct EscapeDelegate<'a, 'tcx: 'a> {
43 tcx: ty::TyCtxt<'a, 'tcx, 'tcx>,
47 impl LintPass for Pass {
48 fn get_lints(&self) -> LintArray {
49 lint_array!(BOXED_LOCAL)
53 impl LateLintPass for Pass {
54 fn check_fn(&mut self, cx: &LateContext, _: visit::FnKind, decl: &FnDecl, body: &Block, _: Span, id: NodeId) {
55 let param_env = ty::ParameterEnvironment::for_item(cx.tcx, id);
57 let infcx = cx.tcx.borrowck_fake_infer_ctxt(param_env);
58 let mut v = EscapeDelegate {
64 let mut vis = ExprUseVisitor::new(&mut v, &infcx);
65 vis.walk_fn(decl, body);
71 cx.tcx.map.span(node),
72 "local variable doesn't need to be boxed here");
77 impl<'a, 'tcx: 'a> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {
78 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.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.tcx
140 if LoanCause::AutoRef == loan_cause {
142 if adj.autoderefs == 0 {
143 self.set.remove(&lid); // Used without autodereffing (i.e. x.clone())
146 span_bug!(cmt.span, "Unknown adjusted AutoRef");
148 } else if LoanCause::AddrOf == loan_cause {
150 if let Some(&AutoAdjustment::AdjustDerefRef(adj)) = self.tcx
156 .get_parent_node(borrow_id)) {
157 if adj.autoderefs <= 1 {
158 // foo(&x) where no extra autoreffing is happening
159 self.set.remove(&lid);
163 } else if LoanCause::MatchDiscriminant == loan_cause {
164 self.set.remove(&lid); // `match x` can move
166 // do nothing for matches, etc. These can't escape
170 fn decl_without_init(&mut self, _: NodeId, _: Span) {}
171 fn mutate(&mut self, _: NodeId, _: Span, _: cmt<'tcx>, _: MutateMode) {}