1 use rustc::hir::intravisit as visit;
2 use rustc::hir::{self, *};
3 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
4 use rustc_typeck::expr_use_visitor::*;
5 use rustc::ty::layout::LayoutOf;
6 use rustc::ty::{self, Ty};
7 use rustc::util::nodemap::HirIdSet;
8 use rustc::{declare_tool_lint, impl_lint_pass};
9 use syntax::source_map::Span;
11 use crate::utils::span_lint;
13 #[derive(Copy, Clone)]
14 pub struct BoxedLocal {
15 pub too_large_for_stack: u64,
18 declare_clippy_lint! {
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.
30 /// # fn foo(bar: usize) {}
31 /// let x = Box::new(1);
33 /// println!("{}", *x);
37 "using `Box<T>` where unnecessary"
40 fn is_non_trait_box(ty: Ty<'_>) -> bool {
41 ty.is_box() && !ty.boxed_ty().is_trait()
44 struct EscapeDelegate<'a, 'tcx> {
45 cx: &'a LateContext<'a, 'tcx>,
47 too_large_for_stack: u64,
50 impl_lint_pass!(BoxedLocal => [BOXED_LOCAL]);
52 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BoxedLocal {
55 cx: &LateContext<'a, 'tcx>,
56 _: visit::FnKind<'tcx>,
62 // If the method is an impl for a trait, don't warn.
63 let parent_id = cx.tcx.hir().get_parent_item(hir_id);
64 let parent_node = cx.tcx.hir().find(parent_id);
66 if let Some(Node::Item(item)) = parent_node {
67 if let ItemKind::Impl(_, _, _, _, Some(..), _, _) = item.kind {
72 let mut v = EscapeDelegate {
74 set: HirIdSet::default(),
75 too_large_for_stack: self.too_large_for_stack,
78 let fn_def_id = cx.tcx.hir().local_def_id(hir_id);
79 ExprUseVisitor::new(&mut v, cx.tcx, fn_def_id, cx.param_env, cx.tables).consume_body(body);
85 cx.tcx.hir().span(node),
86 "local variable doesn't need to be boxed here",
92 // TODO: Replace with Map::is_argument(..) when it's fixed
93 fn is_argument(map: &hir::map::Map<'_>, id: HirId) -> bool {
95 Some(Node::Binding(_)) => (),
99 match map.find(map.get_parent_node(id)) {
100 Some(Node::Param(_)) => true,
105 impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {
106 fn consume(&mut self, cmt: &Place<'tcx>, mode: ConsumeMode) {
107 if let Categorization::Local(lid) = cmt.cat {
108 if let ConsumeMode::Move = mode {
109 // moved out or in. clearly can't be localized
110 self.set.remove(&lid);
113 let map = &self.cx.tcx.hir();
114 if let Categorization::Local(lid) = cmt.cat {
115 if let Some(Node::Binding(_)) = map.find(cmt.hir_id) {
116 if self.set.contains(&lid) {
117 // let y = x where x is known
118 // remove x, insert y
119 self.set.insert(cmt.hir_id);
120 self.set.remove(&lid);
126 fn borrow(&mut self, cmt: &Place<'tcx>, _: ty::BorrowKind) {
127 if let Categorization::Local(lid) = cmt.cat {
128 self.set.remove(&lid);
132 fn mutate(&mut self, cmt: &Place<'tcx>) {
133 let map = &self.cx.tcx.hir();
134 if is_argument(map, cmt.hir_id) {
135 // Skip closure arguments
136 let parent_id = map.get_parent_node(cmt.hir_id);
137 if let Some(Node::Expr(..)) = map.find(map.get_parent_node(parent_id)) {
141 if is_non_trait_box(cmt.ty) && !self.is_large_box(cmt.ty) {
142 self.set.insert(cmt.hir_id);
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()).map_or(0, |l| l.size.bytes()) > self.too_large_for_stack