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.
32 /// let x = Box::new(1);
34 /// println!("{}", *x);
39 "using `Box<T>` where unnecessary"
42 fn is_non_trait_box(ty: Ty<'_>) -> bool {
43 ty.is_box() && !ty.boxed_ty().is_trait()
46 struct EscapeDelegate<'a, 'tcx> {
47 cx: &'a LateContext<'a, 'tcx>,
49 too_large_for_stack: u64,
52 impl_lint_pass!(BoxedLocal => [BOXED_LOCAL]);
54 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BoxedLocal {
57 cx: &LateContext<'a, 'tcx>,
58 _: visit::FnKind<'tcx>,
64 // If the method is an impl for a trait, don't warn.
65 let parent_id = cx.tcx.hir().get_parent_item(hir_id);
66 let parent_node = cx.tcx.hir().find(parent_id);
68 if let Some(Node::Item(item)) = parent_node {
69 if let ItemKind::Impl(_, _, _, _, Some(..), _, _) = item.node {
74 let mut v = EscapeDelegate {
76 set: HirIdSet::default(),
77 too_large_for_stack: self.too_large_for_stack,
80 let fn_def_id = cx.tcx.hir().local_def_id(hir_id);
81 let region_scope_tree = &cx.tcx.region_scope_tree(fn_def_id);
97 cx.tcx.hir().span(node),
98 "local variable doesn't need to be boxed here",
104 // TODO: Replace with Map::is_argument(..) when it's fixed
105 fn is_argument(map: &hir::map::Map<'_>, id: HirId) -> bool {
107 Some(Node::Binding(_)) => (),
111 match map.find(map.get_parent_node(id)) {
112 Some(Node::Arg(_)) => true,
117 impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {
118 fn consume(&mut self, _: HirId, _: Span, cmt: &cmt_<'tcx>, mode: ConsumeMode) {
119 if let Categorization::Local(lid) = cmt.cat {
120 if let Move(DirectRefMove) | Move(CaptureMove) = mode {
121 // moved out or in. clearly can't be localized
122 self.set.remove(&lid);
126 fn matched_pat(&mut self, _: &Pat, _: &cmt_<'tcx>, _: MatchMode) {}
127 fn consume_pat(&mut self, consume_pat: &Pat, cmt: &cmt_<'tcx>, _: ConsumeMode) {
128 let map = &self.cx.tcx.hir();
129 if is_argument(map, consume_pat.hir_id) {
130 // Skip closure arguments
131 let parent_id = map.get_parent_node(consume_pat.hir_id);
132 if let Some(Node::Expr(..)) = map.find(map.get_parent_node(parent_id)) {
136 if is_non_trait_box(cmt.ty) && !self.is_large_box(cmt.ty) {
137 self.set.insert(consume_pat.hir_id);
141 if let Categorization::Rvalue(..) = cmt.cat {
142 if let Some(Node::Stmt(st)) = map.find(map.get_parent_node(cmt.hir_id)) {
143 if let StmtKind::Local(ref loc) = st.node {
144 if let Some(ref ex) = loc.init {
145 if let ExprKind::Box(..) = ex.node {
146 if is_non_trait_box(cmt.ty) && !self.is_large_box(cmt.ty) {
148 self.set.insert(consume_pat.hir_id);
152 // TODO "foo".to_owned() and friends
158 if let Categorization::Local(lid) = cmt.cat {
159 if self.set.contains(&lid) {
160 // let y = x where x is known
161 // remove x, insert y
162 self.set.insert(consume_pat.hir_id);
163 self.set.remove(&lid);
174 loan_cause: LoanCause,
176 if let Categorization::Local(lid) = cmt.cat {
179 // Used without autoderef-ing (i.e., `x.clone()`).
183 // `foo(&x)` where no extra autoref-ing is happening.
186 // `match x` can move.
187 LoanCause::MatchDiscriminant => {
188 self.set.remove(&lid);
191 // Do nothing for matches, etc. These can't escape.
196 fn decl_without_init(&mut self, _: HirId, _: Span) {}
197 fn mutate(&mut self, _: HirId, _: Span, _: &cmt_<'tcx>, _: MutateMode) {}
200 impl<'a, 'tcx> EscapeDelegate<'a, 'tcx> {
201 fn is_large_box(&self, ty: Ty<'tcx>) -> bool {
202 // Large types need to be boxed to avoid stack overflows.
204 self.cx.layout_of(ty.boxed_ty()).ok().map_or(0, |l| l.size.bytes()) > self.too_large_for_stack