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