1 use crate::utils::span_lint;
2 use rustc::hir::intravisit as visit;
4 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
5 use rustc::middle::expr_use_visitor::*;
6 use rustc::middle::mem_categorization::{cmt_, Categorization};
7 use rustc::ty::layout::LayoutOf;
8 use rustc::ty::{self, Ty};
9 use rustc::util::nodemap::NodeSet;
10 use rustc::{declare_tool_lint, lint_array};
11 use syntax::ast::NodeId;
12 use syntax::source_map::Span;
15 pub too_large_for_stack: u64,
18 /// **What it does:** Checks for usage of `Box<T>` where an unboxed `T` would
21 /// **Why is this bad?** This is an unnecessary allocation, and bad for
22 /// performance. It is only necessary to allocate if you wish to move the box
25 /// **Known problems:** None.
30 /// let x = Box::new(1);
32 /// println!("{}", *x);
35 declare_clippy_lint! {
38 "using `Box<T>` where unnecessary"
41 fn is_non_trait_box(ty: Ty<'_>) -> bool {
42 ty.is_box() && !ty.boxed_ty().is_trait()
45 struct EscapeDelegate<'a, 'tcx: 'a> {
46 cx: &'a LateContext<'a, '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 // If the method is an impl for a trait, don't warn
68 let parent_id = cx.tcx.hir().get_parent(node_id);
69 let parent_node = cx.tcx.hir().find(parent_id);
71 if let Some(Node::Item(item)) = parent_node {
72 if let ItemKind::Impl(_, _, _, _, Some(..), _, _) = item.node {
77 let mut v = EscapeDelegate {
79 set: NodeSet::default(),
80 too_large_for_stack: self.too_large_for_stack,
83 let fn_def_id = cx.tcx.hir().local_def_id(node_id);
84 let region_scope_tree = &cx.tcx.region_scope_tree(fn_def_id);
85 ExprUseVisitor::new(&mut v, cx.tcx, cx.param_env, region_scope_tree, cx.tables, None).consume_body(body);
91 cx.tcx.hir().span(node),
92 "local variable doesn't need to be boxed here",
98 impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {
99 fn consume(&mut self, _: NodeId, _: Span, cmt: &cmt_<'tcx>, mode: ConsumeMode) {
100 if let Categorization::Local(lid) = cmt.cat {
101 if let Move(DirectRefMove) = mode {
102 // moved out or in. clearly can't be localized
103 self.set.remove(&lid);
107 fn matched_pat(&mut self, _: &Pat, _: &cmt_<'tcx>, _: MatchMode) {}
108 fn consume_pat(&mut self, consume_pat: &Pat, cmt: &cmt_<'tcx>, _: ConsumeMode) {
109 let map = &self.cx.tcx.hir();
110 if map.is_argument(consume_pat.id) {
111 // Skip closure arguments
112 if let Some(Node::Expr(..)) = map.find(map.get_parent_node(consume_pat.id)) {
115 if is_non_trait_box(cmt.ty) && !self.is_large_box(cmt.ty) {
116 self.set.insert(consume_pat.id);
120 if let Categorization::Rvalue(..) = cmt.cat {
121 let id = map.hir_to_node_id(cmt.hir_id);
122 if let Some(Node::Stmt(st)) = map.find(map.get_parent_node(id)) {
123 if let StmtKind::Local(ref loc) = st.node {
124 if let Some(ref ex) = loc.init {
125 if let ExprKind::Box(..) = ex.node {
126 if is_non_trait_box(cmt.ty) && !self.is_large_box(cmt.ty) {
128 self.set.insert(consume_pat.id);
132 // TODO "foo".to_owned() and friends
138 if let Categorization::Local(lid) = cmt.cat {
139 if self.set.contains(&lid) {
140 // let y = x where x is known
141 // remove x, insert y
142 self.set.insert(consume_pat.id);
143 self.set.remove(&lid);
154 loan_cause: LoanCause,
156 if let Categorization::Local(lid) = cmt.cat {
159 // Used without autodereffing (i.e. x.clone())
163 // foo(&x) where no extra autoreffing is happening
166 // `match x` can move
167 LoanCause::MatchDiscriminant => {
168 self.set.remove(&lid);
171 // do nothing for matches, etc. These can't escape
176 fn decl_without_init(&mut self, _: NodeId, _: Span) {}
177 fn mutate(&mut self, _: NodeId, _: Span, _: &cmt_<'tcx>, _: MutateMode) {}
180 impl<'a, 'tcx> EscapeDelegate<'a, 'tcx> {
181 fn is_large_box(&self, ty: Ty<'tcx>) -> bool {
182 // Large types need to be boxed to avoid stack
185 self.cx.layout_of(ty.boxed_ty()).ok().map_or(0, |l| l.size.bytes()) > self.too_large_for_stack