]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_utils/src/ast_utils.rs
Delete spaces
[rust.git] / src / tools / clippy / clippy_utils / src / ast_utils.rs
1 //! Utilities for manipulating and extracting information from `rustc_ast::ast`.
2 //!
3 //! - The `eq_foobar` functions test for semantic equality but ignores `NodeId`s and `Span`s.
4
5 #![allow(clippy::similar_names, clippy::wildcard_imports, clippy::enum_glob_use)]
6
7 use crate::{both, over};
8 use if_chain::if_chain;
9 use rustc_ast::ptr::P;
10 use rustc_ast::{self as ast, *};
11 use rustc_span::symbol::Ident;
12 use std::mem;
13
14 pub mod ident_iter;
15 pub use ident_iter::IdentIter;
16
17 pub fn is_useless_with_eq_exprs(kind: BinOpKind) -> bool {
18     use BinOpKind::*;
19     matches!(
20         kind,
21         Sub | Div | Eq | Lt | Le | Gt | Ge | Ne | And | Or | BitXor | BitAnd | BitOr
22     )
23 }
24
25 /// Checks if each element in the first slice is contained within the latter as per `eq_fn`.
26 pub fn unordered_over<X>(left: &[X], right: &[X], mut eq_fn: impl FnMut(&X, &X) -> bool) -> bool {
27     left.len() == right.len() && left.iter().all(|l| right.iter().any(|r| eq_fn(l, r)))
28 }
29
30 pub fn eq_id(l: Ident, r: Ident) -> bool {
31     l.name == r.name
32 }
33
34 pub fn eq_pat(l: &Pat, r: &Pat) -> bool {
35     use PatKind::*;
36     match (&l.kind, &r.kind) {
37         (Paren(l), _) => eq_pat(l, r),
38         (_, Paren(r)) => eq_pat(l, r),
39         (Wild, Wild) | (Rest, Rest) => true,
40         (Lit(l), Lit(r)) => eq_expr(l, r),
41         (Ident(b1, i1, s1), Ident(b2, i2, s2)) => b1 == b2 && eq_id(*i1, *i2) && both(s1, s2, |l, r| eq_pat(l, r)),
42         (Range(lf, lt, le), Range(rf, rt, re)) => {
43             eq_expr_opt(lf, rf) && eq_expr_opt(lt, rt) && eq_range_end(&le.node, &re.node)
44         },
45         (Box(l), Box(r))
46         | (Ref(l, Mutability::Not), Ref(r, Mutability::Not))
47         | (Ref(l, Mutability::Mut), Ref(r, Mutability::Mut)) => eq_pat(l, r),
48         (Tuple(l), Tuple(r)) | (Slice(l), Slice(r)) => over(l, r, |l, r| eq_pat(l, r)),
49         (Path(lq, lp), Path(rq, rp)) => both(lq, rq, |l, r| eq_qself(l, r)) && eq_path(lp, rp),
50         (TupleStruct(lqself, lp, lfs), TupleStruct(rqself, rp, rfs)) => eq_maybe_qself(lqself, rqself) && eq_path(lp, rp) && over(lfs, rfs, |l, r| eq_pat(l, r)),
51         (Struct(lqself, lp, lfs, lr), Struct(rqself, rp, rfs, rr)) => {
52             lr == rr && eq_maybe_qself(lqself, rqself) &&eq_path(lp, rp) && unordered_over(lfs, rfs, |lf, rf| eq_field_pat(lf, rf))
53         },
54         (Or(ls), Or(rs)) => unordered_over(ls, rs, |l, r| eq_pat(l, r)),
55         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
56         _ => false,
57     }
58 }
59
60 pub fn eq_range_end(l: &RangeEnd, r: &RangeEnd) -> bool {
61     match (l, r) {
62         (RangeEnd::Excluded, RangeEnd::Excluded) => true,
63         (RangeEnd::Included(l), RangeEnd::Included(r)) => {
64             matches!(l, RangeSyntax::DotDotEq) == matches!(r, RangeSyntax::DotDotEq)
65         },
66         _ => false,
67     }
68 }
69
70 pub fn eq_field_pat(l: &PatField, r: &PatField) -> bool {
71     l.is_placeholder == r.is_placeholder
72         && eq_id(l.ident, r.ident)
73         && eq_pat(&l.pat, &r.pat)
74         && over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))
75 }
76
77 pub fn eq_qself(l: &QSelf, r: &QSelf) -> bool {
78     l.position == r.position && eq_ty(&l.ty, &r.ty)
79 }
80
81 pub fn eq_maybe_qself(l: &Option<QSelf>, r: &Option<QSelf>) -> bool {
82     match (l, r) {
83         (Some(l), Some(r)) => eq_qself(l, r),
84         (None, None) => true,
85         _ => false
86     }
87 }
88
89 pub fn eq_path(l: &Path, r: &Path) -> bool {
90     over(&l.segments, &r.segments, |l, r| eq_path_seg(l, r))
91 }
92
93 pub fn eq_path_seg(l: &PathSegment, r: &PathSegment) -> bool {
94     eq_id(l.ident, r.ident) && both(&l.args, &r.args, |l, r| eq_generic_args(l, r))
95 }
96
97 pub fn eq_generic_args(l: &GenericArgs, r: &GenericArgs) -> bool {
98     match (l, r) {
99         (GenericArgs::AngleBracketed(l), GenericArgs::AngleBracketed(r)) => {
100             over(&l.args, &r.args, |l, r| eq_angle_arg(l, r))
101         },
102         (GenericArgs::Parenthesized(l), GenericArgs::Parenthesized(r)) => {
103             over(&l.inputs, &r.inputs, |l, r| eq_ty(l, r)) && eq_fn_ret_ty(&l.output, &r.output)
104         },
105         _ => false,
106     }
107 }
108
109 pub fn eq_angle_arg(l: &AngleBracketedArg, r: &AngleBracketedArg) -> bool {
110     match (l, r) {
111         (AngleBracketedArg::Arg(l), AngleBracketedArg::Arg(r)) => eq_generic_arg(l, r),
112         (AngleBracketedArg::Constraint(l), AngleBracketedArg::Constraint(r)) => eq_assoc_constraint(l, r),
113         _ => false,
114     }
115 }
116
117 pub fn eq_generic_arg(l: &GenericArg, r: &GenericArg) -> bool {
118     match (l, r) {
119         (GenericArg::Lifetime(l), GenericArg::Lifetime(r)) => eq_id(l.ident, r.ident),
120         (GenericArg::Type(l), GenericArg::Type(r)) => eq_ty(l, r),
121         (GenericArg::Const(l), GenericArg::Const(r)) => eq_expr(&l.value, &r.value),
122         _ => false,
123     }
124 }
125
126 pub fn eq_expr_opt(l: &Option<P<Expr>>, r: &Option<P<Expr>>) -> bool {
127     both(l, r, |l, r| eq_expr(l, r))
128 }
129
130 pub fn eq_struct_rest(l: &StructRest, r: &StructRest) -> bool {
131     match (l, r) {
132         (StructRest::Base(lb), StructRest::Base(rb)) => eq_expr(lb, rb),
133         (StructRest::Rest(_), StructRest::Rest(_)) | (StructRest::None, StructRest::None) => true,
134         _ => false,
135     }
136 }
137
138 pub fn eq_expr(l: &Expr, r: &Expr) -> bool {
139     use ExprKind::*;
140     if !over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r)) {
141         return false;
142     }
143     match (&l.kind, &r.kind) {
144         (Paren(l), _) => eq_expr(l, r),
145         (_, Paren(r)) => eq_expr(l, r),
146         (Err, Err) => true,
147         (Box(l), Box(r)) | (Try(l), Try(r)) | (Await(l), Await(r)) => eq_expr(l, r),
148         (Array(l), Array(r)) | (Tup(l), Tup(r)) => over(l, r, |l, r| eq_expr(l, r)),
149         (Repeat(le, ls), Repeat(re, rs)) => eq_expr(le, re) && eq_expr(&ls.value, &rs.value),
150         (Call(lc, la), Call(rc, ra)) => eq_expr(lc, rc) && over(la, ra, |l, r| eq_expr(l, r)),
151         (MethodCall(lc, la, _), MethodCall(rc, ra, _)) => eq_path_seg(lc, rc) && over(la, ra, |l, r| eq_expr(l, r)),
152         (Binary(lo, ll, lr), Binary(ro, rl, rr)) => lo.node == ro.node && eq_expr(ll, rl) && eq_expr(lr, rr),
153         (Unary(lo, l), Unary(ro, r)) => mem::discriminant(lo) == mem::discriminant(ro) && eq_expr(l, r),
154         (Lit(l), Lit(r)) => l.kind == r.kind,
155         (Cast(l, lt), Cast(r, rt)) | (Type(l, lt), Type(r, rt)) => eq_expr(l, r) && eq_ty(lt, rt),
156         (Let(lp, le), Let(rp, re)) => eq_pat(lp, rp) && eq_expr(le, re),
157         (If(lc, lt, le), If(rc, rt, re)) => eq_expr(lc, rc) && eq_block(lt, rt) && eq_expr_opt(le, re),
158         (While(lc, lt, ll), While(rc, rt, rl)) => eq_label(ll, rl) && eq_expr(lc, rc) && eq_block(lt, rt),
159         (ForLoop(lp, li, lt, ll), ForLoop(rp, ri, rt, rl)) => {
160             eq_label(ll, rl) && eq_pat(lp, rp) && eq_expr(li, ri) && eq_block(lt, rt)
161         },
162         (Loop(lt, ll), Loop(rt, rl)) => eq_label(ll, rl) && eq_block(lt, rt),
163         (Block(lb, ll), Block(rb, rl)) => eq_label(ll, rl) && eq_block(lb, rb),
164         (TryBlock(l), TryBlock(r)) => eq_block(l, r),
165         (Yield(l), Yield(r)) | (Ret(l), Ret(r)) => eq_expr_opt(l, r),
166         (Break(ll, le), Break(rl, re)) => eq_label(ll, rl) && eq_expr_opt(le, re),
167         (Continue(ll), Continue(rl)) => eq_label(ll, rl),
168         (Assign(l1, l2, _), Assign(r1, r2, _)) | (Index(l1, l2), Index(r1, r2)) => eq_expr(l1, r1) && eq_expr(l2, r2),
169         (AssignOp(lo, lp, lv), AssignOp(ro, rp, rv)) => lo.node == ro.node && eq_expr(lp, rp) && eq_expr(lv, rv),
170         (Field(lp, lf), Field(rp, rf)) => eq_id(*lf, *rf) && eq_expr(lp, rp),
171         (Match(ls, la), Match(rs, ra)) => eq_expr(ls, rs) && over(la, ra, |l, r| eq_arm(l, r)),
172         (Closure(lc, la, lm, lf, lb, _), Closure(rc, ra, rm, rf, rb, _)) => {
173             lc == rc && la.is_async() == ra.is_async() && lm == rm && eq_fn_decl(lf, rf) && eq_expr(lb, rb)
174         },
175         (Async(lc, _, lb), Async(rc, _, rb)) => lc == rc && eq_block(lb, rb),
176         (Range(lf, lt, ll), Range(rf, rt, rl)) => ll == rl && eq_expr_opt(lf, rf) && eq_expr_opt(lt, rt),
177         (AddrOf(lbk, lm, le), AddrOf(rbk, rm, re)) => lbk == rbk && lm == rm && eq_expr(le, re),
178         (Path(lq, lp), Path(rq, rp)) => both(lq, rq, |l, r| eq_qself(l, r)) && eq_path(lp, rp),
179         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
180         (Struct(lse), Struct(rse)) => {
181             eq_maybe_qself(&lse.qself, &rse.qself)
182                 && eq_path(&lse.path, &rse.path)
183                 && eq_struct_rest(&lse.rest, &rse.rest)
184                 && unordered_over(&lse.fields, &rse.fields, |l, r| eq_field(l, r))
185         },
186         _ => false,
187     }
188 }
189
190 pub fn eq_field(l: &ExprField, r: &ExprField) -> bool {
191     l.is_placeholder == r.is_placeholder
192         && eq_id(l.ident, r.ident)
193         && eq_expr(&l.expr, &r.expr)
194         && over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))
195 }
196
197 pub fn eq_arm(l: &Arm, r: &Arm) -> bool {
198     l.is_placeholder == r.is_placeholder
199         && eq_pat(&l.pat, &r.pat)
200         && eq_expr(&l.body, &r.body)
201         && eq_expr_opt(&l.guard, &r.guard)
202         && over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))
203 }
204
205 pub fn eq_label(l: &Option<Label>, r: &Option<Label>) -> bool {
206     both(l, r, |l, r| eq_id(l.ident, r.ident))
207 }
208
209 pub fn eq_block(l: &Block, r: &Block) -> bool {
210     l.rules == r.rules && over(&l.stmts, &r.stmts, |l, r| eq_stmt(l, r))
211 }
212
213 pub fn eq_stmt(l: &Stmt, r: &Stmt) -> bool {
214     use StmtKind::*;
215     match (&l.kind, &r.kind) {
216         (Local(l), Local(r)) => {
217             eq_pat(&l.pat, &r.pat)
218                 && both(&l.ty, &r.ty, |l, r| eq_ty(l, r))
219                 && eq_expr_opt(&l.init, &r.init)
220                 && over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))
221         },
222         (Item(l), Item(r)) => eq_item(l, r, eq_item_kind),
223         (Expr(l), Expr(r)) | (Semi(l), Semi(r)) => eq_expr(l, r),
224         (Empty, Empty) => true,
225         (MacCall(l), MacCall(r)) => {
226             l.style == r.style && eq_mac_call(&l.mac, &r.mac) && over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))
227         },
228         _ => false,
229     }
230 }
231
232 pub fn eq_item<K>(l: &Item<K>, r: &Item<K>, mut eq_kind: impl FnMut(&K, &K) -> bool) -> bool {
233     eq_id(l.ident, r.ident)
234         && over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))
235         && eq_vis(&l.vis, &r.vis)
236         && eq_kind(&l.kind, &r.kind)
237 }
238
239 pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
240     use ItemKind::*;
241     match (l, r) {
242         (ExternCrate(l), ExternCrate(r)) => l == r,
243         (Use(l), Use(r)) => eq_use_tree(l, r),
244         (Static(lt, lm, le), Static(rt, rm, re)) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re),
245         (Const(ld, lt, le), Const(rd, rt, re)) => eq_defaultness(*ld, *rd) && eq_ty(lt, rt) && eq_expr_opt(le, re),
246         (Fn(box FnKind(ld, lf, lg, lb)), Fn(box FnKind(rd, rf, rg, rb))) => {
247             eq_defaultness(*ld, *rd) && eq_fn_sig(lf, rf) && eq_generics(lg, rg) && both(lb, rb, |l, r| eq_block(l, r))
248         },
249         (Mod(lu, lmk), Mod(ru, rmk)) => {
250             lu == ru
251                 && match (lmk, rmk) {
252                     (ModKind::Loaded(litems, linline, _), ModKind::Loaded(ritems, rinline, _)) => {
253                         linline == rinline && over(litems, ritems, |l, r| eq_item(l, r, eq_item_kind))
254                     },
255                     (ModKind::Unloaded, ModKind::Unloaded) => true,
256                     _ => false,
257                 }
258         },
259         (ForeignMod(l), ForeignMod(r)) => {
260             both(&l.abi, &r.abi, |l, r| eq_str_lit(l, r))
261                 && over(&l.items, &r.items, |l, r| eq_item(l, r, eq_foreign_item_kind))
262         },
263         (TyAlias(box TyAliasKind(ld, lg, lb, lt)), TyAlias(box TyAliasKind(rd, rg, rb, rt))) => {
264             eq_defaultness(*ld, *rd)
265                 && eq_generics(lg, rg)
266                 && over(lb, rb, |l, r| eq_generic_bound(l, r))
267                 && both(lt, rt, |l, r| eq_ty(l, r))
268         },
269         (Enum(le, lg), Enum(re, rg)) => {
270             over(&le.variants, &re.variants, |l, r| eq_variant(l, r)) && eq_generics(lg, rg)
271         },
272         (Struct(lv, lg), Struct(rv, rg)) | (Union(lv, lg), Union(rv, rg)) => {
273             eq_variant_data(lv, rv) && eq_generics(lg, rg)
274         },
275         (Trait(box TraitKind(la, lu, lg, lb, li)), Trait(box TraitKind(ra, ru, rg, rb, ri))) => {
276             la == ra
277                 && matches!(lu, Unsafe::No) == matches!(ru, Unsafe::No)
278                 && eq_generics(lg, rg)
279                 && over(lb, rb, |l, r| eq_generic_bound(l, r))
280                 && over(li, ri, |l, r| eq_item(l, r, eq_assoc_item_kind))
281         },
282         (TraitAlias(lg, lb), TraitAlias(rg, rb)) => eq_generics(lg, rg) && over(lb, rb, |l, r| eq_generic_bound(l, r)),
283         (
284             Impl(box ImplKind {
285                 unsafety: lu,
286                 polarity: lp,
287                 defaultness: ld,
288                 constness: lc,
289                 generics: lg,
290                 of_trait: lot,
291                 self_ty: lst,
292                 items: li,
293             }),
294             Impl(box ImplKind {
295                 unsafety: ru,
296                 polarity: rp,
297                 defaultness: rd,
298                 constness: rc,
299                 generics: rg,
300                 of_trait: rot,
301                 self_ty: rst,
302                 items: ri,
303             }),
304         ) => {
305             matches!(lu, Unsafe::No) == matches!(ru, Unsafe::No)
306                 && matches!(lp, ImplPolarity::Positive) == matches!(rp, ImplPolarity::Positive)
307                 && eq_defaultness(*ld, *rd)
308                 && matches!(lc, ast::Const::No) == matches!(rc, ast::Const::No)
309                 && eq_generics(lg, rg)
310                 && both(lot, rot, |l, r| eq_path(&l.path, &r.path))
311                 && eq_ty(lst, rst)
312                 && over(li, ri, |l, r| eq_item(l, r, eq_assoc_item_kind))
313         },
314         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
315         (MacroDef(l), MacroDef(r)) => l.macro_rules == r.macro_rules && eq_mac_args(&l.body, &r.body),
316         _ => false,
317     }
318 }
319
320 pub fn eq_foreign_item_kind(l: &ForeignItemKind, r: &ForeignItemKind) -> bool {
321     use ForeignItemKind::*;
322     match (l, r) {
323         (Static(lt, lm, le), Static(rt, rm, re)) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re),
324         (Fn(box FnKind(ld, lf, lg, lb)), Fn(box FnKind(rd, rf, rg, rb))) => {
325             eq_defaultness(*ld, *rd) && eq_fn_sig(lf, rf) && eq_generics(lg, rg) && both(lb, rb, |l, r| eq_block(l, r))
326         },
327         (TyAlias(box TyAliasKind(ld, lg, lb, lt)), TyAlias(box TyAliasKind(rd, rg, rb, rt))) => {
328             eq_defaultness(*ld, *rd)
329                 && eq_generics(lg, rg)
330                 && over(lb, rb, |l, r| eq_generic_bound(l, r))
331                 && both(lt, rt, |l, r| eq_ty(l, r))
332         },
333         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
334         _ => false,
335     }
336 }
337
338 pub fn eq_assoc_item_kind(l: &AssocItemKind, r: &AssocItemKind) -> bool {
339     use AssocItemKind::*;
340     match (l, r) {
341         (Const(ld, lt, le), Const(rd, rt, re)) => eq_defaultness(*ld, *rd) && eq_ty(lt, rt) && eq_expr_opt(le, re),
342         (Fn(box FnKind(ld, lf, lg, lb)), Fn(box FnKind(rd, rf, rg, rb))) => {
343             eq_defaultness(*ld, *rd) && eq_fn_sig(lf, rf) && eq_generics(lg, rg) && both(lb, rb, |l, r| eq_block(l, r))
344         },
345         (TyAlias(box TyAliasKind(ld, lg, lb, lt)), TyAlias(box TyAliasKind(rd, rg, rb, rt))) => {
346             eq_defaultness(*ld, *rd)
347                 && eq_generics(lg, rg)
348                 && over(lb, rb, |l, r| eq_generic_bound(l, r))
349                 && both(lt, rt, |l, r| eq_ty(l, r))
350         },
351         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
352         _ => false,
353     }
354 }
355
356 pub fn eq_variant(l: &Variant, r: &Variant) -> bool {
357     l.is_placeholder == r.is_placeholder
358         && over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))
359         && eq_vis(&l.vis, &r.vis)
360         && eq_id(l.ident, r.ident)
361         && eq_variant_data(&l.data, &r.data)
362         && both(&l.disr_expr, &r.disr_expr, |l, r| eq_expr(&l.value, &r.value))
363 }
364
365 pub fn eq_variant_data(l: &VariantData, r: &VariantData) -> bool {
366     use VariantData::*;
367     match (l, r) {
368         (Unit(_), Unit(_)) => true,
369         (Struct(l, _), Struct(r, _)) | (Tuple(l, _), Tuple(r, _)) => over(l, r, |l, r| eq_struct_field(l, r)),
370         _ => false,
371     }
372 }
373
374 pub fn eq_struct_field(l: &FieldDef, r: &FieldDef) -> bool {
375     l.is_placeholder == r.is_placeholder
376         && over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))
377         && eq_vis(&l.vis, &r.vis)
378         && both(&l.ident, &r.ident, |l, r| eq_id(*l, *r))
379         && eq_ty(&l.ty, &r.ty)
380 }
381
382 pub fn eq_fn_sig(l: &FnSig, r: &FnSig) -> bool {
383     eq_fn_decl(&l.decl, &r.decl) && eq_fn_header(&l.header, &r.header)
384 }
385
386 pub fn eq_fn_header(l: &FnHeader, r: &FnHeader) -> bool {
387     matches!(l.unsafety, Unsafe::No) == matches!(r.unsafety, Unsafe::No)
388         && l.asyncness.is_async() == r.asyncness.is_async()
389         && matches!(l.constness, Const::No) == matches!(r.constness, Const::No)
390         && eq_ext(&l.ext, &r.ext)
391 }
392
393 pub fn eq_generics(l: &Generics, r: &Generics) -> bool {
394     over(&l.params, &r.params, |l, r| eq_generic_param(l, r))
395         && over(&l.where_clause.predicates, &r.where_clause.predicates, |l, r| {
396             eq_where_predicate(l, r)
397         })
398 }
399
400 pub fn eq_where_predicate(l: &WherePredicate, r: &WherePredicate) -> bool {
401     use WherePredicate::*;
402     match (l, r) {
403         (BoundPredicate(l), BoundPredicate(r)) => {
404             over(&l.bound_generic_params, &r.bound_generic_params, |l, r| {
405                 eq_generic_param(l, r)
406             }) && eq_ty(&l.bounded_ty, &r.bounded_ty)
407                 && over(&l.bounds, &r.bounds, |l, r| eq_generic_bound(l, r))
408         },
409         (RegionPredicate(l), RegionPredicate(r)) => {
410             eq_id(l.lifetime.ident, r.lifetime.ident) && over(&l.bounds, &r.bounds, |l, r| eq_generic_bound(l, r))
411         },
412         (EqPredicate(l), EqPredicate(r)) => eq_ty(&l.lhs_ty, &r.lhs_ty) && eq_ty(&l.rhs_ty, &r.rhs_ty),
413         _ => false,
414     }
415 }
416
417 pub fn eq_use_tree(l: &UseTree, r: &UseTree) -> bool {
418     eq_path(&l.prefix, &r.prefix) && eq_use_tree_kind(&l.kind, &r.kind)
419 }
420
421 pub fn eq_anon_const(l: &AnonConst, r: &AnonConst) -> bool {
422     eq_expr(&l.value, &r.value)
423 }
424
425 pub fn eq_use_tree_kind(l: &UseTreeKind, r: &UseTreeKind) -> bool {
426     use UseTreeKind::*;
427     match (l, r) {
428         (Glob, Glob) => true,
429         (Simple(l, _, _), Simple(r, _, _)) => both(l, r, |l, r| eq_id(*l, *r)),
430         (Nested(l), Nested(r)) => over(l, r, |(l, _), (r, _)| eq_use_tree(l, r)),
431         _ => false,
432     }
433 }
434
435 pub fn eq_defaultness(l: Defaultness, r: Defaultness) -> bool {
436     matches!(
437         (l, r),
438         (Defaultness::Final, Defaultness::Final) | (Defaultness::Default(_), Defaultness::Default(_))
439     )
440 }
441
442 pub fn eq_vis(l: &Visibility, r: &Visibility) -> bool {
443     use VisibilityKind::*;
444     match (&l.kind, &r.kind) {
445         (Public, Public) | (Inherited, Inherited) | (Crate(_), Crate(_)) => true,
446         (Restricted { path: l, .. }, Restricted { path: r, .. }) => eq_path(l, r),
447         _ => false,
448     }
449 }
450
451 pub fn eq_fn_decl(l: &FnDecl, r: &FnDecl) -> bool {
452     eq_fn_ret_ty(&l.output, &r.output)
453         && over(&l.inputs, &r.inputs, |l, r| {
454             l.is_placeholder == r.is_placeholder
455                 && eq_pat(&l.pat, &r.pat)
456                 && eq_ty(&l.ty, &r.ty)
457                 && over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))
458         })
459 }
460
461 pub fn eq_fn_ret_ty(l: &FnRetTy, r: &FnRetTy) -> bool {
462     match (l, r) {
463         (FnRetTy::Default(_), FnRetTy::Default(_)) => true,
464         (FnRetTy::Ty(l), FnRetTy::Ty(r)) => eq_ty(l, r),
465         _ => false,
466     }
467 }
468
469 pub fn eq_ty(l: &Ty, r: &Ty) -> bool {
470     use TyKind::*;
471     match (&l.kind, &r.kind) {
472         (Paren(l), _) => eq_ty(l, r),
473         (_, Paren(r)) => eq_ty(l, r),
474         (Never, Never) | (Infer, Infer) | (ImplicitSelf, ImplicitSelf) | (Err, Err) | (CVarArgs, CVarArgs) => true,
475         (Slice(l), Slice(r)) => eq_ty(l, r),
476         (Array(le, ls), Array(re, rs)) => eq_ty(le, re) && eq_expr(&ls.value, &rs.value),
477         (Ptr(l), Ptr(r)) => l.mutbl == r.mutbl && eq_ty(&l.ty, &r.ty),
478         (Rptr(ll, l), Rptr(rl, r)) => {
479             both(ll, rl, |l, r| eq_id(l.ident, r.ident)) && l.mutbl == r.mutbl && eq_ty(&l.ty, &r.ty)
480         },
481         (BareFn(l), BareFn(r)) => {
482             l.unsafety == r.unsafety
483                 && eq_ext(&l.ext, &r.ext)
484                 && over(&l.generic_params, &r.generic_params, |l, r| eq_generic_param(l, r))
485                 && eq_fn_decl(&l.decl, &r.decl)
486         },
487         (Tup(l), Tup(r)) => over(l, r, |l, r| eq_ty(l, r)),
488         (Path(lq, lp), Path(rq, rp)) => both(lq, rq, |l, r| eq_qself(l, r)) && eq_path(lp, rp),
489         (TraitObject(lg, ls), TraitObject(rg, rs)) => ls == rs && over(lg, rg, |l, r| eq_generic_bound(l, r)),
490         (ImplTrait(_, lg), ImplTrait(_, rg)) => over(lg, rg, |l, r| eq_generic_bound(l, r)),
491         (Typeof(l), Typeof(r)) => eq_expr(&l.value, &r.value),
492         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
493         _ => false,
494     }
495 }
496
497 pub fn eq_ext(l: &Extern, r: &Extern) -> bool {
498     use Extern::*;
499     match (l, r) {
500         (None, None) | (Implicit, Implicit) => true,
501         (Explicit(l), Explicit(r)) => eq_str_lit(l, r),
502         _ => false,
503     }
504 }
505
506 pub fn eq_str_lit(l: &StrLit, r: &StrLit) -> bool {
507     l.style == r.style && l.symbol == r.symbol && l.suffix == r.suffix
508 }
509
510 pub fn eq_poly_ref_trait(l: &PolyTraitRef, r: &PolyTraitRef) -> bool {
511     eq_path(&l.trait_ref.path, &r.trait_ref.path)
512         && over(&l.bound_generic_params, &r.bound_generic_params, |l, r| {
513             eq_generic_param(l, r)
514         })
515 }
516
517 pub fn eq_generic_param(l: &GenericParam, r: &GenericParam) -> bool {
518     use GenericParamKind::*;
519     l.is_placeholder == r.is_placeholder
520         && eq_id(l.ident, r.ident)
521         && over(&l.bounds, &r.bounds, |l, r| eq_generic_bound(l, r))
522         && match (&l.kind, &r.kind) {
523             (Lifetime, Lifetime) => true,
524             (Type { default: l }, Type { default: r }) => both(l, r, |l, r| eq_ty(l, r)),
525             (
526                 Const {
527                     ty: lt,
528                     kw_span: _,
529                     default: ld,
530                 },
531                 Const {
532                     ty: rt,
533                     kw_span: _,
534                     default: rd,
535                 },
536             ) => eq_ty(lt, rt) && both(ld, rd, |ld, rd| eq_anon_const(ld, rd)),
537             _ => false,
538         }
539         && over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))
540 }
541
542 pub fn eq_generic_bound(l: &GenericBound, r: &GenericBound) -> bool {
543     use GenericBound::*;
544     match (l, r) {
545         (Trait(ptr1, tbm1), Trait(ptr2, tbm2)) => tbm1 == tbm2 && eq_poly_ref_trait(ptr1, ptr2),
546         (Outlives(l), Outlives(r)) => eq_id(l.ident, r.ident),
547         _ => false,
548     }
549 }
550
551 pub fn eq_assoc_constraint(l: &AssocTyConstraint, r: &AssocTyConstraint) -> bool {
552     use AssocTyConstraintKind::*;
553     eq_id(l.ident, r.ident)
554         && match (&l.kind, &r.kind) {
555             (Equality { ty: l }, Equality { ty: r }) => eq_ty(l, r),
556             (Bound { bounds: l }, Bound { bounds: r }) => over(l, r, |l, r| eq_generic_bound(l, r)),
557             _ => false,
558         }
559 }
560
561 pub fn eq_mac_call(l: &MacCall, r: &MacCall) -> bool {
562     eq_path(&l.path, &r.path) && eq_mac_args(&l.args, &r.args)
563 }
564
565 pub fn eq_attr(l: &Attribute, r: &Attribute) -> bool {
566     use AttrKind::*;
567     l.style == r.style
568         && match (&l.kind, &r.kind) {
569             (DocComment(l1, l2), DocComment(r1, r2)) => l1 == r1 && l2 == r2,
570             (Normal(l, _), Normal(r, _)) => eq_path(&l.path, &r.path) && eq_mac_args(&l.args, &r.args),
571             _ => false,
572         }
573 }
574
575 pub fn eq_mac_args(l: &MacArgs, r: &MacArgs) -> bool {
576     use MacArgs::*;
577     match (l, r) {
578         (Empty, Empty) => true,
579         (Delimited(_, ld, lts), Delimited(_, rd, rts)) => ld == rd && lts.eq_unspanned(rts),
580         (Eq(_, lt), Eq(_, rt)) => lt.kind == rt.kind,
581         _ => false,
582     }
583 }
584
585 /// Extract args from an assert-like macro.
586 ///
587 /// Currently working with:
588 /// - `assert_eq!` and `assert_ne!`
589 /// - `debug_assert_eq!` and `debug_assert_ne!`
590 ///
591 /// For example:
592 ///
593 /// `debug_assert_eq!(a, b)` will return Some([a, b])
594 pub fn extract_assert_macro_args(mut expr: &Expr) -> Option<[&Expr; 2]> {
595     if_chain! {
596         if let ExprKind::If(_, ref block, _) = expr.kind;
597         if let StmtKind::Semi(ref e) = block.stmts.get(0)?.kind;
598         then {
599             expr = e;
600         }
601     }
602     if_chain! {
603         if let ExprKind::Block(ref block, _) = expr.kind;
604         if let StmtKind::Expr(ref expr) = block.stmts.get(0)?.kind;
605         if let ExprKind::Match(ref match_expr, _) = expr.kind;
606         if let ExprKind::Tup(ref tup) = match_expr.kind;
607         if let [a, b, ..] = tup.as_slice();
608         if let (&ExprKind::AddrOf(_, _, ref a), &ExprKind::AddrOf(_, _, ref b)) = (&a.kind, &b.kind);
609         then {
610             return Some([&*a, &*b]);
611         }
612     }
613     None
614 }