]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/utils/hir_utils.rs
Merge pull request #3303 from shssoichiro/3069-unnecessary-fold-pattern-guard
[rust.git] / clippy_lints / src / utils / hir_utils.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10
11 use crate::consts::{constant_simple, constant_context};
12 use crate::rustc::lint::LateContext;
13 use crate::rustc::hir::*;
14 use crate::rustc::ty::{TypeckTables};
15 use std::hash::{Hash, Hasher};
16 use std::collections::hash_map::DefaultHasher;
17 use crate::syntax::ast::Name;
18 use crate::syntax::ptr::P;
19 use crate::utils::differing_macro_contexts;
20
21 /// Type used to check whether two ast are the same. This is different from the
22 /// operator
23 /// `==` on ast types as this operator would compare true equality with ID and
24 /// span.
25 ///
26 /// Note that some expressions kinds are not considered but could be added.
27 pub struct SpanlessEq<'a, 'tcx: 'a> {
28     /// Context used to evaluate constant expressions.
29     cx: &'a LateContext<'a, 'tcx>,
30     tables: &'a TypeckTables<'tcx>,
31     /// If is true, never consider as equal expressions containing function
32     /// calls.
33     ignore_fn: bool,
34 }
35
36 impl<'a, 'tcx: 'a> SpanlessEq<'a, 'tcx> {
37     pub fn new(cx: &'a LateContext<'a, 'tcx>) -> Self {
38         Self {
39             cx,
40             tables: cx.tables,
41             ignore_fn: false,
42         }
43     }
44
45     pub fn ignore_fn(self) -> Self {
46         Self {
47             cx: self.cx,
48             tables: self.cx.tables,
49             ignore_fn: true,
50         }
51     }
52
53     /// Check whether two statements are the same.
54     pub fn eq_stmt(&mut self, left: &Stmt, right: &Stmt) -> bool {
55         match (&left.node, &right.node) {
56             (&StmtKind::Decl(ref l, _), &StmtKind::Decl(ref r, _)) => {
57                 if let (&DeclKind::Local(ref l), &DeclKind::Local(ref r)) = (&l.node, &r.node) {
58                     both(&l.ty, &r.ty, |l, r| self.eq_ty(l, r)) && both(&l.init, &r.init, |l, r| self.eq_expr(l, r))
59                 } else {
60                     false
61                 }
62             },
63             (&StmtKind::Expr(ref l, _), &StmtKind::Expr(ref r, _)) | (&StmtKind::Semi(ref l, _), &StmtKind::Semi(ref r, _)) => {
64                 self.eq_expr(l, r)
65             },
66             _ => false,
67         }
68     }
69
70     /// Check whether two blocks are the same.
71     pub fn eq_block(&mut self, left: &Block, right: &Block) -> bool {
72         over(&left.stmts, &right.stmts, |l, r| self.eq_stmt(l, r))
73             && both(&left.expr, &right.expr, |l, r| self.eq_expr(l, r))
74     }
75
76     #[allow(clippy::similar_names)]
77     pub fn eq_expr(&mut self, left: &Expr, right: &Expr) -> bool {
78         if self.ignore_fn && differing_macro_contexts(left.span, right.span) {
79             return false;
80         }
81
82         if let (Some(l), Some(r)) = (constant_simple(self.cx, self.tables, left), constant_simple(self.cx, self.tables, right)) {
83             if l == r {
84                 return true;
85             }
86         }
87
88         match (&left.node, &right.node) {
89             (&ExprKind::AddrOf(l_mut, ref le), &ExprKind::AddrOf(r_mut, ref re)) => l_mut == r_mut && self.eq_expr(le, re),
90             (&ExprKind::Continue(li), &ExprKind::Continue(ri)) => {
91                 both(&li.label, &ri.label, |l, r| l.ident.as_str() == r.ident.as_str())
92             },
93             (&ExprKind::Assign(ref ll, ref lr), &ExprKind::Assign(ref rl, ref rr)) => self.eq_expr(ll, rl) && self.eq_expr(lr, rr),
94             (&ExprKind::AssignOp(ref lo, ref ll, ref lr), &ExprKind::AssignOp(ref ro, ref rl, ref rr)) => {
95                 lo.node == ro.node && self.eq_expr(ll, rl) && self.eq_expr(lr, rr)
96             },
97             (&ExprKind::Block(ref l, _), &ExprKind::Block(ref r, _)) => self.eq_block(l, r),
98             (&ExprKind::Binary(l_op, ref ll, ref lr), &ExprKind::Binary(r_op, ref rl, ref rr)) => {
99                 l_op.node == r_op.node && self.eq_expr(ll, rl) && self.eq_expr(lr, rr)
100                     || swap_binop(l_op.node, ll, lr).map_or(false, |(l_op, ll, lr)| {
101                         l_op == r_op.node && self.eq_expr(ll, rl) && self.eq_expr(lr, rr)
102                     })
103             },
104             (&ExprKind::Break(li, ref le), &ExprKind::Break(ri, ref re)) => {
105                 both(&li.label, &ri.label, |l, r| l.ident.as_str() == r.ident.as_str())
106                     && both(le, re, |l, r| self.eq_expr(l, r))
107             },
108             (&ExprKind::Box(ref l), &ExprKind::Box(ref r)) => self.eq_expr(l, r),
109             (&ExprKind::Call(ref l_fun, ref l_args), &ExprKind::Call(ref r_fun, ref r_args)) => {
110                 !self.ignore_fn && self.eq_expr(l_fun, r_fun) && self.eq_exprs(l_args, r_args)
111             },
112             (&ExprKind::Cast(ref lx, ref lt), &ExprKind::Cast(ref rx, ref rt)) |
113             (&ExprKind::Type(ref lx, ref lt), &ExprKind::Type(ref rx, ref rt)) => self.eq_expr(lx, rx) && self.eq_ty(lt, rt),
114             (&ExprKind::Field(ref l_f_exp, ref l_f_ident), &ExprKind::Field(ref r_f_exp, ref r_f_ident)) => {
115                 l_f_ident.name == r_f_ident.name && self.eq_expr(l_f_exp, r_f_exp)
116             },
117             (&ExprKind::Index(ref la, ref li), &ExprKind::Index(ref ra, ref ri)) => self.eq_expr(la, ra) && self.eq_expr(li, ri),
118             (&ExprKind::If(ref lc, ref lt, ref le), &ExprKind::If(ref rc, ref rt, ref re)) => {
119                 self.eq_expr(lc, rc) && self.eq_expr(&**lt, &**rt) && both(le, re, |l, r| self.eq_expr(l, r))
120             },
121             (&ExprKind::Lit(ref l), &ExprKind::Lit(ref r)) => l.node == r.node,
122             (&ExprKind::Loop(ref lb, ref ll, ref lls), &ExprKind::Loop(ref rb, ref rl, ref rls)) => {
123                 lls == rls && self.eq_block(lb, rb) && both(ll, rl, |l, r| l.ident.as_str() == r.ident.as_str())
124             },
125             (&ExprKind::Match(ref le, ref la, ref ls), &ExprKind::Match(ref re, ref ra, ref rs)) => {
126                 ls == rs && self.eq_expr(le, re) && over(la, ra, |l, r| {
127                     self.eq_expr(&l.body, &r.body) && both(&l.guard, &r.guard, |l, r| self.eq_guard(l, r))
128                         && over(&l.pats, &r.pats, |l, r| self.eq_pat(l, r))
129                 })
130             },
131             (&ExprKind::MethodCall(ref l_path, _, ref l_args), &ExprKind::MethodCall(ref r_path, _, ref r_args)) => {
132                 !self.ignore_fn && self.eq_path_segment(l_path, r_path) && self.eq_exprs(l_args, r_args)
133             },
134             (&ExprKind::Repeat(ref le, ref ll_id), &ExprKind::Repeat(ref re, ref rl_id)) => {
135                 let mut celcx = constant_context(self.cx, self.cx.tcx.body_tables(ll_id.body));
136                 let ll = celcx.expr(&self.cx.tcx.hir.body(ll_id.body).value);
137                 let mut celcx = constant_context(self.cx, self.cx.tcx.body_tables(rl_id.body));
138                 let rl = celcx.expr(&self.cx.tcx.hir.body(rl_id.body).value);
139
140                 self.eq_expr(le, re) && ll == rl
141             },
142             (&ExprKind::Ret(ref l), &ExprKind::Ret(ref r)) => both(l, r, |l, r| self.eq_expr(l, r)),
143             (&ExprKind::Path(ref l), &ExprKind::Path(ref r)) => self.eq_qpath(l, r),
144             (&ExprKind::Struct(ref l_path, ref lf, ref lo), &ExprKind::Struct(ref r_path, ref rf, ref ro)) => {
145                 self.eq_qpath(l_path, r_path) && both(lo, ro, |l, r| self.eq_expr(l, r))
146                     && over(lf, rf, |l, r| self.eq_field(l, r))
147             },
148             (&ExprKind::Tup(ref l_tup), &ExprKind::Tup(ref r_tup)) => self.eq_exprs(l_tup, r_tup),
149             (&ExprKind::Unary(l_op, ref le), &ExprKind::Unary(r_op, ref re)) => l_op == r_op && self.eq_expr(le, re),
150             (&ExprKind::Array(ref l), &ExprKind::Array(ref r)) => self.eq_exprs(l, r),
151             (&ExprKind::While(ref lc, ref lb, ref ll), &ExprKind::While(ref rc, ref rb, ref rl)) => {
152                 self.eq_expr(lc, rc) && self.eq_block(lb, rb) && both(ll, rl, |l, r| l.ident.as_str() == r.ident.as_str())
153             },
154             _ => false,
155         }
156     }
157
158     fn eq_exprs(&mut self, left: &P<[Expr]>, right: &P<[Expr]>) -> bool {
159         over(left, right, |l, r| self.eq_expr(l, r))
160     }
161
162     fn eq_field(&mut self, left: &Field, right: &Field) -> bool {
163         left.ident.name == right.ident.name && self.eq_expr(&left.expr, &right.expr)
164     }
165
166     fn eq_guard(&mut self, left: &Guard, right: &Guard) -> bool {
167         match (left, right) {
168             (Guard::If(l), Guard::If(r)) => self.eq_expr(l, r),
169         }
170     }
171
172     fn eq_generic_arg(&mut self, left: &GenericArg, right: &GenericArg) -> bool {
173         match (left, right) {
174             (GenericArg::Lifetime(l_lt), GenericArg::Lifetime(r_lt)) => self.eq_lifetime(l_lt, r_lt),
175             (GenericArg::Type(l_ty), GenericArg::Type(r_ty)) => self.eq_ty(l_ty, r_ty),
176             _ => false,
177         }
178     }
179
180     fn eq_lifetime(&mut self, left: &Lifetime, right: &Lifetime) -> bool {
181         left.name == right.name
182     }
183
184     /// Check whether two patterns are the same.
185     pub fn eq_pat(&mut self, left: &Pat, right: &Pat) -> bool {
186         match (&left.node, &right.node) {
187             (&PatKind::Box(ref l), &PatKind::Box(ref r)) => self.eq_pat(l, r),
188             (&PatKind::TupleStruct(ref lp, ref la, ls), &PatKind::TupleStruct(ref rp, ref ra, rs)) => {
189                 self.eq_qpath(lp, rp) && over(la, ra, |l, r| self.eq_pat(l, r)) && ls == rs
190             },
191             (&PatKind::Binding(ref lb, _, ref li, ref lp), &PatKind::Binding(ref rb, _, ref ri, ref rp)) => {
192                 lb == rb && li.name.as_str() == ri.name.as_str() && both(lp, rp, |l, r| self.eq_pat(l, r))
193             },
194             (&PatKind::Path(ref l), &PatKind::Path(ref r)) => self.eq_qpath(l, r),
195             (&PatKind::Lit(ref l), &PatKind::Lit(ref r)) => self.eq_expr(l, r),
196             (&PatKind::Tuple(ref l, ls), &PatKind::Tuple(ref r, rs)) => {
197                 ls == rs && over(l, r, |l, r| self.eq_pat(l, r))
198             },
199             (&PatKind::Range(ref ls, ref le, ref li), &PatKind::Range(ref rs, ref re, ref ri)) => {
200                 self.eq_expr(ls, rs) && self.eq_expr(le, re) && (*li == *ri)
201             },
202             (&PatKind::Ref(ref le, ref lm), &PatKind::Ref(ref re, ref rm)) => lm == rm && self.eq_pat(le, re),
203             (&PatKind::Slice(ref ls, ref li, ref le), &PatKind::Slice(ref rs, ref ri, ref re)) => {
204                 over(ls, rs, |l, r| self.eq_pat(l, r)) && over(le, re, |l, r| self.eq_pat(l, r))
205                     && both(li, ri, |l, r| self.eq_pat(l, r))
206             },
207             (&PatKind::Wild, &PatKind::Wild) => true,
208             _ => false,
209         }
210     }
211
212     #[allow(clippy::similar_names)]
213     fn eq_qpath(&mut self, left: &QPath, right: &QPath) -> bool {
214         match (left, right) {
215             (&QPath::Resolved(ref lty, ref lpath), &QPath::Resolved(ref rty, ref rpath)) => {
216                 both(lty, rty, |l, r| self.eq_ty(l, r)) && self.eq_path(lpath, rpath)
217             },
218             (&QPath::TypeRelative(ref lty, ref lseg), &QPath::TypeRelative(ref rty, ref rseg)) => {
219                 self.eq_ty(lty, rty) && self.eq_path_segment(lseg, rseg)
220             },
221             _ => false,
222         }
223     }
224
225     fn eq_path(&mut self, left: &Path, right: &Path) -> bool {
226         left.is_global() == right.is_global()
227             && over(&left.segments, &right.segments, |l, r| self.eq_path_segment(l, r))
228     }
229
230     fn eq_path_parameters(&mut self, left: &GenericArgs, right: &GenericArgs) -> bool {
231         if !(left.parenthesized || right.parenthesized) {
232             over(&left.args, &right.args, |l, r| self.eq_generic_arg(l, r)) // FIXME(flip1995): may not work
233                 && over(&left.bindings, &right.bindings, |l, r| self.eq_type_binding(l, r))
234         } else if left.parenthesized && right.parenthesized {
235             over(left.inputs(), right.inputs(), |l, r| self.eq_ty(l, r))
236                 && both(
237                     &Some(&left.bindings[0].ty),
238                     &Some(&right.bindings[0].ty),
239                     |l, r| self.eq_ty(l, r),
240                 )
241         } else {
242             false
243         }
244     }
245
246     pub fn eq_path_segments(&mut self, left: &[PathSegment], right: &[PathSegment]) -> bool {
247         left.len() == right.len() && left.iter().zip(right).all(|(l, r)| self.eq_path_segment(l, r))
248     }
249
250     pub fn eq_path_segment(&mut self, left: &PathSegment, right: &PathSegment) -> bool {
251         // The == of idents doesn't work with different contexts,
252         // we have to be explicit about hygiene
253         if left.ident.as_str() != right.ident.as_str() {
254             return false;
255         }
256         match (&left.args, &right.args) {
257             (&None, &None) => true,
258             (&Some(ref l), &Some(ref r)) => self.eq_path_parameters(l, r),
259             _ => false,
260         }
261     }
262
263     pub fn eq_ty(&mut self, left: &Ty, right: &Ty) -> bool {
264         self.eq_ty_kind(&left.node, &right.node)
265     }
266
267     #[allow(clippy::similar_names)]
268     pub fn eq_ty_kind(&mut self, left: &TyKind, right: &TyKind) -> bool {
269         match (left, right) {
270             (&TyKind::Slice(ref l_vec), &TyKind::Slice(ref r_vec)) => self.eq_ty(l_vec, r_vec),
271             (&TyKind::Array(ref lt, ref ll_id), &TyKind::Array(ref rt, ref rl_id)) => {
272                 let full_table = self.tables;
273
274                 let mut celcx = constant_context(self.cx, self.cx.tcx.body_tables(ll_id.body));
275                 self.tables = self.cx.tcx.body_tables(ll_id.body);
276                 let ll = celcx.expr(&self.cx.tcx.hir.body(ll_id.body).value);
277
278                 let mut celcx = constant_context(self.cx, self.cx.tcx.body_tables(rl_id.body));
279                 self.tables = self.cx.tcx.body_tables(rl_id.body);
280                 let rl = celcx.expr(&self.cx.tcx.hir.body(rl_id.body).value);
281
282                 let eq_ty = self.eq_ty(lt, rt);
283                 self.tables = full_table;
284                 eq_ty && ll == rl
285             },
286             (&TyKind::Ptr(ref l_mut), &TyKind::Ptr(ref r_mut)) => l_mut.mutbl == r_mut.mutbl && self.eq_ty(&*l_mut.ty, &*r_mut.ty),
287             (&TyKind::Rptr(_, ref l_rmut), &TyKind::Rptr(_, ref r_rmut)) => {
288                 l_rmut.mutbl == r_rmut.mutbl && self.eq_ty(&*l_rmut.ty, &*r_rmut.ty)
289             },
290             (&TyKind::Path(ref l), &TyKind::Path(ref r)) => self.eq_qpath(l, r),
291             (&TyKind::Tup(ref l), &TyKind::Tup(ref r)) => over(l, r, |l, r| self.eq_ty(l, r)),
292             (&TyKind::Infer, &TyKind::Infer) => true,
293             _ => false,
294         }
295     }
296
297     fn eq_type_binding(&mut self, left: &TypeBinding, right: &TypeBinding) -> bool {
298         left.ident.name == right.ident.name && self.eq_ty(&left.ty, &right.ty)
299     }
300 }
301
302 fn swap_binop<'a>(binop: BinOpKind, lhs: &'a Expr, rhs: &'a Expr) -> Option<(BinOpKind, &'a Expr, &'a Expr)> {
303     match binop {
304         BinOpKind::Add |
305         BinOpKind::Mul |
306         BinOpKind::Eq |
307         BinOpKind::Ne |
308         BinOpKind::BitAnd |
309         BinOpKind::BitXor |
310         BinOpKind::BitOr => Some((binop, rhs, lhs)),
311         BinOpKind::Lt => Some((BinOpKind::Gt, rhs, lhs)),
312         BinOpKind::Le => Some((BinOpKind::Ge, rhs, lhs)),
313         BinOpKind::Ge => Some((BinOpKind::Le, rhs, lhs)),
314         BinOpKind::Gt => Some((BinOpKind::Lt, rhs, lhs)),
315         BinOpKind::Shl |
316         BinOpKind::Shr |
317         BinOpKind::Rem |
318         BinOpKind::Sub |
319         BinOpKind::Div |
320         BinOpKind::And |
321         BinOpKind::Or => None,
322     }
323 }
324
325 /// Check if the two `Option`s are both `None` or some equal values as per
326 /// `eq_fn`.
327 fn both<X, F>(l: &Option<X>, r: &Option<X>, mut eq_fn: F) -> bool
328 where
329     F: FnMut(&X, &X) -> bool,
330 {
331     l.as_ref()
332         .map_or_else(|| r.is_none(), |x| r.as_ref().map_or(false, |y| eq_fn(x, y)))
333 }
334
335 /// Check if two slices are equal as per `eq_fn`.
336 fn over<X, F>(left: &[X], right: &[X], mut eq_fn: F) -> bool
337 where
338     F: FnMut(&X, &X) -> bool,
339 {
340     left.len() == right.len() && left.iter().zip(right).all(|(x, y)| eq_fn(x, y))
341 }
342
343
344 /// Type used to hash an ast element. This is different from the `Hash` trait
345 /// on ast types as this
346 /// trait would consider IDs and spans.
347 ///
348 /// All expressions kind are hashed, but some might have a weaker hash.
349 pub struct SpanlessHash<'a, 'tcx: 'a> {
350     /// Context used to evaluate constant expressions.
351     cx: &'a LateContext<'a, 'tcx>,
352     tables: &'a TypeckTables<'tcx>,
353     s: DefaultHasher,
354 }
355
356 impl<'a, 'tcx: 'a> SpanlessHash<'a, 'tcx> {
357     pub fn new(cx: &'a LateContext<'a, 'tcx>, tables: &'a TypeckTables<'tcx>) -> Self {
358         Self {
359             cx,
360             tables,
361             s: DefaultHasher::new(),
362         }
363     }
364
365     pub fn finish(&self) -> u64 {
366         self.s.finish()
367     }
368
369     pub fn hash_block(&mut self, b: &Block) {
370         for s in &b.stmts {
371             self.hash_stmt(s);
372         }
373
374         if let Some(ref e) = b.expr {
375             self.hash_expr(e);
376         }
377
378         match b.rules {
379             BlockCheckMode::DefaultBlock => 0,
380             BlockCheckMode::UnsafeBlock(_) => 1,
381             BlockCheckMode::PushUnsafeBlock(_) => 2,
382             BlockCheckMode::PopUnsafeBlock(_) => 3,
383         }.hash(&mut self.s);
384     }
385
386     #[allow(clippy::many_single_char_names)]
387     pub fn hash_expr(&mut self, e: &Expr) {
388         if let Some(e) = constant_simple(self.cx, self.tables, e) {
389             return e.hash(&mut self.s);
390         }
391
392         match e.node {
393             ExprKind::AddrOf(m, ref e) => {
394                 let c: fn(_, _) -> _ = ExprKind::AddrOf;
395                 c.hash(&mut self.s);
396                 m.hash(&mut self.s);
397                 self.hash_expr(e);
398             },
399             ExprKind::Continue(i) => {
400                 let c: fn(_) -> _ = ExprKind::Continue;
401                 c.hash(&mut self.s);
402                 if let Some(i) = i.label {
403                     self.hash_name(i.ident.name);
404                 }
405             },
406             ExprKind::Yield(ref e) => {
407                 let c: fn(_) -> _ = ExprKind::Yield;
408                 c.hash(&mut self.s);
409                 self.hash_expr(e);
410             },
411             ExprKind::Assign(ref l, ref r) => {
412                 let c: fn(_, _) -> _ = ExprKind::Assign;
413                 c.hash(&mut self.s);
414                 self.hash_expr(l);
415                 self.hash_expr(r);
416             },
417             ExprKind::AssignOp(ref o, ref l, ref r) => {
418                 let c: fn(_, _, _) -> _ = ExprKind::AssignOp;
419                 c.hash(&mut self.s);
420                 o.hash(&mut self.s);
421                 self.hash_expr(l);
422                 self.hash_expr(r);
423             },
424             ExprKind::Block(ref b, _) => {
425                 let c: fn(_, _) -> _ = ExprKind::Block;
426                 c.hash(&mut self.s);
427                 self.hash_block(b);
428             },
429             ExprKind::Binary(op, ref l, ref r) => {
430                 let c: fn(_, _, _) -> _ = ExprKind::Binary;
431                 c.hash(&mut self.s);
432                 op.node.hash(&mut self.s);
433                 self.hash_expr(l);
434                 self.hash_expr(r);
435             },
436             ExprKind::Break(i, ref j) => {
437                 let c: fn(_, _) -> _ = ExprKind::Break;
438                 c.hash(&mut self.s);
439                 if let Some(i) = i.label {
440                     self.hash_name(i.ident.name);
441                 }
442                 if let Some(ref j) = *j {
443                     self.hash_expr(&*j);
444                 }
445             },
446             ExprKind::Box(ref e) => {
447                 let c: fn(_) -> _ = ExprKind::Box;
448                 c.hash(&mut self.s);
449                 self.hash_expr(e);
450             },
451             ExprKind::Call(ref fun, ref args) => {
452                 let c: fn(_, _) -> _ = ExprKind::Call;
453                 c.hash(&mut self.s);
454                 self.hash_expr(fun);
455                 self.hash_exprs(args);
456             },
457             ExprKind::Cast(ref e, ref _ty) => {
458                 let c: fn(_, _) -> _ = ExprKind::Cast;
459                 c.hash(&mut self.s);
460                 self.hash_expr(e);
461                 // TODO: _ty
462             },
463             ExprKind::Closure(cap, _, eid, _, _) => {
464                 let c: fn(_, _, _, _, _) -> _ = ExprKind::Closure;
465                 c.hash(&mut self.s);
466                 match cap {
467                     CaptureClause::CaptureByValue => 0,
468                     CaptureClause::CaptureByRef => 1,
469                 }.hash(&mut self.s);
470                 self.hash_expr(&self.cx.tcx.hir.body(eid).value);
471             },
472             ExprKind::Field(ref e, ref f) => {
473                 let c: fn(_, _) -> _ = ExprKind::Field;
474                 c.hash(&mut self.s);
475                 self.hash_expr(e);
476                 self.hash_name(f.name);
477             },
478             ExprKind::Index(ref a, ref i) => {
479                 let c: fn(_, _) -> _ = ExprKind::Index;
480                 c.hash(&mut self.s);
481                 self.hash_expr(a);
482                 self.hash_expr(i);
483             },
484             ExprKind::InlineAsm(..) => {
485                 let c: fn(_, _, _) -> _ = ExprKind::InlineAsm;
486                 c.hash(&mut self.s);
487             },
488             ExprKind::If(ref cond, ref t, ref e) => {
489                 let c: fn(_, _, _) -> _ = ExprKind::If;
490                 c.hash(&mut self.s);
491                 self.hash_expr(cond);
492                 self.hash_expr(&**t);
493                 if let Some(ref e) = *e {
494                     self.hash_expr(e);
495                 }
496             },
497             ExprKind::Lit(ref l) => {
498                 let c: fn(_) -> _ = ExprKind::Lit;
499                 c.hash(&mut self.s);
500                 l.hash(&mut self.s);
501             },
502             ExprKind::Loop(ref b, ref i, _) => {
503                 let c: fn(_, _, _) -> _ = ExprKind::Loop;
504                 c.hash(&mut self.s);
505                 self.hash_block(b);
506                 if let Some(i) = *i {
507                     self.hash_name(i.ident.name);
508                 }
509             },
510             ExprKind::Match(ref e, ref arms, ref s) => {
511                 let c: fn(_, _, _) -> _ = ExprKind::Match;
512                 c.hash(&mut self.s);
513                 self.hash_expr(e);
514
515                 for arm in arms {
516                     // TODO: arm.pat?
517                     if let Some(ref e) = arm.guard {
518                         self.hash_guard(e);
519                     }
520                     self.hash_expr(&arm.body);
521                 }
522
523                 s.hash(&mut self.s);
524             },
525             ExprKind::MethodCall(ref path, ref _tys, ref args) => {
526                 let c: fn(_, _, _) -> _ = ExprKind::MethodCall;
527                 c.hash(&mut self.s);
528                 self.hash_name(path.ident.name);
529                 self.hash_exprs(args);
530             },
531             ExprKind::Repeat(ref e, ref l_id) => {
532                 let c: fn(_, _) -> _ = ExprKind::Repeat;
533                 c.hash(&mut self.s);
534                 self.hash_expr(e);
535                 let full_table = self.tables;
536                 self.tables = self.cx.tcx.body_tables(l_id.body);
537                 self.hash_expr(&self.cx.tcx.hir.body(l_id.body).value);
538                 self.tables = full_table;
539             },
540             ExprKind::Ret(ref e) => {
541                 let c: fn(_) -> _ = ExprKind::Ret;
542                 c.hash(&mut self.s);
543                 if let Some(ref e) = *e {
544                     self.hash_expr(e);
545                 }
546             },
547             ExprKind::Path(ref qpath) => {
548                 let c: fn(_) -> _ = ExprKind::Path;
549                 c.hash(&mut self.s);
550                 self.hash_qpath(qpath);
551             },
552             ExprKind::Struct(ref path, ref fields, ref expr) => {
553                 let c: fn(_, _, _) -> _ = ExprKind::Struct;
554                 c.hash(&mut self.s);
555
556                 self.hash_qpath(path);
557
558                 for f in fields {
559                     self.hash_name(f.ident.name);
560                     self.hash_expr(&f.expr);
561                 }
562
563                 if let Some(ref e) = *expr {
564                     self.hash_expr(e);
565                 }
566             },
567             ExprKind::Tup(ref tup) => {
568                 let c: fn(_) -> _ = ExprKind::Tup;
569                 c.hash(&mut self.s);
570                 self.hash_exprs(tup);
571             },
572             ExprKind::Type(ref e, ref _ty) => {
573                 let c: fn(_, _) -> _ = ExprKind::Type;
574                 c.hash(&mut self.s);
575                 self.hash_expr(e);
576                 // TODO: _ty
577             },
578             ExprKind::Unary(lop, ref le) => {
579                 let c: fn(_, _) -> _ = ExprKind::Unary;
580                 c.hash(&mut self.s);
581
582                 lop.hash(&mut self.s);
583                 self.hash_expr(le);
584             },
585             ExprKind::Array(ref v) => {
586                 let c: fn(_) -> _ = ExprKind::Array;
587                 c.hash(&mut self.s);
588
589                 self.hash_exprs(v);
590             },
591             ExprKind::While(ref cond, ref b, l) => {
592                 let c: fn(_, _, _) -> _ = ExprKind::While;
593                 c.hash(&mut self.s);
594
595                 self.hash_expr(cond);
596                 self.hash_block(b);
597                 if let Some(l) = l {
598                     self.hash_name(l.ident.name);
599                 }
600             },
601         }
602     }
603
604     pub fn hash_exprs(&mut self, e: &P<[Expr]>) {
605         for e in e {
606             self.hash_expr(e);
607         }
608     }
609
610     pub fn hash_name(&mut self, n: Name) {
611         n.as_str().hash(&mut self.s);
612     }
613
614     pub fn hash_qpath(&mut self, p: &QPath) {
615         match *p {
616             QPath::Resolved(_, ref path) => {
617                 self.hash_path(path);
618             },
619             QPath::TypeRelative(_, ref path) => {
620                 self.hash_name(path.ident.name);
621             },
622         }
623         // self.cx.tables.qpath_def(p, id).hash(&mut self.s);
624     }
625
626     pub fn hash_path(&mut self, p: &Path) {
627         p.is_global().hash(&mut self.s);
628         for p in &p.segments {
629             self.hash_name(p.ident.name);
630         }
631     }
632
633     pub fn hash_stmt(&mut self, b: &Stmt) {
634         match b.node {
635             StmtKind::Decl(ref decl, _) => {
636                 let c: fn(_, _) -> _ = StmtKind::Decl;
637                 c.hash(&mut self.s);
638
639                 if let DeclKind::Local(ref local) = decl.node {
640                     if let Some(ref init) = local.init {
641                         self.hash_expr(init);
642                     }
643                 }
644             },
645             StmtKind::Expr(ref expr, _) => {
646                 let c: fn(_, _) -> _ = StmtKind::Expr;
647                 c.hash(&mut self.s);
648                 self.hash_expr(expr);
649             },
650             StmtKind::Semi(ref expr, _) => {
651                 let c: fn(_, _) -> _ = StmtKind::Semi;
652                 c.hash(&mut self.s);
653                 self.hash_expr(expr);
654             },
655         }
656     }
657
658     pub fn hash_guard(&mut self, g: &Guard) {
659         match g {
660             Guard::If(ref expr) => {
661                 let c: fn(_) -> _ = Guard::If;
662                 c.hash(&mut self.s);
663                 self.hash_expr(expr);
664             }
665         }
666     }
667 }