2 use rustc::hir::intravisit as visit;
3 use rustc::hir::map::Node::{NodeExpr, NodeStmt};
4 use rustc::infer::InferCtxt;
6 use rustc::middle::expr_use_visitor::*;
7 use rustc::middle::mem_categorization::{cmt, Categorization};
8 use rustc::ty::adjustment::AutoAdjustment;
10 use rustc::ty::layout::TargetDataLayout;
11 use rustc::util::nodemap::NodeSet;
12 use syntax::ast::NodeId;
13 use syntax::codemap::Span;
17 pub too_large_for_stack: u64,
20 /// **What it does:** This lint checks for usage of `Box<T>` where an unboxed `T` would work fine.
22 /// **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.
24 /// **Known problems:** None
30 /// let x = Box::new(1);
32 /// println!("{}", *x);
36 pub BOXED_LOCAL, Warn, "using `Box<T>` where unnecessary"
39 fn is_non_trait_box(ty: ty::Ty) -> bool {
41 ty::TyBox(ref inner) => !inner.is_trait(),
46 struct EscapeDelegate<'a, 'tcx: 'a+'gcx, 'gcx: 'a> {
47 tcx: ty::TyCtxt<'a, 'tcx, 'tcx>,
49 infcx: &'a InferCtxt<'a, 'gcx, 'gcx>,
50 target: TargetDataLayout,
51 too_large_for_stack: u64,
54 impl LintPass for Pass {
55 fn get_lints(&self) -> LintArray {
56 lint_array!(BOXED_LOCAL)
60 impl LateLintPass for Pass {
61 fn check_fn(&mut self, cx: &LateContext, _: visit::FnKind, decl: &FnDecl, body: &Block, _: Span, id: NodeId) {
62 let param_env = ty::ParameterEnvironment::for_item(cx.tcx, id);
64 let infcx = cx.tcx.borrowck_fake_infer_ctxt(param_env);
66 // we store the infcx because it is expensive to recreate
67 // the context each time.
68 let mut v = EscapeDelegate {
72 target: TargetDataLayout::parse(cx.sess()),
73 too_large_for_stack: self.too_large_for_stack,
77 let mut vis = ExprUseVisitor::new(&mut v, &infcx);
78 vis.walk_fn(decl, body);
84 cx.tcx.map.span(node),
85 "local variable doesn't need to be boxed here");
90 impl<'a, 'tcx: 'a+'gcx, 'gcx: 'a> Delegate<'tcx> for EscapeDelegate<'a, 'tcx, 'gcx> {
91 fn consume(&mut self, _: NodeId, _: Span, cmt: cmt<'tcx>, mode: ConsumeMode) {
92 if let Categorization::Local(lid) = cmt.cat {
93 if self.set.contains(&lid) {
94 if let Move(DirectRefMove) = mode {
95 // moved out or in. clearly can't be localized
96 self.set.remove(&lid);
101 fn matched_pat(&mut self, _: &Pat, _: cmt<'tcx>, _: MatchMode) {}
102 fn consume_pat(&mut self, consume_pat: &Pat, cmt: cmt<'tcx>, _: ConsumeMode) {
103 let map = &self.tcx.map;
104 if map.is_argument(consume_pat.id) {
105 // Skip closure arguments
106 if let Some(NodeExpr(..)) = map.find(map.get_parent_node(consume_pat.id)) {
109 if is_non_trait_box(cmt.ty) && !self.is_large_box(cmt.ty) {
110 self.set.insert(consume_pat.id);
114 if let Categorization::Rvalue(..) = cmt.cat {
115 if let Some(NodeStmt(st)) = map.find(map.get_parent_node(cmt.id)) {
116 if let StmtDecl(ref decl, _) = st.node {
117 if let DeclLocal(ref loc) = decl.node {
118 if let Some(ref ex) = loc.init {
119 if let ExprBox(..) = ex.node {
120 if is_non_trait_box(cmt.ty) && !self.is_large_box(cmt.ty) {
122 self.set.insert(consume_pat.id);
126 // TODO "foo".to_owned() and friends
133 if let Categorization::Local(lid) = cmt.cat {
134 if self.set.contains(&lid) {
135 // let y = x where x is known
136 // remove x, insert y
137 self.set.insert(consume_pat.id);
138 self.set.remove(&lid);
143 fn borrow(&mut self, borrow_id: NodeId, _: Span, cmt: cmt<'tcx>, _: ty::Region, _: ty::BorrowKind,
144 loan_cause: LoanCause) {
146 if let Categorization::Local(lid) = cmt.cat {
147 if self.set.contains(&lid) {
148 if let Some(&AutoAdjustment::AdjustDerefRef(adj)) = self.tcx
153 if LoanCause::AutoRef == loan_cause {
155 if adj.autoderefs == 0 {
156 self.set.remove(&lid); // Used without autodereffing (i.e. x.clone())
159 span_bug!(cmt.span, "Unknown adjusted AutoRef");
161 } else if LoanCause::AddrOf == loan_cause {
163 if let Some(&AutoAdjustment::AdjustDerefRef(adj)) = self.tcx
169 .get_parent_node(borrow_id)) {
170 if adj.autoderefs <= 1 {
171 // foo(&x) where no extra autoreffing is happening
172 self.set.remove(&lid);
176 } else if LoanCause::MatchDiscriminant == loan_cause {
177 self.set.remove(&lid); // `match x` can move
179 // do nothing for matches, etc. These can't escape
183 fn decl_without_init(&mut self, _: NodeId, _: Span) {}
184 fn mutate(&mut self, _: NodeId, _: Span, _: cmt<'tcx>, _: MutateMode) {}
187 impl<'a, 'tcx: 'a+'gcx, 'gcx: 'a> EscapeDelegate<'a, 'tcx, 'gcx> {
188 fn is_large_box(&self, ty: ty::Ty<'gcx>) -> bool {
189 // Large types need to be boxed to avoid stack
192 ty::TyBox(ref inner) => {
193 if let Ok(layout) = inner.layout(self.infcx) {
194 let size = layout.size(&self.target);
195 size.bytes() > self.too_large_for_stack