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