]> git.lizzy.rs Git - rust.git/blob - src/escape.rs
Merge pull request #481 from fhartwig/lifetime-with-alias
[rust.git] / src / escape.rs
1 use rustc::lint::*;
2 use rustc_front::hir::*;
3 use rustc_front::intravisit as visit;
4 use rustc::front::map::Node;
5 use rustc::middle::ty;
6 use rustc::middle::ty::adjustment::AutoAdjustment;
7 use rustc::middle::expr_use_visitor::*;
8 use rustc::middle::infer;
9 use rustc::middle::mem_categorization::{cmt, Categorization};
10 use rustc::util::nodemap::NodeSet;
11 use syntax::ast::NodeId;
12 use syntax::codemap::Span;
13 use utils::span_lint;
14
15 pub struct EscapePass;
16
17 declare_lint!(pub BOXED_LOCAL, Warn, "using Box<T> where unnecessary");
18
19 struct EscapeDelegate<'a, 'tcx: 'a> {
20     cx: &'a LateContext<'a, 'tcx>,
21     set: NodeSet,
22 }
23
24 impl LintPass for EscapePass {
25     fn get_lints(&self) -> LintArray {
26         lint_array!(BOXED_LOCAL)
27     }
28 }
29
30 impl LateLintPass for EscapePass {
31     fn check_fn(&mut self,
32                 cx: &LateContext,
33                 _: visit::FnKind,
34                 decl: &FnDecl,
35                 body: &Block,
36                 _: Span,
37                 id: NodeId) {
38         let param_env = ty::ParameterEnvironment::for_item(cx.tcx, id);
39         let infcx = infer::new_infer_ctxt(cx.tcx, &cx.tcx.tables, Some(param_env), false);
40         let mut v = EscapeDelegate {
41             cx: cx,
42             set: NodeSet(),
43         };
44         {
45             let mut vis = ExprUseVisitor::new(&mut v, &infcx);
46             vis.walk_fn(decl, body);
47         }
48         for node in v.set {
49             span_lint(cx,
50                       BOXED_LOCAL,
51                       cx.tcx.map.span(node),
52                       "local variable doesn't need to be boxed here");
53         }
54     }
55 }
56
57 impl<'a, 'tcx: 'a> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {
58     fn consume(&mut self,
59                _: NodeId,
60                _: Span,
61                cmt: cmt<'tcx>,
62                mode: ConsumeMode) {
63
64         if let Categorization::Local(lid) = cmt.cat {
65             if self.set.contains(&lid) {
66                 if let Move(DirectRefMove) = mode {
67                     // moved out or in. clearly can't be localized
68                     self.set.remove(&lid);
69                 }
70             }
71         }
72     }
73     fn matched_pat(&mut self, _: &Pat, _: cmt<'tcx>, _: MatchMode) {}
74     fn consume_pat(&mut self, consume_pat: &Pat, cmt: cmt<'tcx>, _: ConsumeMode) {
75         if let Categorization::Rvalue(..) = cmt.cat {
76             if let Some(Node::NodeStmt(st)) = self.cx
77                                                   .tcx
78                                                   .map
79                                                   .find(self.cx.tcx.map.get_parent_node(cmt.id)) {
80                 if let StmtDecl(ref decl, _) = st.node {
81                     if let DeclLocal(ref loc) = decl.node {
82                         if let Some(ref ex) = loc.init {
83                             if let ExprBox(..) = ex.node {
84                                 if let ty::TyBox(..) = cmt.ty.sty {
85                                     // let x = box (...)
86                                     self.set.insert(consume_pat.id);
87                                 }
88                                 // TODO Box::new
89                                 // TODO vec![]
90                                 // TODO "foo".to_owned() and friends
91                             }
92                         }
93                     }
94                 }
95             }
96         }
97         if let Categorization::Local(lid) = cmt.cat {
98             if self.set.contains(&lid) {
99                 // let y = x where x is known
100                 // remove x, insert y
101                 self.set.insert(consume_pat.id);
102                 self.set.remove(&lid);
103             }
104         }
105
106     }
107     fn borrow(&mut self,
108               borrow_id: NodeId,
109               _: Span,
110               cmt: cmt<'tcx>,
111               _: ty::Region,
112               _: ty::BorrowKind,
113               loan_cause: LoanCause) {
114
115         if let Categorization::Local(lid) = cmt.cat {
116             if self.set.contains(&lid) {
117                 if let Some(&AutoAdjustment::AdjustDerefRef(adj)) = self.cx
118                                                                         .tcx
119                                                                         .tables
120                                                                         .borrow()
121                                                                         .adjustments
122                                                                         .get(&borrow_id) {
123                     if LoanCause::AutoRef == loan_cause {
124                         // x.foo()
125                         if adj.autoderefs <= 0 {
126                             self.set.remove(&lid); // Used without autodereffing (i.e. x.clone())
127                         }
128                     } else {
129                         self.cx.sess().span_bug(cmt.span, "Unknown adjusted AutoRef");
130                     }
131                 } else if LoanCause::AddrOf == loan_cause {
132                     // &x
133                     if let Some(&AutoAdjustment::AdjustDerefRef(adj)) =
134                            self.cx.tcx.tables.borrow().adjustments
135                                .get(&self.cx.tcx.map.get_parent_node(borrow_id)) {
136                         if adj.autoderefs <= 1 {
137                             // foo(&x) where no extra autoreffing is happening
138                             self.set.remove(&lid);
139                         }
140                     }
141
142                 } else if LoanCause::MatchDiscriminant == loan_cause {
143                     self.set.remove(&lid); // `match x` can move
144                 }
145                 // do nothing for matches, etc. These can't escape
146             }
147         }
148     }
149     fn decl_without_init(&mut self, _: NodeId, _: Span) {}
150     fn mutate(&mut self,
151               _: NodeId,
152               _: Span,
153               _: cmt<'tcx>,
154               _: MutateMode) {
155     }
156 }