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};
9 use rustc::ty::layout::TargetDataLayout;
10 use rustc::util::nodemap::NodeSet;
11 use syntax::ast::NodeId;
12 use syntax::codemap::Span;
16 pub too_large_for_stack: u64,
19 /// **What it does:** Checks for usage of `Box<T>` where an unboxed `T` would
22 /// **Why is this bad?** This is an unnecessary allocation, and bad for
23 /// performance. It is only necessary to allocate if you wish to move the box
26 /// **Known problems:** None.
31 /// let x = Box::new(1);
33 /// println!("{}", *x);
39 "using `Box<T>` where unnecessary"
42 fn is_non_trait_box(ty: ty::Ty) -> bool {
44 ty::TyBox(inner) => !inner.is_trait(),
49 struct EscapeDelegate<'a, 'tcx: 'a + 'gcx, 'gcx: 'a> {
50 tcx: ty::TyCtxt<'a, 'tcx, 'tcx>,
52 infcx: &'a InferCtxt<'a, 'gcx, 'gcx>,
53 target: TargetDataLayout,
54 too_large_for_stack: u64,
57 impl LintPass for Pass {
58 fn get_lints(&self) -> LintArray {
59 lint_array!(BOXED_LOCAL)
63 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
66 cx: &LateContext<'a, 'tcx>,
67 _: visit::FnKind<'tcx>,
73 let param_env = ty::ParameterEnvironment::for_item(cx.tcx, id);
75 let infcx = cx.tcx.borrowck_fake_infer_ctxt(param_env);
77 // we store the infcx because it is expensive to recreate
78 // the context each time.
79 let mut v = EscapeDelegate {
83 target: TargetDataLayout::parse(cx.sess()),
84 too_large_for_stack: self.too_large_for_stack,
88 let mut vis = ExprUseVisitor::new(&mut v, &infcx);
89 vis.walk_fn(decl, body);
95 cx.tcx.map.span(node),
96 "local variable doesn't need to be boxed here");
101 impl<'a, 'tcx: 'a + 'gcx, 'gcx: 'a> Delegate<'tcx> for EscapeDelegate<'a, 'tcx, 'gcx> {
102 fn consume(&mut self, _: NodeId, _: Span, cmt: cmt<'tcx>, mode: ConsumeMode) {
103 if let Categorization::Local(lid) = cmt.cat {
104 if self.set.contains(&lid) {
105 if let Move(DirectRefMove) = mode {
106 // moved out or in. clearly can't be localized
107 self.set.remove(&lid);
112 fn matched_pat(&mut self, _: &Pat, _: cmt<'tcx>, _: MatchMode) {}
113 fn consume_pat(&mut self, consume_pat: &Pat, cmt: cmt<'tcx>, _: ConsumeMode) {
114 let map = &self.tcx.map;
115 if map.is_argument(consume_pat.id) {
116 // Skip closure arguments
117 if let Some(NodeExpr(..)) = map.find(map.get_parent_node(consume_pat.id)) {
120 if is_non_trait_box(cmt.ty) && !self.is_large_box(cmt.ty) {
121 self.set.insert(consume_pat.id);
125 if let Categorization::Rvalue(..) = cmt.cat {
126 if let Some(NodeStmt(st)) = map.find(map.get_parent_node(cmt.id)) {
127 if let StmtDecl(ref decl, _) = st.node {
128 if let DeclLocal(ref loc) = decl.node {
129 if let Some(ref ex) = loc.init {
130 if let ExprBox(..) = ex.node {
131 if is_non_trait_box(cmt.ty) && !self.is_large_box(cmt.ty) {
133 self.set.insert(consume_pat.id);
137 // TODO "foo".to_owned() and friends
144 if let Categorization::Local(lid) = cmt.cat {
145 if self.set.contains(&lid) {
146 // let y = x where x is known
147 // remove x, insert y
148 self.set.insert(consume_pat.id);
149 self.set.remove(&lid);
161 loan_cause: LoanCause
163 use rustc::ty::adjustment::Adjust;
165 if let Categorization::Local(lid) = cmt.cat {
166 if self.set.contains(&lid) {
167 if let Some(&Adjust::DerefRef { autoderefs, .. }) =
174 if LoanCause::AutoRef == loan_cause {
177 self.set.remove(&lid); // Used without autodereffing (i.e. x.clone())
180 span_bug!(cmt.span, "Unknown adjusted AutoRef");
182 } else if LoanCause::AddrOf == loan_cause {
184 if let Some(&Adjust::DerefRef { autoderefs, .. }) =
191 .get_parent_node(borrow_id))
194 // foo(&x) where no extra autoreffing is happening
195 self.set.remove(&lid);
199 } else if LoanCause::MatchDiscriminant == loan_cause {
200 self.set.remove(&lid); // `match x` can move
202 // do nothing for matches, etc. These can't escape
206 fn decl_without_init(&mut self, _: NodeId, _: Span) {}
207 fn mutate(&mut self, _: NodeId, _: Span, _: cmt<'tcx>, _: MutateMode) {}
210 impl<'a, 'tcx: 'a + 'gcx, 'gcx: 'a> EscapeDelegate<'a, 'tcx, 'gcx> {
211 fn is_large_box(&self, ty: ty::Ty<'gcx>) -> bool {
212 // Large types need to be boxed to avoid stack
215 ty::TyBox(inner) => {
216 if let Ok(layout) = inner.layout(self.infcx) {
217 let size = layout.size(&self.target);
218 size.bytes() > self.too_large_for_stack