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 LateLintPass for Pass {
64 fn check_fn(&mut self, cx: &LateContext, _: visit::FnKind, decl: &FnDecl, body: &Expr, _: Span, id: NodeId) {
65 let param_env = ty::ParameterEnvironment::for_item(cx.tcx, id);
67 let infcx = cx.tcx.borrowck_fake_infer_ctxt(param_env);
69 // we store the infcx because it is expensive to recreate
70 // the context each time.
71 let mut v = EscapeDelegate {
75 target: TargetDataLayout::parse(cx.sess()),
76 too_large_for_stack: self.too_large_for_stack,
80 let mut vis = ExprUseVisitor::new(&mut v, &infcx);
81 vis.walk_fn(decl, body);
87 cx.tcx.map.span(node),
88 "local variable doesn't need to be boxed here");
93 impl<'a, 'tcx: 'a+'gcx, 'gcx: 'a> Delegate<'tcx> for EscapeDelegate<'a, 'tcx, 'gcx> {
94 fn consume(&mut self, _: NodeId, _: Span, cmt: cmt<'tcx>, mode: ConsumeMode) {
95 if let Categorization::Local(lid) = cmt.cat {
96 if self.set.contains(&lid) {
97 if let Move(DirectRefMove) = mode {
98 // moved out or in. clearly can't be localized
99 self.set.remove(&lid);
104 fn matched_pat(&mut self, _: &Pat, _: cmt<'tcx>, _: MatchMode) {}
105 fn consume_pat(&mut self, consume_pat: &Pat, cmt: cmt<'tcx>, _: ConsumeMode) {
106 let map = &self.tcx.map;
107 if map.is_argument(consume_pat.id) {
108 // Skip closure arguments
109 if let Some(NodeExpr(..)) = map.find(map.get_parent_node(consume_pat.id)) {
112 if is_non_trait_box(cmt.ty) && !self.is_large_box(cmt.ty) {
113 self.set.insert(consume_pat.id);
117 if let Categorization::Rvalue(..) = cmt.cat {
118 if let Some(NodeStmt(st)) = map.find(map.get_parent_node(cmt.id)) {
119 if let StmtDecl(ref decl, _) = st.node {
120 if let DeclLocal(ref loc) = decl.node {
121 if let Some(ref ex) = loc.init {
122 if let ExprBox(..) = ex.node {
123 if is_non_trait_box(cmt.ty) && !self.is_large_box(cmt.ty) {
125 self.set.insert(consume_pat.id);
129 // TODO "foo".to_owned() and friends
136 if let Categorization::Local(lid) = cmt.cat {
137 if self.set.contains(&lid) {
138 // let y = x where x is known
139 // remove x, insert y
140 self.set.insert(consume_pat.id);
141 self.set.remove(&lid);
146 fn borrow(&mut self, borrow_id: NodeId, _: Span, cmt: cmt<'tcx>, _: &ty::Region, _: ty::BorrowKind,
147 loan_cause: LoanCause) {
148 use rustc::ty::adjustment::Adjust;
150 if let Categorization::Local(lid) = cmt.cat {
151 if self.set.contains(&lid) {
152 if let Some(&Adjust::DerefRef { autoderefs, .. }) = self.tcx
158 if LoanCause::AutoRef == loan_cause {
161 self.set.remove(&lid); // Used without autodereffing (i.e. x.clone())
164 span_bug!(cmt.span, "Unknown adjusted AutoRef");
166 } else if LoanCause::AddrOf == loan_cause {
168 if let Some(&Adjust::DerefRef { autoderefs, .. }) = self.tcx
174 .get_parent_node(borrow_id))
177 // foo(&x) where no extra autoreffing is happening
178 self.set.remove(&lid);
182 } else if LoanCause::MatchDiscriminant == loan_cause {
183 self.set.remove(&lid); // `match x` can move
185 // do nothing for matches, etc. These can't escape
189 fn decl_without_init(&mut self, _: NodeId, _: Span) {}
190 fn mutate(&mut self, _: NodeId, _: Span, _: cmt<'tcx>, _: MutateMode) {}
193 impl<'a, 'tcx: 'a+'gcx, 'gcx: 'a> EscapeDelegate<'a, 'tcx, 'gcx> {
194 fn is_large_box(&self, ty: ty::Ty<'gcx>) -> bool {
195 // Large types need to be boxed to avoid stack
198 ty::TyBox(inner) => {
199 if let Ok(layout) = inner.layout(self.infcx) {
200 let size = layout.size(&self.target);
201 size.bytes() > self.too_large_for_stack