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};
8 use rustc::util::nodemap::NodeSet;
9 use syntax::ast::NodeId;
10 use syntax::codemap::Span;
14 pub too_large_for_stack: u64,
17 /// **What it does:** Checks for usage of `Box<T>` where an unboxed `T` would
20 /// **Why is this bad?** This is an unnecessary allocation, and bad for
21 /// performance. It is only necessary to allocate if you wish to move the box
24 /// **Known problems:** None.
29 /// let x = Box::new(1);
31 /// println!("{}", *x);
37 "using `Box<T>` where unnecessary"
40 fn is_non_trait_box(ty: ty::Ty) -> bool {
41 ty.is_box() && !ty.boxed_ty().is_trait()
44 struct EscapeDelegate<'a, 'tcx: 'a> {
46 tcx: ty::TyCtxt<'a, 'tcx, 'tcx>,
47 param_env: ty::ParamEnv<'tcx>,
48 too_large_for_stack: u64,
51 impl LintPass for Pass {
52 fn get_lints(&self) -> LintArray {
53 lint_array!(BOXED_LOCAL)
57 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
60 cx: &LateContext<'a, 'tcx>,
61 _: visit::FnKind<'tcx>,
67 let fn_def_id = cx.tcx.hir.local_def_id(node_id);
68 let param_env = cx.tcx.param_env(fn_def_id).reveal_all();
69 let mut v = EscapeDelegate {
73 too_large_for_stack: self.too_large_for_stack,
76 cx.tcx.infer_ctxt(body.id()).enter(|infcx| {
77 let region_maps = &cx.tcx.region_maps(fn_def_id);
78 let mut vis = ExprUseVisitor::new(&mut v, region_maps, &infcx, param_env);
79 vis.consume_body(body);
85 cx.tcx.hir.span(node),
86 "local variable doesn't need to be boxed here");
91 impl<'a, 'gcx: 'tcx, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'gcx> {
92 fn consume(&mut self, _: NodeId, _: Span, cmt: cmt<'tcx>, mode: ConsumeMode) {
93 if let Categorization::Local(lid) = cmt.cat {
94 if let Move(DirectRefMove) = mode {
95 // moved out or in. clearly can't be localized
96 self.set.remove(&lid);
100 fn matched_pat(&mut self, _: &Pat, _: cmt<'tcx>, _: MatchMode) {}
101 fn consume_pat(&mut self, consume_pat: &Pat, cmt: cmt<'tcx>, _: ConsumeMode) {
102 let map = &self.tcx.hir;
103 if map.is_argument(consume_pat.id) {
104 // Skip closure arguments
105 if let Some(NodeExpr(..)) = map.find(map.get_parent_node(consume_pat.id)) {
108 if is_non_trait_box(cmt.ty) && !self.is_large_box(cmt.ty) {
109 self.set.insert(consume_pat.id);
113 if let Categorization::Rvalue(..) = cmt.cat {
114 if let Some(NodeStmt(st)) = map.find(map.get_parent_node(cmt.id)) {
115 if let StmtDecl(ref decl, _) = st.node {
116 if let DeclLocal(ref loc) = decl.node {
117 if let Some(ref ex) = loc.init {
118 if let ExprBox(..) = ex.node {
119 if is_non_trait_box(cmt.ty) && !self.is_large_box(cmt.ty) {
121 self.set.insert(consume_pat.id);
125 // TODO "foo".to_owned() and friends
132 if let Categorization::Local(lid) = cmt.cat {
133 if self.set.contains(&lid) {
134 // let y = x where x is known
135 // remove x, insert y
136 self.set.insert(consume_pat.id);
137 self.set.remove(&lid);
149 loan_cause: LoanCause
151 if let Categorization::Local(lid) = cmt.cat {
154 // Used without autodereffing (i.e. x.clone())
158 // foo(&x) where no extra autoreffing is happening
161 // `match x` can move
162 LoanCause::MatchDiscriminant => {
163 self.set.remove(&lid);
166 // do nothing for matches, etc. These can't escape
171 fn decl_without_init(&mut self, _: NodeId, _: Span) {}
172 fn mutate(&mut self, _: NodeId, _: Span, _: cmt<'tcx>, _: MutateMode) {}
175 impl<'a, 'tcx: 'a> EscapeDelegate<'a, 'tcx> {
176 fn is_large_box(&self, ty: ty::Ty) -> bool {
177 // Large types need to be boxed to avoid stack
180 if let Some(inner) = self.tcx.lift(&ty.boxed_ty()) {
181 if let Ok(layout) = inner.layout(self.tcx, self.param_env) {
182 return layout.size(self.tcx).bytes() > self.too_large_for_stack;