]> git.lizzy.rs Git - rust.git/blob - clippy_utils/src/ast_utils.rs
Auto merge of #6601 - mdm:fix-unit-args-false-positive, r=camsteffen
[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 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)) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re),
233         (Const(ld, lt, le), Const(rd, rt, re)) => eq_defaultness(*ld, *rd) && eq_ty(lt, rt) && eq_expr_opt(le, re),
234         (Fn(box FnKind(ld, lf, lg, lb)), Fn(box FnKind(rd, rf, rg, rb))) => {
235             eq_defaultness(*ld, *rd) && eq_fn_sig(lf, rf) && eq_generics(lg, rg) && both(lb, rb, |l, r| eq_block(l, r))
236         },
237         (Mod(lu, lmk), Mod(ru, rmk)) => {
238             lu == ru
239                 && match (lmk, rmk) {
240                     (ModKind::Loaded(litems, linline, _), ModKind::Loaded(ritems, rinline, _)) => {
241                         linline == rinline && over(litems, ritems, |l, r| eq_item(l, r, eq_item_kind))
242                     },
243                     (ModKind::Unloaded, ModKind::Unloaded) => true,
244                     _ => false,
245                 }
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)) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re),
312         (Fn(box FnKind(ld, lf, lg, lb)), Fn(box FnKind(rd, rf, rg, rb))) => {
313             eq_defaultness(*ld, *rd) && eq_fn_sig(lf, rf) && eq_generics(lg, rg) && both(lb, rb, |l, r| eq_block(l, r))
314         },
315         (TyAlias(box TyAliasKind(ld, lg, lb, lt)), TyAlias(box TyAliasKind(rd, rg, rb, rt))) => {
316             eq_defaultness(*ld, *rd)
317                 && eq_generics(lg, rg)
318                 && over(lb, rb, |l, r| eq_generic_bound(l, r))
319                 && both(lt, rt, |l, r| eq_ty(l, r))
320         },
321         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
322         _ => false,
323     }
324 }
325
326 pub fn eq_assoc_item_kind(l: &AssocItemKind, r: &AssocItemKind) -> bool {
327     use AssocItemKind::*;
328     match (l, r) {
329         (Const(ld, lt, le), Const(rd, rt, re)) => eq_defaultness(*ld, *rd) && eq_ty(lt, rt) && eq_expr_opt(le, re),
330         (Fn(box FnKind(ld, lf, lg, lb)), Fn(box FnKind(rd, rf, rg, rb))) => {
331             eq_defaultness(*ld, *rd) && eq_fn_sig(lf, rf) && eq_generics(lg, rg) && both(lb, rb, |l, r| eq_block(l, r))
332         },
333         (TyAlias(box TyAliasKind(ld, lg, lb, lt)), TyAlias(box TyAliasKind(rd, rg, rb, rt))) => {
334             eq_defaultness(*ld, *rd)
335                 && eq_generics(lg, rg)
336                 && over(lb, rb, |l, r| eq_generic_bound(l, r))
337                 && both(lt, rt, |l, r| eq_ty(l, r))
338         },
339         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
340         _ => false,
341     }
342 }
343
344 pub fn eq_variant(l: &Variant, r: &Variant) -> bool {
345     l.is_placeholder == r.is_placeholder
346         && over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))
347         && eq_vis(&l.vis, &r.vis)
348         && eq_id(l.ident, r.ident)
349         && eq_variant_data(&l.data, &r.data)
350         && both(&l.disr_expr, &r.disr_expr, |l, r| eq_expr(&l.value, &r.value))
351 }
352
353 pub fn eq_variant_data(l: &VariantData, r: &VariantData) -> bool {
354     use VariantData::*;
355     match (l, r) {
356         (Unit(_), Unit(_)) => true,
357         (Struct(l, _), Struct(r, _)) | (Tuple(l, _), Tuple(r, _)) => over(l, r, |l, r| eq_struct_field(l, r)),
358         _ => false,
359     }
360 }
361
362 pub fn eq_struct_field(l: &StructField, r: &StructField) -> bool {
363     l.is_placeholder == r.is_placeholder
364         && over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))
365         && eq_vis(&l.vis, &r.vis)
366         && both(&l.ident, &r.ident, |l, r| eq_id(*l, *r))
367         && eq_ty(&l.ty, &r.ty)
368 }
369
370 pub fn eq_fn_sig(l: &FnSig, r: &FnSig) -> bool {
371     eq_fn_decl(&l.decl, &r.decl) && eq_fn_header(&l.header, &r.header)
372 }
373
374 pub fn eq_fn_header(l: &FnHeader, r: &FnHeader) -> bool {
375     matches!(l.unsafety, Unsafe::No) == matches!(r.unsafety, Unsafe::No)
376         && l.asyncness.is_async() == r.asyncness.is_async()
377         && matches!(l.constness, Const::No) == matches!(r.constness, Const::No)
378         && eq_ext(&l.ext, &r.ext)
379 }
380
381 pub fn eq_generics(l: &Generics, r: &Generics) -> bool {
382     over(&l.params, &r.params, |l, r| eq_generic_param(l, r))
383         && over(&l.where_clause.predicates, &r.where_clause.predicates, |l, r| {
384             eq_where_predicate(l, r)
385         })
386 }
387
388 pub fn eq_where_predicate(l: &WherePredicate, r: &WherePredicate) -> bool {
389     use WherePredicate::*;
390     match (l, r) {
391         (BoundPredicate(l), BoundPredicate(r)) => {
392             over(&l.bound_generic_params, &r.bound_generic_params, |l, r| {
393                 eq_generic_param(l, r)
394             }) && eq_ty(&l.bounded_ty, &r.bounded_ty)
395                 && over(&l.bounds, &r.bounds, |l, r| eq_generic_bound(l, r))
396         },
397         (RegionPredicate(l), RegionPredicate(r)) => {
398             eq_id(l.lifetime.ident, r.lifetime.ident) && over(&l.bounds, &r.bounds, |l, r| eq_generic_bound(l, r))
399         },
400         (EqPredicate(l), EqPredicate(r)) => eq_ty(&l.lhs_ty, &r.lhs_ty) && eq_ty(&l.rhs_ty, &r.rhs_ty),
401         _ => false,
402     }
403 }
404
405 pub fn eq_use_tree(l: &UseTree, r: &UseTree) -> bool {
406     eq_path(&l.prefix, &r.prefix) && eq_use_tree_kind(&l.kind, &r.kind)
407 }
408
409 pub fn eq_use_tree_kind(l: &UseTreeKind, r: &UseTreeKind) -> bool {
410     use UseTreeKind::*;
411     match (l, r) {
412         (Glob, Glob) => true,
413         (Simple(l, _, _), Simple(r, _, _)) => both(l, r, |l, r| eq_id(*l, *r)),
414         (Nested(l), Nested(r)) => over(l, r, |(l, _), (r, _)| eq_use_tree(l, r)),
415         _ => false,
416     }
417 }
418
419 pub fn eq_anon_const(l: &AnonConst, r: &AnonConst) -> bool {
420     eq_expr(&l.value, &r.value)
421 }
422
423 pub fn eq_defaultness(l: Defaultness, r: Defaultness) -> bool {
424     matches!(
425         (l, r),
426         (Defaultness::Final, Defaultness::Final) | (Defaultness::Default(_), Defaultness::Default(_))
427     )
428 }
429
430 pub fn eq_vis(l: &Visibility, r: &Visibility) -> bool {
431     use VisibilityKind::*;
432     match (&l.kind, &r.kind) {
433         (Public, Public) | (Inherited, Inherited) | (Crate(_), Crate(_)) => true,
434         (Restricted { path: l, .. }, Restricted { path: r, .. }) => eq_path(l, r),
435         _ => false,
436     }
437 }
438
439 pub fn eq_fn_decl(l: &FnDecl, r: &FnDecl) -> bool {
440     eq_fn_ret_ty(&l.output, &r.output)
441         && over(&l.inputs, &r.inputs, |l, r| {
442             l.is_placeholder == r.is_placeholder
443                 && eq_pat(&l.pat, &r.pat)
444                 && eq_ty(&l.ty, &r.ty)
445                 && over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))
446         })
447 }
448
449 pub fn eq_fn_ret_ty(l: &FnRetTy, r: &FnRetTy) -> bool {
450     match (l, r) {
451         (FnRetTy::Default(_), FnRetTy::Default(_)) => true,
452         (FnRetTy::Ty(l), FnRetTy::Ty(r)) => eq_ty(l, r),
453         _ => false,
454     }
455 }
456
457 pub fn eq_ty(l: &Ty, r: &Ty) -> bool {
458     use TyKind::*;
459     match (&l.kind, &r.kind) {
460         (Paren(l), _) => eq_ty(l, r),
461         (_, Paren(r)) => eq_ty(l, r),
462         (Never, Never) | (Infer, Infer) | (ImplicitSelf, ImplicitSelf) | (Err, Err) | (CVarArgs, CVarArgs) => true,
463         (Slice(l), Slice(r)) => eq_ty(l, r),
464         (Array(le, ls), Array(re, rs)) => eq_ty(le, re) && eq_expr(&ls.value, &rs.value),
465         (Ptr(l), Ptr(r)) => l.mutbl == r.mutbl && eq_ty(&l.ty, &r.ty),
466         (Rptr(ll, l), Rptr(rl, r)) => {
467             both(ll, rl, |l, r| eq_id(l.ident, r.ident)) && l.mutbl == r.mutbl && eq_ty(&l.ty, &r.ty)
468         },
469         (BareFn(l), BareFn(r)) => {
470             l.unsafety == r.unsafety
471                 && eq_ext(&l.ext, &r.ext)
472                 && over(&l.generic_params, &r.generic_params, |l, r| eq_generic_param(l, r))
473                 && eq_fn_decl(&l.decl, &r.decl)
474         },
475         (Tup(l), Tup(r)) => over(l, r, |l, r| eq_ty(l, r)),
476         (Path(lq, lp), Path(rq, rp)) => both(lq, rq, |l, r| eq_qself(l, r)) && eq_path(lp, rp),
477         (TraitObject(lg, ls), TraitObject(rg, rs)) => ls == rs && over(lg, rg, |l, r| eq_generic_bound(l, r)),
478         (ImplTrait(_, lg), ImplTrait(_, rg)) => over(lg, rg, |l, r| eq_generic_bound(l, r)),
479         (Typeof(l), Typeof(r)) => eq_expr(&l.value, &r.value),
480         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
481         _ => false,
482     }
483 }
484
485 pub fn eq_ext(l: &Extern, r: &Extern) -> bool {
486     use Extern::*;
487     match (l, r) {
488         (None, None) | (Implicit, Implicit) => true,
489         (Explicit(l), Explicit(r)) => eq_str_lit(l, r),
490         _ => false,
491     }
492 }
493
494 pub fn eq_str_lit(l: &StrLit, r: &StrLit) -> bool {
495     l.style == r.style && l.symbol == r.symbol && l.suffix == r.suffix
496 }
497
498 pub fn eq_poly_ref_trait(l: &PolyTraitRef, r: &PolyTraitRef) -> bool {
499     eq_path(&l.trait_ref.path, &r.trait_ref.path)
500         && over(&l.bound_generic_params, &r.bound_generic_params, |l, r| {
501             eq_generic_param(l, r)
502         })
503 }
504
505 pub fn eq_generic_param(l: &GenericParam, r: &GenericParam) -> bool {
506     use GenericParamKind::*;
507     l.is_placeholder == r.is_placeholder
508         && eq_id(l.ident, r.ident)
509         && over(&l.bounds, &r.bounds, |l, r| eq_generic_bound(l, r))
510         && match (&l.kind, &r.kind) {
511             (Lifetime, Lifetime) => true,
512             (Type { default: l }, Type { default: r }) => both(l, r, |l, r| eq_ty(l, r)),
513             (
514                 Const {
515                     ty: lt,
516                     kw_span: _,
517                     default: ld,
518                 },
519                 Const {
520                     ty: rt,
521                     kw_span: _,
522                     default: rd,
523                 },
524             ) => eq_ty(lt, rt) && both(ld, rd, |ld, rd| eq_anon_const(ld, rd)),
525             _ => false,
526         }
527         && over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))
528 }
529
530 pub fn eq_generic_bound(l: &GenericBound, r: &GenericBound) -> bool {
531     use GenericBound::*;
532     match (l, r) {
533         (Trait(ptr1, tbm1), Trait(ptr2, tbm2)) => tbm1 == tbm2 && eq_poly_ref_trait(ptr1, ptr2),
534         (Outlives(l), Outlives(r)) => eq_id(l.ident, r.ident),
535         _ => false,
536     }
537 }
538
539 pub fn eq_assoc_constraint(l: &AssocTyConstraint, r: &AssocTyConstraint) -> bool {
540     use AssocTyConstraintKind::*;
541     eq_id(l.ident, r.ident)
542         && match (&l.kind, &r.kind) {
543             (Equality { ty: l }, Equality { ty: r }) => eq_ty(l, r),
544             (Bound { bounds: l }, Bound { bounds: r }) => over(l, r, |l, r| eq_generic_bound(l, r)),
545             _ => false,
546         }
547 }
548
549 pub fn eq_mac_call(l: &MacCall, r: &MacCall) -> bool {
550     eq_path(&l.path, &r.path) && eq_mac_args(&l.args, &r.args)
551 }
552
553 pub fn eq_attr(l: &Attribute, r: &Attribute) -> bool {
554     use AttrKind::*;
555     l.style == r.style
556         && match (&l.kind, &r.kind) {
557             (DocComment(l1, l2), DocComment(r1, r2)) => l1 == r1 && l2 == r2,
558             (Normal(l, _), Normal(r, _)) => eq_path(&l.path, &r.path) && eq_mac_args(&l.args, &r.args),
559             _ => false,
560         }
561 }
562
563 pub fn eq_mac_args(l: &MacArgs, r: &MacArgs) -> bool {
564     use MacArgs::*;
565     match (l, r) {
566         (Empty, Empty) => true,
567         (Delimited(_, ld, lts), Delimited(_, rd, rts)) => ld == rd && lts.eq_unspanned(rts),
568         (Eq(_, lt), Eq(_, rt)) => lt.kind == rt.kind,
569         _ => false,
570     }
571 }