]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/utils/ast_utils.rs
Rollup merge of #81588 - xfix:delete-doc-alias, r=Mark-Simulacrum
[rust.git] / src / tools / clippy / clippy_lints / src / utils / 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::utils::{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, |l, r| eq_qself(l, r)) && eq_path(lp, rp),
49         (TupleStruct(lp, lfs), TupleStruct(rp, rfs)) => eq_path(lp, rp) && over(lfs, rfs, |l, r| eq_pat(l, r)),
50         (Struct(lp, lfs, lr), Struct(rp, rfs, rr)) => {
51             lr == rr && eq_path(lp, rp) && unordered_over(lfs, rfs, |lf, rf| eq_field_pat(lf, rf))
52         },
53         (Or(ls), Or(rs)) => unordered_over(ls, rs, |l, r| eq_pat(l, r)),
54         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
55         _ => false,
56     }
57 }
58
59 pub fn eq_range_end(l: &RangeEnd, r: &RangeEnd) -> bool {
60     match (l, r) {
61         (RangeEnd::Excluded, RangeEnd::Excluded) => true,
62         (RangeEnd::Included(l), RangeEnd::Included(r)) => {
63             matches!(l, RangeSyntax::DotDotEq) == matches!(r, RangeSyntax::DotDotEq)
64         },
65         _ => false,
66     }
67 }
68
69 pub fn eq_field_pat(l: &FieldPat, r: &FieldPat) -> bool {
70     l.is_placeholder == r.is_placeholder
71         && eq_id(l.ident, r.ident)
72         && eq_pat(&l.pat, &r.pat)
73         && over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))
74 }
75
76 pub fn eq_qself(l: &QSelf, r: &QSelf) -> bool {
77     l.position == r.position && eq_ty(&l.ty, &r.ty)
78 }
79
80 pub fn eq_path(l: &Path, r: &Path) -> bool {
81     over(&l.segments, &r.segments, |l, r| eq_path_seg(l, r))
82 }
83
84 pub fn eq_path_seg(l: &PathSegment, r: &PathSegment) -> bool {
85     eq_id(l.ident, r.ident) && both(&l.args, &r.args, |l, r| eq_generic_args(l, r))
86 }
87
88 pub fn eq_generic_args(l: &GenericArgs, r: &GenericArgs) -> bool {
89     match (l, r) {
90         (GenericArgs::AngleBracketed(l), GenericArgs::AngleBracketed(r)) => {
91             over(&l.args, &r.args, |l, r| eq_angle_arg(l, r))
92         },
93         (GenericArgs::Parenthesized(l), GenericArgs::Parenthesized(r)) => {
94             over(&l.inputs, &r.inputs, |l, r| eq_ty(l, r)) && eq_fn_ret_ty(&l.output, &r.output)
95         },
96         _ => false,
97     }
98 }
99
100 pub fn eq_angle_arg(l: &AngleBracketedArg, r: &AngleBracketedArg) -> bool {
101     match (l, r) {
102         (AngleBracketedArg::Arg(l), AngleBracketedArg::Arg(r)) => eq_generic_arg(l, r),
103         (AngleBracketedArg::Constraint(l), AngleBracketedArg::Constraint(r)) => eq_assoc_constraint(l, r),
104         _ => false,
105     }
106 }
107
108 pub fn eq_generic_arg(l: &GenericArg, r: &GenericArg) -> bool {
109     match (l, r) {
110         (GenericArg::Lifetime(l), GenericArg::Lifetime(r)) => eq_id(l.ident, r.ident),
111         (GenericArg::Type(l), GenericArg::Type(r)) => eq_ty(l, r),
112         (GenericArg::Const(l), GenericArg::Const(r)) => eq_expr(&l.value, &r.value),
113         _ => false,
114     }
115 }
116
117 pub fn eq_expr_opt(l: &Option<P<Expr>>, r: &Option<P<Expr>>) -> bool {
118     both(l, r, |l, r| eq_expr(l, r))
119 }
120
121 pub fn eq_struct_rest(l: &StructRest, r: &StructRest) -> bool {
122     match (l, r) {
123         (StructRest::Base(lb), StructRest::Base(rb)) => eq_expr(lb, rb),
124         (StructRest::Rest(_), StructRest::Rest(_)) | (StructRest::None, StructRest::None) => true,
125         _ => false,
126     }
127 }
128
129 pub fn eq_expr(l: &Expr, r: &Expr) -> bool {
130     use ExprKind::*;
131     if !over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r)) {
132         return false;
133     }
134     match (&l.kind, &r.kind) {
135         (Paren(l), _) => eq_expr(l, r),
136         (_, Paren(r)) => eq_expr(l, r),
137         (Err, Err) => true,
138         (Box(l), Box(r)) | (Try(l), Try(r)) | (Await(l), Await(r)) => eq_expr(l, r),
139         (Array(l), Array(r)) | (Tup(l), Tup(r)) => over(l, r, |l, r| eq_expr(l, r)),
140         (Repeat(le, ls), Repeat(re, rs)) => eq_expr(le, re) && eq_expr(&ls.value, &rs.value),
141         (Call(lc, la), Call(rc, ra)) => eq_expr(lc, rc) && over(la, ra, |l, r| eq_expr(l, r)),
142         (MethodCall(lc, la, _), MethodCall(rc, ra, _)) => eq_path_seg(lc, rc) && over(la, ra, |l, r| eq_expr(l, r)),
143         (Binary(lo, ll, lr), Binary(ro, rl, rr)) => lo.node == ro.node && eq_expr(ll, rl) && eq_expr(lr, rr),
144         (Unary(lo, l), Unary(ro, r)) => mem::discriminant(lo) == mem::discriminant(ro) && eq_expr(l, r),
145         (Lit(l), Lit(r)) => l.kind == r.kind,
146         (Cast(l, lt), Cast(r, rt)) | (Type(l, lt), Type(r, rt)) => eq_expr(l, r) && eq_ty(lt, rt),
147         (Let(lp, le), Let(rp, re)) => eq_pat(lp, rp) && eq_expr(le, re),
148         (If(lc, lt, le), If(rc, rt, re)) => eq_expr(lc, rc) && eq_block(lt, rt) && eq_expr_opt(le, re),
149         (While(lc, lt, ll), While(rc, rt, rl)) => eq_label(ll, rl) && eq_expr(lc, rc) && eq_block(lt, rt),
150         (ForLoop(lp, li, lt, ll), ForLoop(rp, ri, rt, rl)) => {
151             eq_label(ll, rl) && eq_pat(lp, rp) && eq_expr(li, ri) && eq_block(lt, rt)
152         },
153         (Loop(lt, ll), Loop(rt, rl)) => eq_label(ll, rl) && eq_block(lt, rt),
154         (Block(lb, ll), Block(rb, rl)) => eq_label(ll, rl) && eq_block(lb, rb),
155         (TryBlock(l), TryBlock(r)) => eq_block(l, r),
156         (Yield(l), Yield(r)) | (Ret(l), Ret(r)) => eq_expr_opt(l, r),
157         (Break(ll, le), Break(rl, re)) => eq_label(ll, rl) && eq_expr_opt(le, re),
158         (Continue(ll), Continue(rl)) => eq_label(ll, rl),
159         (Assign(l1, l2, _), Assign(r1, r2, _)) | (Index(l1, l2), Index(r1, r2)) => eq_expr(l1, r1) && eq_expr(l2, r2),
160         (AssignOp(lo, lp, lv), AssignOp(ro, rp, rv)) => lo.node == ro.node && eq_expr(lp, rp) && eq_expr(lv, rv),
161         (Field(lp, lf), Field(rp, rf)) => eq_id(*lf, *rf) && eq_expr(lp, rp),
162         (Match(ls, la), Match(rs, ra)) => eq_expr(ls, rs) && over(la, ra, |l, r| eq_arm(l, r)),
163         (Closure(lc, la, lm, lf, lb, _), Closure(rc, ra, rm, rf, rb, _)) => {
164             lc == rc && la.is_async() == ra.is_async() && lm == rm && eq_fn_decl(lf, rf) && eq_expr(lb, rb)
165         },
166         (Async(lc, _, lb), Async(rc, _, rb)) => lc == rc && eq_block(lb, rb),
167         (Range(lf, lt, ll), Range(rf, rt, rl)) => ll == rl && eq_expr_opt(lf, rf) && eq_expr_opt(lt, rt),
168         (AddrOf(lbk, lm, le), AddrOf(rbk, rm, re)) => lbk == rbk && lm == rm && eq_expr(le, re),
169         (Path(lq, lp), Path(rq, rp)) => both(lq, rq, |l, r| eq_qself(l, r)) && eq_path(lp, rp),
170         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
171         (Struct(lp, lfs, lb), Struct(rp, rfs, rb)) => {
172             eq_path(lp, rp) && eq_struct_rest(lb, rb) && unordered_over(lfs, rfs, |l, r| eq_field(l, r))
173         },
174         _ => false,
175     }
176 }
177
178 pub fn eq_field(l: &Field, r: &Field) -> bool {
179     l.is_placeholder == r.is_placeholder
180         && eq_id(l.ident, r.ident)
181         && eq_expr(&l.expr, &r.expr)
182         && over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))
183 }
184
185 pub fn eq_arm(l: &Arm, r: &Arm) -> bool {
186     l.is_placeholder == r.is_placeholder
187         && eq_pat(&l.pat, &r.pat)
188         && eq_expr(&l.body, &r.body)
189         && eq_expr_opt(&l.guard, &r.guard)
190         && over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))
191 }
192
193 pub fn eq_label(l: &Option<Label>, r: &Option<Label>) -> bool {
194     both(l, r, |l, r| eq_id(l.ident, r.ident))
195 }
196
197 pub fn eq_block(l: &Block, r: &Block) -> bool {
198     l.rules == r.rules && over(&l.stmts, &r.stmts, |l, r| eq_stmt(l, r))
199 }
200
201 pub fn eq_stmt(l: &Stmt, r: &Stmt) -> bool {
202     use StmtKind::*;
203     match (&l.kind, &r.kind) {
204         (Local(l), Local(r)) => {
205             eq_pat(&l.pat, &r.pat)
206                 && both(&l.ty, &r.ty, |l, r| eq_ty(l, r))
207                 && eq_expr_opt(&l.init, &r.init)
208                 && over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))
209         },
210         (Item(l), Item(r)) => eq_item(l, r, eq_item_kind),
211         (Expr(l), Expr(r)) | (Semi(l), Semi(r)) => eq_expr(l, r),
212         (Empty, Empty) => true,
213         (MacCall(l), MacCall(r)) => {
214             l.style == r.style && eq_mac_call(&l.mac, &r.mac) && over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))
215         },
216         _ => false,
217     }
218 }
219
220 pub fn eq_item<K>(l: &Item<K>, r: &Item<K>, mut eq_kind: impl FnMut(&K, &K) -> bool) -> bool {
221     eq_id(l.ident, r.ident)
222         && over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))
223         && eq_vis(&l.vis, &r.vis)
224         && eq_kind(&l.kind, &r.kind)
225 }
226
227 pub fn eq_item_kind(l: &ItemKind, r: &ItemKind) -> bool {
228     use ItemKind::*;
229     match (l, r) {
230         (ExternCrate(l), ExternCrate(r)) => l == r,
231         (Use(l), Use(r)) => eq_use_tree(l, r),
232         (Static(lt, lm, le), Static(rt, rm, re)) => {
233             lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re)
234         }
235         (Const(ld, lt, le), Const(rd, rt, re)) => {
236             eq_defaultness(*ld, *rd) && eq_ty(lt, rt) && eq_expr_opt(le, re)
237         }
238         (Fn(box FnKind(ld, lf, lg, lb)), Fn(box FnKind(rd, rf, rg, rb))) => {
239             eq_defaultness(*ld, *rd)
240                 && eq_fn_sig(lf, rf)
241                 && eq_generics(lg, rg)
242                 && both(lb, rb, |l, r| eq_block(l, r))
243         }
244         (Mod(l), Mod(r)) => {
245             l.inline == r.inline && over(&l.items, &r.items, |l, r| eq_item(l, r, eq_item_kind))
246         }
247         (ForeignMod(l), ForeignMod(r)) => {
248             both(&l.abi, &r.abi, |l, r| eq_str_lit(l, r))
249                 && over(&l.items, &r.items, |l, r| eq_item(l, r, eq_foreign_item_kind))
250         }
251         (TyAlias(box TyAliasKind(ld, lg, lb, lt)), TyAlias(box TyAliasKind(rd, rg, rb, rt))) => {
252             eq_defaultness(*ld, *rd)
253                 && eq_generics(lg, rg)
254                 && over(lb, rb, |l, r| eq_generic_bound(l, r))
255                 && both(lt, rt, |l, r| eq_ty(l, r))
256         },
257         (Enum(le, lg), Enum(re, rg)) => {
258             over(&le.variants, &re.variants, |l, r| eq_variant(l, r)) && eq_generics(lg, rg)
259         },
260         (Struct(lv, lg), Struct(rv, rg)) | (Union(lv, lg), Union(rv, rg)) => {
261             eq_variant_data(lv, rv) && eq_generics(lg, rg)
262         }
263         (Trait(box TraitKind(la, lu, lg, lb, li)), Trait(box TraitKind(ra, ru, rg, rb, ri))) => {
264             la == ra
265                 && matches!(lu, Unsafe::No) == matches!(ru, Unsafe::No)
266                 && eq_generics(lg, rg)
267                 && over(lb, rb, |l, r| eq_generic_bound(l, r))
268                 && over(li, ri, |l, r| eq_item(l, r, eq_assoc_item_kind))
269         },
270         (TraitAlias(lg, lb), TraitAlias(rg, rb)) => eq_generics(lg, rg) && over(lb, rb, |l, r| eq_generic_bound(l, r)),
271         (
272             Impl(box ImplKind {
273                 unsafety: lu,
274                 polarity: lp,
275                 defaultness: ld,
276                 constness: lc,
277                 generics: lg,
278                 of_trait: lot,
279                 self_ty: lst,
280                 items: li,
281             }),
282             Impl(box ImplKind {
283                 unsafety: ru,
284                 polarity: rp,
285                 defaultness: rd,
286                 constness: rc,
287                 generics: rg,
288                 of_trait: rot,
289                 self_ty: rst,
290                 items: ri,
291             }),
292         ) => {
293             matches!(lu, Unsafe::No) == matches!(ru, Unsafe::No)
294                 && matches!(lp, ImplPolarity::Positive) == matches!(rp, ImplPolarity::Positive)
295                 && eq_defaultness(*ld, *rd)
296                 && matches!(lc, ast::Const::No) == matches!(rc, ast::Const::No)
297                 && eq_generics(lg, rg)
298                 && both(lot, rot, |l, r| eq_path(&l.path, &r.path))
299                 && eq_ty(lst, rst)
300                 && over(li, ri, |l, r| eq_item(l, r, eq_assoc_item_kind))
301         },
302         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
303         (MacroDef(l), MacroDef(r)) => l.macro_rules == r.macro_rules && eq_mac_args(&l.body, &r.body),
304         _ => false,
305     }
306 }
307
308 pub fn eq_foreign_item_kind(l: &ForeignItemKind, r: &ForeignItemKind) -> bool {
309     use ForeignItemKind::*;
310     match (l, r) {
311         (Static(lt, lm, le), Static(rt, rm, re)) => {
312             lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re)
313         }
314         (Fn(box FnKind(ld, lf, lg, lb)), Fn(box FnKind(rd, rf, rg, rb))) => {
315             eq_defaultness(*ld, *rd)
316                 && eq_fn_sig(lf, rf)
317                 && eq_generics(lg, rg)
318                 && both(lb, rb, |l, r| eq_block(l, r))
319         }
320         (TyAlias(box TyAliasKind(ld, lg, lb, lt)), TyAlias(box TyAliasKind(rd, rg, rb, rt))) => {
321             eq_defaultness(*ld, *rd)
322                 && eq_generics(lg, rg)
323                 && over(lb, rb, |l, r| eq_generic_bound(l, r))
324                 && both(lt, rt, |l, r| eq_ty(l, r))
325         },
326         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
327         _ => false,
328     }
329 }
330
331 pub fn eq_assoc_item_kind(l: &AssocItemKind, r: &AssocItemKind) -> bool {
332     use AssocItemKind::*;
333     match (l, r) {
334         (Const(ld, lt, le), Const(rd, rt, re)) => {
335             eq_defaultness(*ld, *rd) && eq_ty(lt, rt) && eq_expr_opt(le, re)
336         }
337         (Fn(box FnKind(ld, lf, lg, lb)), Fn(box FnKind(rd, rf, rg, rb))) => {
338             eq_defaultness(*ld, *rd)
339                 && eq_fn_sig(lf, rf)
340                 && eq_generics(lg, rg)
341                 && both(lb, rb, |l, r| eq_block(l, r))
342         }
343         (TyAlias(box TyAliasKind(ld, lg, lb, lt)), TyAlias(box TyAliasKind(rd, rg, rb, rt))) => {
344             eq_defaultness(*ld, *rd)
345                 && eq_generics(lg, rg)
346                 && over(lb, rb, |l, r| eq_generic_bound(l, r))
347                 && both(lt, rt, |l, r| eq_ty(l, r))
348         },
349         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
350         _ => false,
351     }
352 }
353
354 pub fn eq_variant(l: &Variant, r: &Variant) -> bool {
355     l.is_placeholder == r.is_placeholder
356         && over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))
357         && eq_vis(&l.vis, &r.vis)
358         && eq_id(l.ident, r.ident)
359         && eq_variant_data(&l.data, &r.data)
360         && both(&l.disr_expr, &r.disr_expr, |l, r| eq_expr(&l.value, &r.value))
361 }
362
363 pub fn eq_variant_data(l: &VariantData, r: &VariantData) -> bool {
364     use VariantData::*;
365     match (l, r) {
366         (Unit(_), Unit(_)) => true,
367         (Struct(l, _), Struct(r, _)) | (Tuple(l, _), Tuple(r, _)) => over(l, r, |l, r| eq_struct_field(l, r)),
368         _ => false,
369     }
370 }
371
372 pub fn eq_struct_field(l: &StructField, r: &StructField) -> bool {
373     l.is_placeholder == r.is_placeholder
374         && over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))
375         && eq_vis(&l.vis, &r.vis)
376         && both(&l.ident, &r.ident, |l, r| eq_id(*l, *r))
377         && eq_ty(&l.ty, &r.ty)
378 }
379
380 pub fn eq_fn_sig(l: &FnSig, r: &FnSig) -> bool {
381     eq_fn_decl(&l.decl, &r.decl) && eq_fn_header(&l.header, &r.header)
382 }
383
384 pub fn eq_fn_header(l: &FnHeader, r: &FnHeader) -> bool {
385     matches!(l.unsafety, Unsafe::No) == matches!(r.unsafety, Unsafe::No)
386         && l.asyncness.is_async() == r.asyncness.is_async()
387         && matches!(l.constness, Const::No) == matches!(r.constness, Const::No)
388         && eq_ext(&l.ext, &r.ext)
389 }
390
391 pub fn eq_generics(l: &Generics, r: &Generics) -> bool {
392     over(&l.params, &r.params, |l, r| eq_generic_param(l, r))
393         && over(&l.where_clause.predicates, &r.where_clause.predicates, |l, r| {
394             eq_where_predicate(l, r)
395         })
396 }
397
398 pub fn eq_where_predicate(l: &WherePredicate, r: &WherePredicate) -> bool {
399     use WherePredicate::*;
400     match (l, r) {
401         (BoundPredicate(l), BoundPredicate(r)) => {
402             over(&l.bound_generic_params, &r.bound_generic_params, |l, r| {
403                 eq_generic_param(l, r)
404             }) && eq_ty(&l.bounded_ty, &r.bounded_ty)
405                 && over(&l.bounds, &r.bounds, |l, r| eq_generic_bound(l, r))
406         },
407         (RegionPredicate(l), RegionPredicate(r)) => {
408             eq_id(l.lifetime.ident, r.lifetime.ident) && over(&l.bounds, &r.bounds, |l, r| eq_generic_bound(l, r))
409         },
410         (EqPredicate(l), EqPredicate(r)) => eq_ty(&l.lhs_ty, &r.lhs_ty) && eq_ty(&l.rhs_ty, &r.rhs_ty),
411         _ => false,
412     }
413 }
414
415 pub fn eq_use_tree(l: &UseTree, r: &UseTree) -> bool {
416     eq_path(&l.prefix, &r.prefix) && eq_use_tree_kind(&l.kind, &r.kind)
417 }
418
419 pub fn eq_use_tree_kind(l: &UseTreeKind, r: &UseTreeKind) -> bool {
420     use UseTreeKind::*;
421     match (l, r) {
422         (Glob, Glob) => true,
423         (Simple(l, _, _), Simple(r, _, _)) => both(l, r, |l, r| eq_id(*l, *r)),
424         (Nested(l), Nested(r)) => over(l, r, |(l, _), (r, _)| eq_use_tree(l, r)),
425         _ => false,
426     }
427 }
428
429 pub fn eq_anon_const(l: &AnonConst, r: &AnonConst) -> bool {
430     eq_expr(&l.value, &r.value)
431 }
432
433 pub fn eq_defaultness(l: Defaultness, r: Defaultness) -> bool {
434     matches!(
435         (l, r),
436         (Defaultness::Final, Defaultness::Final) | (Defaultness::Default(_), Defaultness::Default(_))
437     )
438 }
439
440 pub fn eq_vis(l: &Visibility, r: &Visibility) -> bool {
441     use VisibilityKind::*;
442     match (&l.kind, &r.kind) {
443         (Public, Public) | (Inherited, Inherited) | (Crate(_), Crate(_)) => true,
444         (Restricted { path: l, .. }, Restricted { path: r, .. }) => eq_path(l, r),
445         _ => false,
446     }
447 }
448
449 pub fn eq_fn_decl(l: &FnDecl, r: &FnDecl) -> bool {
450     eq_fn_ret_ty(&l.output, &r.output)
451         && over(&l.inputs, &r.inputs, |l, r| {
452             l.is_placeholder == r.is_placeholder
453                 && eq_pat(&l.pat, &r.pat)
454                 && eq_ty(&l.ty, &r.ty)
455                 && over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))
456         })
457 }
458
459 pub fn eq_fn_ret_ty(l: &FnRetTy, r: &FnRetTy) -> bool {
460     match (l, r) {
461         (FnRetTy::Default(_), FnRetTy::Default(_)) => true,
462         (FnRetTy::Ty(l), FnRetTy::Ty(r)) => eq_ty(l, r),
463         _ => false,
464     }
465 }
466
467 pub fn eq_ty(l: &Ty, r: &Ty) -> bool {
468     use TyKind::*;
469     match (&l.kind, &r.kind) {
470         (Paren(l), _) => eq_ty(l, r),
471         (_, Paren(r)) => eq_ty(l, r),
472         (Never, Never) | (Infer, Infer) | (ImplicitSelf, ImplicitSelf) | (Err, Err) | (CVarArgs, CVarArgs) => true,
473         (Slice(l), Slice(r)) => eq_ty(l, r),
474         (Array(le, ls), Array(re, rs)) => eq_ty(le, re) && eq_expr(&ls.value, &rs.value),
475         (Ptr(l), Ptr(r)) => l.mutbl == r.mutbl && eq_ty(&l.ty, &r.ty),
476         (Rptr(ll, l), Rptr(rl, r)) => {
477             both(ll, rl, |l, r| eq_id(l.ident, r.ident)) && l.mutbl == r.mutbl && eq_ty(&l.ty, &r.ty)
478         },
479         (BareFn(l), BareFn(r)) => {
480             l.unsafety == r.unsafety
481                 && eq_ext(&l.ext, &r.ext)
482                 && over(&l.generic_params, &r.generic_params, |l, r| eq_generic_param(l, r))
483                 && eq_fn_decl(&l.decl, &r.decl)
484         },
485         (Tup(l), Tup(r)) => over(l, r, |l, r| eq_ty(l, r)),
486         (Path(lq, lp), Path(rq, rp)) => both(lq, rq, |l, r| eq_qself(l, r)) && eq_path(lp, rp),
487         (TraitObject(lg, ls), TraitObject(rg, rs)) => ls == rs && over(lg, rg, |l, r| eq_generic_bound(l, r)),
488         (ImplTrait(_, lg), ImplTrait(_, rg)) => over(lg, rg, |l, r| eq_generic_bound(l, r)),
489         (Typeof(l), Typeof(r)) => eq_expr(&l.value, &r.value),
490         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
491         _ => false,
492     }
493 }
494
495 pub fn eq_ext(l: &Extern, r: &Extern) -> bool {
496     use Extern::*;
497     match (l, r) {
498         (None, None) | (Implicit, Implicit) => true,
499         (Explicit(l), Explicit(r)) => eq_str_lit(l, r),
500         _ => false,
501     }
502 }
503
504 pub fn eq_str_lit(l: &StrLit, r: &StrLit) -> bool {
505     l.style == r.style && l.symbol == r.symbol && l.suffix == r.suffix
506 }
507
508 pub fn eq_poly_ref_trait(l: &PolyTraitRef, r: &PolyTraitRef) -> bool {
509     eq_path(&l.trait_ref.path, &r.trait_ref.path)
510         && over(&l.bound_generic_params, &r.bound_generic_params, |l, r| {
511             eq_generic_param(l, r)
512         })
513 }
514
515 pub fn eq_generic_param(l: &GenericParam, r: &GenericParam) -> bool {
516     use GenericParamKind::*;
517     l.is_placeholder == r.is_placeholder
518         && eq_id(l.ident, r.ident)
519         && over(&l.bounds, &r.bounds, |l, r| eq_generic_bound(l, r))
520         && match (&l.kind, &r.kind) {
521             (Lifetime, Lifetime) => true,
522             (Type { default: l }, Type { default: r }) => both(l, r, |l, r| eq_ty(l, r)),
523             (
524                 Const {
525                     ty: lt,
526                     kw_span: _,
527                     default: ld,
528                 },
529                 Const {
530                     ty: rt,
531                     kw_span: _,
532                     default: rd,
533                 },
534             ) => eq_ty(lt, rt) && both(ld, rd, |ld, rd| eq_anon_const(ld, rd)),
535             _ => false,
536         }
537         && over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))
538 }
539
540 pub fn eq_generic_bound(l: &GenericBound, r: &GenericBound) -> bool {
541     use GenericBound::*;
542     match (l, r) {
543         (Trait(ptr1, tbm1), Trait(ptr2, tbm2)) => tbm1 == tbm2 && eq_poly_ref_trait(ptr1, ptr2),
544         (Outlives(l), Outlives(r)) => eq_id(l.ident, r.ident),
545         _ => false,
546     }
547 }
548
549 pub fn eq_assoc_constraint(l: &AssocTyConstraint, r: &AssocTyConstraint) -> bool {
550     use AssocTyConstraintKind::*;
551     eq_id(l.ident, r.ident)
552         && match (&l.kind, &r.kind) {
553             (Equality { ty: l }, Equality { ty: r }) => eq_ty(l, r),
554             (Bound { bounds: l }, Bound { bounds: r }) => over(l, r, |l, r| eq_generic_bound(l, r)),
555             _ => false,
556         }
557 }
558
559 pub fn eq_mac_call(l: &MacCall, r: &MacCall) -> bool {
560     eq_path(&l.path, &r.path) && eq_mac_args(&l.args, &r.args)
561 }
562
563 pub fn eq_attr(l: &Attribute, r: &Attribute) -> bool {
564     use AttrKind::*;
565     l.style == r.style
566         && match (&l.kind, &r.kind) {
567             (DocComment(l1, l2), DocComment(r1, r2)) => l1 == r1 && l2 == r2,
568             (Normal(l, _), Normal(r, _)) => eq_path(&l.path, &r.path) && eq_mac_args(&l.args, &r.args),
569             _ => false,
570         }
571 }
572
573 pub fn eq_mac_args(l: &MacArgs, r: &MacArgs) -> bool {
574     use MacArgs::*;
575     match (l, r) {
576         (Empty, Empty) => true,
577         (Delimited(_, ld, lts), Delimited(_, rd, rts)) => ld == rd && lts.eq_unspanned(rts),
578         (Eq(_, lt), Eq(_, rt)) => lt.kind == rt.kind,
579         _ => false,
580     }
581 }