]> git.lizzy.rs Git - rust.git/blob - clippy_utils/src/ast_utils.rs
Merge commit 'cb7915b00c235e9b5861564f3be78dba330980ee' into clippyup
[rust.git] / 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, eq_qself) && eq_path(lp, rp),
50         (TupleStruct(lqself, lp, lfs), TupleStruct(rqself, rp, rfs)) => {
51             eq_maybe_qself(lqself, rqself) && eq_path(lp, rp) && over(lfs, rfs, |l, r| eq_pat(l, r))
52         },
53         (Struct(lqself, lp, lfs, lr), Struct(rqself, rp, rfs, rr)) => {
54             lr == rr && eq_maybe_qself(lqself, rqself) && eq_path(lp, rp) && unordered_over(lfs, rfs, eq_field_pat)
55         },
56         (Or(ls), Or(rs)) => unordered_over(ls, rs, |l, r| eq_pat(l, r)),
57         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
58         _ => false,
59     }
60 }
61
62 pub fn eq_range_end(l: &RangeEnd, r: &RangeEnd) -> bool {
63     match (l, r) {
64         (RangeEnd::Excluded, RangeEnd::Excluded) => true,
65         (RangeEnd::Included(l), RangeEnd::Included(r)) => {
66             matches!(l, RangeSyntax::DotDotEq) == matches!(r, RangeSyntax::DotDotEq)
67         },
68         _ => false,
69     }
70 }
71
72 pub fn eq_field_pat(l: &PatField, r: &PatField) -> bool {
73     l.is_placeholder == r.is_placeholder
74         && eq_id(l.ident, r.ident)
75         && eq_pat(&l.pat, &r.pat)
76         && over(&l.attrs, &r.attrs, eq_attr)
77 }
78
79 pub fn eq_qself(l: &QSelf, r: &QSelf) -> bool {
80     l.position == r.position && eq_ty(&l.ty, &r.ty)
81 }
82
83 pub fn eq_maybe_qself(l: &Option<QSelf>, r: &Option<QSelf>) -> bool {
84     match (l, r) {
85         (Some(l), Some(r)) => eq_qself(l, r),
86         (None, None) => true,
87         _ => false,
88     }
89 }
90
91 pub fn eq_path(l: &Path, r: &Path) -> bool {
92     over(&l.segments, &r.segments, eq_path_seg)
93 }
94
95 pub fn eq_path_seg(l: &PathSegment, r: &PathSegment) -> bool {
96     eq_id(l.ident, r.ident) && both(&l.args, &r.args, |l, r| eq_generic_args(l, r))
97 }
98
99 pub fn eq_generic_args(l: &GenericArgs, r: &GenericArgs) -> bool {
100     match (l, r) {
101         (GenericArgs::AngleBracketed(l), GenericArgs::AngleBracketed(r)) => over(&l.args, &r.args, eq_angle_arg),
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, eq_attr) {
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, eq_arm),
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, eq_qself) && 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, eq_field)
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, eq_attr)
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, eq_attr)
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, eq_stmt)
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_local_kind(&l.kind, &r.kind)
220                 && over(&l.attrs, &r.attrs, eq_attr)
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, eq_attr)
227         },
228         _ => false,
229     }
230 }
231
232 pub fn eq_local_kind(l: &LocalKind, r: &LocalKind) -> bool {
233     use LocalKind::*;
234     match (l, r) {
235         (Decl, Decl) => true,
236         (Init(l), Init(r)) => eq_expr(l, r),
237         (InitElse(li, le), InitElse(ri, re)) => eq_expr(li, ri) && eq_block(le, re),
238         _ => false,
239     }
240 }
241
242 pub fn eq_item<K>(l: &Item<K>, r: &Item<K>, mut eq_kind: impl FnMut(&K, &K) -> bool) -> bool {
243     eq_id(l.ident, r.ident) && over(&l.attrs, &r.attrs, eq_attr) && eq_vis(&l.vis, &r.vis) && eq_kind(&l.kind, &r.kind)
244 }
245
246 pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
247     use ItemKind::*;
248     match (l, r) {
249         (ExternCrate(l), ExternCrate(r)) => l == r,
250         (Use(l), Use(r)) => eq_use_tree(l, r),
251         (Static(lt, lm, le), Static(rt, rm, re)) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re),
252         (Const(ld, lt, le), Const(rd, rt, re)) => eq_defaultness(*ld, *rd) && eq_ty(lt, rt) && eq_expr_opt(le, re),
253         (Fn(box FnKind(ld, lf, lg, lb)), Fn(box FnKind(rd, rf, rg, rb))) => {
254             eq_defaultness(*ld, *rd) && eq_fn_sig(lf, rf) && eq_generics(lg, rg) && both(lb, rb, |l, r| eq_block(l, r))
255         },
256         (Mod(lu, lmk), Mod(ru, rmk)) => {
257             lu == ru
258                 && match (lmk, rmk) {
259                     (ModKind::Loaded(litems, linline, _), ModKind::Loaded(ritems, rinline, _)) => {
260                         linline == rinline && over(litems, ritems, |l, r| eq_item(l, r, eq_item_kind))
261                     },
262                     (ModKind::Unloaded, ModKind::Unloaded) => true,
263                     _ => false,
264                 }
265         },
266         (ForeignMod(l), ForeignMod(r)) => {
267             both(&l.abi, &r.abi, eq_str_lit) && over(&l.items, &r.items, |l, r| eq_item(l, r, eq_foreign_item_kind))
268         },
269         (TyAlias(box TyAliasKind(ld, lg, lb, lt)), TyAlias(box TyAliasKind(rd, rg, rb, rt))) => {
270             eq_defaultness(*ld, *rd)
271                 && eq_generics(lg, rg)
272                 && over(lb, rb, eq_generic_bound)
273                 && both(lt, rt, |l, r| eq_ty(l, r))
274         },
275         (Enum(le, lg), Enum(re, rg)) => over(&le.variants, &re.variants, eq_variant) && eq_generics(lg, rg),
276         (Struct(lv, lg), Struct(rv, rg)) | (Union(lv, lg), Union(rv, rg)) => {
277             eq_variant_data(lv, rv) && eq_generics(lg, rg)
278         },
279         (Trait(box TraitKind(la, lu, lg, lb, li)), Trait(box TraitKind(ra, ru, rg, rb, ri))) => {
280             la == ra
281                 && matches!(lu, Unsafe::No) == matches!(ru, Unsafe::No)
282                 && eq_generics(lg, rg)
283                 && over(lb, rb, eq_generic_bound)
284                 && over(li, ri, |l, r| eq_item(l, r, eq_assoc_item_kind))
285         },
286         (TraitAlias(lg, lb), TraitAlias(rg, rb)) => eq_generics(lg, rg) && over(lb, rb, eq_generic_bound),
287         (
288             Impl(box ImplKind {
289                 unsafety: lu,
290                 polarity: lp,
291                 defaultness: ld,
292                 constness: lc,
293                 generics: lg,
294                 of_trait: lot,
295                 self_ty: lst,
296                 items: li,
297             }),
298             Impl(box ImplKind {
299                 unsafety: ru,
300                 polarity: rp,
301                 defaultness: rd,
302                 constness: rc,
303                 generics: rg,
304                 of_trait: rot,
305                 self_ty: rst,
306                 items: ri,
307             }),
308         ) => {
309             matches!(lu, Unsafe::No) == matches!(ru, Unsafe::No)
310                 && matches!(lp, ImplPolarity::Positive) == matches!(rp, ImplPolarity::Positive)
311                 && eq_defaultness(*ld, *rd)
312                 && matches!(lc, ast::Const::No) == matches!(rc, ast::Const::No)
313                 && eq_generics(lg, rg)
314                 && both(lot, rot, |l, r| eq_path(&l.path, &r.path))
315                 && eq_ty(lst, rst)
316                 && over(li, ri, |l, r| eq_item(l, r, eq_assoc_item_kind))
317         },
318         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
319         (MacroDef(l), MacroDef(r)) => l.macro_rules == r.macro_rules && eq_mac_args(&l.body, &r.body),
320         _ => false,
321     }
322 }
323
324 pub fn eq_foreign_item_kind(l: &ForeignItemKind, r: &ForeignItemKind) -> bool {
325     use ForeignItemKind::*;
326     match (l, r) {
327         (Static(lt, lm, le), Static(rt, rm, re)) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re),
328         (Fn(box FnKind(ld, lf, lg, lb)), Fn(box FnKind(rd, rf, rg, rb))) => {
329             eq_defaultness(*ld, *rd) && eq_fn_sig(lf, rf) && eq_generics(lg, rg) && both(lb, rb, |l, r| eq_block(l, r))
330         },
331         (TyAlias(box TyAliasKind(ld, lg, lb, lt)), TyAlias(box TyAliasKind(rd, rg, rb, rt))) => {
332             eq_defaultness(*ld, *rd)
333                 && eq_generics(lg, rg)
334                 && over(lb, rb, eq_generic_bound)
335                 && both(lt, rt, |l, r| eq_ty(l, r))
336         },
337         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
338         _ => false,
339     }
340 }
341
342 pub fn eq_assoc_item_kind(l: &AssocItemKind, r: &AssocItemKind) -> bool {
343     use AssocItemKind::*;
344     match (l, r) {
345         (Const(ld, lt, le), Const(rd, rt, re)) => eq_defaultness(*ld, *rd) && eq_ty(lt, rt) && eq_expr_opt(le, re),
346         (Fn(box FnKind(ld, lf, lg, lb)), Fn(box FnKind(rd, rf, rg, rb))) => {
347             eq_defaultness(*ld, *rd) && eq_fn_sig(lf, rf) && eq_generics(lg, rg) && both(lb, rb, |l, r| eq_block(l, r))
348         },
349         (TyAlias(box TyAliasKind(ld, lg, lb, lt)), TyAlias(box TyAliasKind(rd, rg, rb, rt))) => {
350             eq_defaultness(*ld, *rd)
351                 && eq_generics(lg, rg)
352                 && over(lb, rb, eq_generic_bound)
353                 && both(lt, rt, |l, r| eq_ty(l, r))
354         },
355         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
356         _ => false,
357     }
358 }
359
360 pub fn eq_variant(l: &Variant, r: &Variant) -> bool {
361     l.is_placeholder == r.is_placeholder
362         && over(&l.attrs, &r.attrs, eq_attr)
363         && eq_vis(&l.vis, &r.vis)
364         && eq_id(l.ident, r.ident)
365         && eq_variant_data(&l.data, &r.data)
366         && both(&l.disr_expr, &r.disr_expr, |l, r| eq_expr(&l.value, &r.value))
367 }
368
369 pub fn eq_variant_data(l: &VariantData, r: &VariantData) -> bool {
370     use VariantData::*;
371     match (l, r) {
372         (Unit(_), Unit(_)) => true,
373         (Struct(l, _), Struct(r, _)) | (Tuple(l, _), Tuple(r, _)) => over(l, r, eq_struct_field),
374         _ => false,
375     }
376 }
377
378 pub fn eq_struct_field(l: &FieldDef, r: &FieldDef) -> bool {
379     l.is_placeholder == r.is_placeholder
380         && over(&l.attrs, &r.attrs, eq_attr)
381         && eq_vis(&l.vis, &r.vis)
382         && both(&l.ident, &r.ident, |l, r| eq_id(*l, *r))
383         && eq_ty(&l.ty, &r.ty)
384 }
385
386 pub fn eq_fn_sig(l: &FnSig, r: &FnSig) -> bool {
387     eq_fn_decl(&l.decl, &r.decl) && eq_fn_header(&l.header, &r.header)
388 }
389
390 pub fn eq_fn_header(l: &FnHeader, r: &FnHeader) -> bool {
391     matches!(l.unsafety, Unsafe::No) == matches!(r.unsafety, Unsafe::No)
392         && l.asyncness.is_async() == r.asyncness.is_async()
393         && matches!(l.constness, Const::No) == matches!(r.constness, Const::No)
394         && eq_ext(&l.ext, &r.ext)
395 }
396
397 pub fn eq_generics(l: &Generics, r: &Generics) -> bool {
398     over(&l.params, &r.params, eq_generic_param)
399         && over(&l.where_clause.predicates, &r.where_clause.predicates, |l, r| {
400             eq_where_predicate(l, r)
401         })
402 }
403
404 pub fn eq_where_predicate(l: &WherePredicate, r: &WherePredicate) -> bool {
405     use WherePredicate::*;
406     match (l, r) {
407         (BoundPredicate(l), BoundPredicate(r)) => {
408             over(&l.bound_generic_params, &r.bound_generic_params, |l, r| {
409                 eq_generic_param(l, r)
410             }) && eq_ty(&l.bounded_ty, &r.bounded_ty)
411                 && over(&l.bounds, &r.bounds, eq_generic_bound)
412         },
413         (RegionPredicate(l), RegionPredicate(r)) => {
414             eq_id(l.lifetime.ident, r.lifetime.ident) && over(&l.bounds, &r.bounds, eq_generic_bound)
415         },
416         (EqPredicate(l), EqPredicate(r)) => eq_ty(&l.lhs_ty, &r.lhs_ty) && eq_ty(&l.rhs_ty, &r.rhs_ty),
417         _ => false,
418     }
419 }
420
421 pub fn eq_use_tree(l: &UseTree, r: &UseTree) -> bool {
422     eq_path(&l.prefix, &r.prefix) && eq_use_tree_kind(&l.kind, &r.kind)
423 }
424
425 pub fn eq_anon_const(l: &AnonConst, r: &AnonConst) -> bool {
426     eq_expr(&l.value, &r.value)
427 }
428
429 pub fn eq_use_tree_kind(l: &UseTreeKind, r: &UseTreeKind) -> bool {
430     use UseTreeKind::*;
431     match (l, r) {
432         (Glob, Glob) => true,
433         (Simple(l, _, _), Simple(r, _, _)) => both(l, r, |l, r| eq_id(*l, *r)),
434         (Nested(l), Nested(r)) => over(l, r, |(l, _), (r, _)| eq_use_tree(l, r)),
435         _ => false,
436     }
437 }
438
439 pub fn eq_defaultness(l: Defaultness, r: Defaultness) -> bool {
440     matches!(
441         (l, r),
442         (Defaultness::Final, Defaultness::Final) | (Defaultness::Default(_), Defaultness::Default(_))
443     )
444 }
445
446 pub fn eq_vis(l: &Visibility, r: &Visibility) -> bool {
447     use VisibilityKind::*;
448     match (&l.kind, &r.kind) {
449         (Public, Public) | (Inherited, Inherited) | (Crate(_), Crate(_)) => true,
450         (Restricted { path: l, .. }, Restricted { path: r, .. }) => eq_path(l, r),
451         _ => false,
452     }
453 }
454
455 pub fn eq_fn_decl(l: &FnDecl, r: &FnDecl) -> bool {
456     eq_fn_ret_ty(&l.output, &r.output)
457         && over(&l.inputs, &r.inputs, |l, r| {
458             l.is_placeholder == r.is_placeholder
459                 && eq_pat(&l.pat, &r.pat)
460                 && eq_ty(&l.ty, &r.ty)
461                 && over(&l.attrs, &r.attrs, eq_attr)
462         })
463 }
464
465 pub fn eq_fn_ret_ty(l: &FnRetTy, r: &FnRetTy) -> bool {
466     match (l, r) {
467         (FnRetTy::Default(_), FnRetTy::Default(_)) => true,
468         (FnRetTy::Ty(l), FnRetTy::Ty(r)) => eq_ty(l, r),
469         _ => false,
470     }
471 }
472
473 pub fn eq_ty(l: &Ty, r: &Ty) -> bool {
474     use TyKind::*;
475     match (&l.kind, &r.kind) {
476         (Paren(l), _) => eq_ty(l, r),
477         (_, Paren(r)) => eq_ty(l, r),
478         (Never, Never) | (Infer, Infer) | (ImplicitSelf, ImplicitSelf) | (Err, Err) | (CVarArgs, CVarArgs) => true,
479         (Slice(l), Slice(r)) => eq_ty(l, r),
480         (Array(le, ls), Array(re, rs)) => eq_ty(le, re) && eq_expr(&ls.value, &rs.value),
481         (Ptr(l), Ptr(r)) => l.mutbl == r.mutbl && eq_ty(&l.ty, &r.ty),
482         (Rptr(ll, l), Rptr(rl, r)) => {
483             both(ll, rl, |l, r| eq_id(l.ident, r.ident)) && l.mutbl == r.mutbl && eq_ty(&l.ty, &r.ty)
484         },
485         (BareFn(l), BareFn(r)) => {
486             l.unsafety == r.unsafety
487                 && eq_ext(&l.ext, &r.ext)
488                 && over(&l.generic_params, &r.generic_params, eq_generic_param)
489                 && eq_fn_decl(&l.decl, &r.decl)
490         },
491         (Tup(l), Tup(r)) => over(l, r, |l, r| eq_ty(l, r)),
492         (Path(lq, lp), Path(rq, rp)) => both(lq, rq, eq_qself) && eq_path(lp, rp),
493         (TraitObject(lg, ls), TraitObject(rg, rs)) => ls == rs && over(lg, rg, eq_generic_bound),
494         (ImplTrait(_, lg), ImplTrait(_, rg)) => over(lg, rg, eq_generic_bound),
495         (Typeof(l), Typeof(r)) => eq_expr(&l.value, &r.value),
496         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
497         _ => false,
498     }
499 }
500
501 pub fn eq_ext(l: &Extern, r: &Extern) -> bool {
502     use Extern::*;
503     match (l, r) {
504         (None, None) | (Implicit, Implicit) => true,
505         (Explicit(l), Explicit(r)) => eq_str_lit(l, r),
506         _ => false,
507     }
508 }
509
510 pub fn eq_str_lit(l: &StrLit, r: &StrLit) -> bool {
511     l.style == r.style && l.symbol == r.symbol && l.suffix == r.suffix
512 }
513
514 pub fn eq_poly_ref_trait(l: &PolyTraitRef, r: &PolyTraitRef) -> bool {
515     eq_path(&l.trait_ref.path, &r.trait_ref.path)
516         && over(&l.bound_generic_params, &r.bound_generic_params, |l, r| {
517             eq_generic_param(l, r)
518         })
519 }
520
521 pub fn eq_generic_param(l: &GenericParam, r: &GenericParam) -> bool {
522     use GenericParamKind::*;
523     l.is_placeholder == r.is_placeholder
524         && eq_id(l.ident, r.ident)
525         && over(&l.bounds, &r.bounds, eq_generic_bound)
526         && match (&l.kind, &r.kind) {
527             (Lifetime, Lifetime) => true,
528             (Type { default: l }, Type { default: r }) => both(l, r, |l, r| eq_ty(l, r)),
529             (
530                 Const {
531                     ty: lt,
532                     kw_span: _,
533                     default: ld,
534                 },
535                 Const {
536                     ty: rt,
537                     kw_span: _,
538                     default: rd,
539                 },
540             ) => eq_ty(lt, rt) && both(ld, rd, eq_anon_const),
541             _ => false,
542         }
543         && over(&l.attrs, &r.attrs, eq_attr)
544 }
545
546 pub fn eq_generic_bound(l: &GenericBound, r: &GenericBound) -> bool {
547     use GenericBound::*;
548     match (l, r) {
549         (Trait(ptr1, tbm1), Trait(ptr2, tbm2)) => tbm1 == tbm2 && eq_poly_ref_trait(ptr1, ptr2),
550         (Outlives(l), Outlives(r)) => eq_id(l.ident, r.ident),
551         _ => false,
552     }
553 }
554
555 pub fn eq_assoc_constraint(l: &AssocTyConstraint, r: &AssocTyConstraint) -> bool {
556     use AssocTyConstraintKind::*;
557     eq_id(l.ident, r.ident)
558         && match (&l.kind, &r.kind) {
559             (Equality { ty: l }, Equality { ty: r }) => eq_ty(l, r),
560             (Bound { bounds: l }, Bound { bounds: r }) => over(l, r, eq_generic_bound),
561             _ => false,
562         }
563 }
564
565 pub fn eq_mac_call(l: &MacCall, r: &MacCall) -> bool {
566     eq_path(&l.path, &r.path) && eq_mac_args(&l.args, &r.args)
567 }
568
569 pub fn eq_attr(l: &Attribute, r: &Attribute) -> bool {
570     use AttrKind::*;
571     l.style == r.style
572         && match (&l.kind, &r.kind) {
573             (DocComment(l1, l2), DocComment(r1, r2)) => l1 == r1 && l2 == r2,
574             (Normal(l, _), Normal(r, _)) => eq_path(&l.path, &r.path) && eq_mac_args(&l.args, &r.args),
575             _ => false,
576         }
577 }
578
579 pub fn eq_mac_args(l: &MacArgs, r: &MacArgs) -> bool {
580     use MacArgs::*;
581     match (l, r) {
582         (Empty, Empty) => true,
583         (Delimited(_, ld, lts), Delimited(_, rd, rts)) => ld == rd && lts.eq_unspanned(rts),
584         (Eq(_, lt), Eq(_, rt)) => lt.kind == rt.kind,
585         _ => false,
586     }
587 }
588
589 /// Extract args from an assert-like macro.
590 ///
591 /// Currently working with:
592 /// - `assert_eq!` and `assert_ne!`
593 /// - `debug_assert_eq!` and `debug_assert_ne!`
594 ///
595 /// For example:
596 ///
597 /// `debug_assert_eq!(a, b)` will return Some([a, b])
598 pub fn extract_assert_macro_args(mut expr: &Expr) -> Option<[&Expr; 2]> {
599     if_chain! {
600         if let ExprKind::If(_, ref block, _) = expr.kind;
601         if let StmtKind::Semi(ref e) = block.stmts.get(0)?.kind;
602         then {
603             expr = e;
604         }
605     }
606     if_chain! {
607         if let ExprKind::Block(ref block, _) = expr.kind;
608         if let StmtKind::Expr(ref expr) = block.stmts.get(0)?.kind;
609         if let ExprKind::Match(ref match_expr, _) = expr.kind;
610         if let ExprKind::Tup(ref tup) = match_expr.kind;
611         if let [a, b, ..] = tup.as_slice();
612         if let (&ExprKind::AddrOf(_, _, ref a), &ExprKind::AddrOf(_, _, ref b)) = (&a.kind, &b.kind);
613         then {
614             return Some([&*a, &*b]);
615         }
616     }
617     None
618 }