]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/escape.rs
align with rust-lang/rust/#58836
[rust.git] / clippy_lints / src / escape.rs
1 use crate::utils::span_lint;
2 use rustc::hir::intravisit as visit;
3 use rustc::hir::*;
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::HirIdSet;
10 use rustc::{declare_tool_lint, lint_array};
11 use syntax::source_map::Span;
12
13 pub struct Pass {
14     pub too_large_for_stack: u64,
15 }
16
17 /// **What it does:** Checks for usage of `Box<T>` where an unboxed `T` would
18 /// work fine.
19 ///
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
22 /// into something.
23 ///
24 /// **Known problems:** None.
25 ///
26 /// **Example:**
27 /// ```rust
28 /// fn main() {
29 ///     let x = Box::new(1);
30 ///     foo(*x);
31 ///     println!("{}", *x);
32 /// }
33 /// ```
34 declare_clippy_lint! {
35     pub BOXED_LOCAL,
36     perf,
37     "using `Box<T>` where unnecessary"
38 }
39
40 fn is_non_trait_box(ty: Ty<'_>) -> bool {
41     ty.is_box() && !ty.boxed_ty().is_trait()
42 }
43
44 struct EscapeDelegate<'a, 'tcx: 'a> {
45     cx: &'a LateContext<'a, 'tcx>,
46     set: HirIdSet,
47     too_large_for_stack: u64,
48 }
49
50 impl LintPass for Pass {
51     fn get_lints(&self) -> LintArray {
52         lint_array!(BOXED_LOCAL)
53     }
54
55     fn name(&self) -> &'static str {
56         "BoxedLocal"
57     }
58 }
59
60 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
61     fn check_fn(
62         &mut self,
63         cx: &LateContext<'a, 'tcx>,
64         _: visit::FnKind<'tcx>,
65         _: &'tcx FnDecl,
66         body: &'tcx Body,
67         _: Span,
68         hir_id: HirId,
69     ) {
70         // If the method is an impl for a trait, don't warn
71         let parent_id = cx.tcx.hir().get_parent_item(hir_id);
72         let parent_node = cx.tcx.hir().find_by_hir_id(parent_id);
73
74         if let Some(Node::Item(item)) = parent_node {
75             if let ItemKind::Impl(_, _, _, _, Some(..), _, _) = item.node {
76                 return;
77             }
78         }
79
80         let mut v = EscapeDelegate {
81             cx,
82             set: HirIdSet::default(),
83             too_large_for_stack: self.too_large_for_stack,
84         };
85
86         let fn_def_id = cx.tcx.hir().local_def_id_from_hir_id(hir_id);
87         let region_scope_tree = &cx.tcx.region_scope_tree(fn_def_id);
88         ExprUseVisitor::new(&mut v, cx.tcx, cx.param_env, region_scope_tree, cx.tables, None).consume_body(body);
89
90         for node in v.set {
91             span_lint(
92                 cx,
93                 BOXED_LOCAL,
94                 cx.tcx.hir().span_by_hir_id(node),
95                 "local variable doesn't need to be boxed here",
96             );
97         }
98     }
99 }
100
101 impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {
102     fn consume(&mut self, _: HirId, _: Span, cmt: &cmt_<'tcx>, mode: ConsumeMode) {
103         if let Categorization::Local(lid) = cmt.cat {
104             if let Move(DirectRefMove) = mode {
105                 // moved out or in. clearly can't be localized
106                 self.set.remove(&lid);
107             }
108         }
109     }
110     fn matched_pat(&mut self, _: &Pat, _: &cmt_<'tcx>, _: MatchMode) {}
111     fn consume_pat(&mut self, consume_pat: &Pat, cmt: &cmt_<'tcx>, _: ConsumeMode) {
112         let map = &self.cx.tcx.hir();
113         if map.is_argument(map.hir_to_node_id(consume_pat.hir_id)) {
114             // Skip closure arguments
115             if let Some(Node::Expr(..)) = map.find_by_hir_id(
116                 map.get_parent_node_by_hir_id(consume_pat.hir_id))
117             {
118                 return;
119             }
120             if is_non_trait_box(cmt.ty) && !self.is_large_box(cmt.ty) {
121                 self.set.insert(consume_pat.hir_id);
122             }
123             return;
124         }
125         if let Categorization::Rvalue(..) = cmt.cat {
126             let id = map.hir_to_node_id(cmt.hir_id);
127             if let Some(Node::Stmt(st)) = map.find(map.get_parent_node(id)) {
128                 if let StmtKind::Local(ref loc) = st.node {
129                     if let Some(ref ex) = loc.init {
130                         if let ExprKind::Box(..) = ex.node {
131                             if is_non_trait_box(cmt.ty) && !self.is_large_box(cmt.ty) {
132                                 // let x = box (...)
133                                 self.set.insert(consume_pat.hir_id);
134                             }
135                             // TODO Box::new
136                             // TODO vec![]
137                             // TODO "foo".to_owned() and friends
138                         }
139                     }
140                 }
141             }
142         }
143         if let Categorization::Local(lid) = cmt.cat {
144             if self.set.contains(&lid) {
145                 // let y = x where x is known
146                 // remove x, insert y
147                 self.set.insert(consume_pat.hir_id);
148                 self.set.remove(&lid);
149             }
150         }
151     }
152     fn borrow(
153         &mut self,
154         _: HirId,
155         _: Span,
156         cmt: &cmt_<'tcx>,
157         _: ty::Region<'_>,
158         _: ty::BorrowKind,
159         loan_cause: LoanCause,
160     ) {
161         if let Categorization::Local(lid) = cmt.cat {
162             match loan_cause {
163                 // x.foo()
164                 // Used without autodereffing (i.e. x.clone())
165                 LoanCause::AutoRef |
166
167                 // &x
168                 // foo(&x) where no extra autoreffing is happening
169                 LoanCause::AddrOf |
170
171                 // `match x` can move
172                 LoanCause::MatchDiscriminant => {
173                     self.set.remove(&lid);
174                 }
175
176                 // do nothing for matches, etc. These can't escape
177                 _ => {}
178             }
179         }
180     }
181     fn decl_without_init(&mut self, _: HirId, _: Span) {}
182     fn mutate(&mut self, _: HirId, _: Span, _: &cmt_<'tcx>, _: MutateMode) {}
183 }
184
185 impl<'a, 'tcx> EscapeDelegate<'a, 'tcx> {
186     fn is_large_box(&self, ty: Ty<'tcx>) -> bool {
187         // Large types need to be boxed to avoid stack
188         // overflows.
189         if ty.is_box() {
190             self.cx.layout_of(ty.boxed_ty()).ok().map_or(0, |l| l.size.bytes()) > self.too_large_for_stack
191         } else {
192             false
193         }
194     }
195 }