1 use rustc::hir::intravisit as visit;
2 use rustc::hir::{self, *};
3 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
4 use rustc::middle::expr_use_visitor::*;
5 use rustc::middle::mem_categorization::{cmt_, Categorization};
6 use rustc::ty::layout::LayoutOf;
7 use rustc::ty::{self, Ty};
8 use rustc::util::nodemap::HirIdSet;
9 use rustc::{declare_tool_lint, impl_lint_pass};
10 use syntax::source_map::Span;
12 use crate::utils::span_lint;
14 #[derive(Copy, Clone)]
15 pub struct BoxedLocal {
16 pub too_large_for_stack: u64,
19 declare_clippy_lint! {
20 /// **What it does:** Checks for usage of `Box<T>` where an unboxed `T` would
23 /// **Why is this bad?** This is an unnecessary allocation, and bad for
24 /// performance. It is only necessary to allocate if you wish to move the box
27 /// **Known problems:** None.
31 /// # fn foo(bar: usize) {}
32 /// let x = Box::new(1);
34 /// println!("{}", *x);
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> {
46 cx: &'a LateContext<'a, 'tcx>,
48 too_large_for_stack: u64,
51 impl_lint_pass!(BoxedLocal => [BOXED_LOCAL]);
53 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BoxedLocal {
56 cx: &LateContext<'a, 'tcx>,
57 _: visit::FnKind<'tcx>,
63 // If the method is an impl for a trait, don't warn.
64 let parent_id = cx.tcx.hir().get_parent_item(hir_id);
65 let parent_node = cx.tcx.hir().find(parent_id);
67 if let Some(Node::Item(item)) = parent_node {
68 if let ItemKind::Impl(_, _, _, _, Some(..), _, _) = item.kind {
73 let mut v = EscapeDelegate {
75 set: HirIdSet::default(),
76 too_large_for_stack: self.too_large_for_stack,
79 let fn_def_id = cx.tcx.hir().local_def_id(hir_id);
80 let region_scope_tree = &cx.tcx.region_scope_tree(fn_def_id);
81 ExprUseVisitor::new(&mut v, cx.tcx, fn_def_id, cx.param_env, region_scope_tree, cx.tables).consume_body(body);
87 cx.tcx.hir().span(node),
88 "local variable doesn't need to be boxed here",
94 // TODO: Replace with Map::is_argument(..) when it's fixed
95 fn is_argument(map: &hir::map::Map<'_>, id: HirId) -> bool {
97 Some(Node::Binding(_)) => (),
101 match map.find(map.get_parent_node(id)) {
102 Some(Node::Param(_)) => true,
107 impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {
108 fn consume(&mut self, cmt: &cmt_<'tcx>, mode: ConsumeMode) {
109 if let Categorization::Local(lid) = cmt.cat {
110 if let ConsumeMode::Move = mode {
111 // moved out or in. clearly can't be localized
112 self.set.remove(&lid);
115 let map = &self.cx.tcx.hir();
116 if is_argument(map, cmt.hir_id) {
117 // Skip closure arguments
118 let parent_id = map.get_parent_node(cmt.hir_id);
119 if let Some(Node::Expr(..)) = map.find(map.get_parent_node(parent_id)) {
123 if is_non_trait_box(cmt.ty) && !self.is_large_box(cmt.ty) {
124 self.set.insert(cmt.hir_id);
128 if let Categorization::Local(lid) = cmt.cat {
129 if let Some(Node::Binding(_)) = map.find(cmt.hir_id) {
130 if self.set.contains(&lid) {
131 // let y = x where x is known
132 // remove x, insert y
133 self.set.insert(cmt.hir_id);
134 self.set.remove(&lid);
140 fn borrow(&mut self, cmt: &cmt_<'tcx>, _: ty::BorrowKind) {
141 if let Categorization::Local(lid) = cmt.cat {
142 self.set.remove(&lid);
146 fn mutate(&mut self, _: &cmt_<'tcx>) {}
149 impl<'a, 'tcx> EscapeDelegate<'a, 'tcx> {
150 fn is_large_box(&self, ty: Ty<'tcx>) -> bool {
151 // Large types need to be boxed to avoid stack overflows.
153 self.cx.layout_of(ty.boxed_ty()).ok().map_or(0, |l| l.size.bytes()) > self.too_large_for_stack