]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_lints/src/utils/ast_utils.rs
Auto merge of #80789 - Aaron1011:fix/stmt-empty, r=petrochenkov
[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)) => 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(ld, lf, lg, lb), Fn(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(l), Mod(r)) => l.inline == r.inline && over(&l.items, &r.items, |l, r| eq_item(l, r, eq_item_kind)),
238         (ForeignMod(l), ForeignMod(r)) => {
239             both(&l.abi, &r.abi, |l, r| eq_str_lit(l, r))
240                 && over(&l.items, &r.items, |l, r| eq_item(l, r, eq_foreign_item_kind))
241         },
242         (TyAlias(ld, lg, lb, lt), TyAlias(rd, rg, rb, rt)) => {
243             eq_defaultness(*ld, *rd)
244                 && eq_generics(lg, rg)
245                 && over(lb, rb, |l, r| eq_generic_bound(l, r))
246                 && both(lt, rt, |l, r| eq_ty(l, r))
247         },
248         (Enum(le, lg), Enum(re, rg)) => {
249             over(&le.variants, &re.variants, |l, r| eq_variant(l, r)) && eq_generics(lg, rg)
250         },
251         (Struct(lv, lg), Struct(rv, rg)) | (Union(lv, lg), Union(rv, rg)) => {
252             eq_variant_data(lv, rv) && eq_generics(lg, rg)
253         },
254         (Trait(la, lu, lg, lb, li), Trait(ra, ru, rg, rb, ri)) => {
255             la == ra
256                 && matches!(lu, Unsafe::No) == matches!(ru, Unsafe::No)
257                 && eq_generics(lg, rg)
258                 && over(lb, rb, |l, r| eq_generic_bound(l, r))
259                 && over(li, ri, |l, r| eq_item(l, r, eq_assoc_item_kind))
260         },
261         (TraitAlias(lg, lb), TraitAlias(rg, rb)) => eq_generics(lg, rg) && over(lb, rb, |l, r| eq_generic_bound(l, r)),
262         (
263             Impl {
264                 unsafety: lu,
265                 polarity: lp,
266                 defaultness: ld,
267                 constness: lc,
268                 generics: lg,
269                 of_trait: lot,
270                 self_ty: lst,
271                 items: li,
272             },
273             Impl {
274                 unsafety: ru,
275                 polarity: rp,
276                 defaultness: rd,
277                 constness: rc,
278                 generics: rg,
279                 of_trait: rot,
280                 self_ty: rst,
281                 items: ri,
282             },
283         ) => {
284             matches!(lu, Unsafe::No) == matches!(ru, Unsafe::No)
285                 && matches!(lp, ImplPolarity::Positive) == matches!(rp, ImplPolarity::Positive)
286                 && eq_defaultness(*ld, *rd)
287                 && matches!(lc, ast::Const::No) == matches!(rc, ast::Const::No)
288                 && eq_generics(lg, rg)
289                 && both(lot, rot, |l, r| eq_path(&l.path, &r.path))
290                 && eq_ty(lst, rst)
291                 && over(li, ri, |l, r| eq_item(l, r, eq_assoc_item_kind))
292         },
293         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
294         (MacroDef(l), MacroDef(r)) => l.macro_rules == r.macro_rules && eq_mac_args(&l.body, &r.body),
295         _ => false,
296     }
297 }
298
299 pub fn eq_foreign_item_kind(l: &ForeignItemKind, r: &ForeignItemKind) -> bool {
300     use ForeignItemKind::*;
301     match (l, r) {
302         (Static(lt, lm, le), Static(rt, rm, re)) => lm == rm && eq_ty(lt, rt) && eq_expr_opt(le, re),
303         (Fn(ld, lf, lg, lb), Fn(rd, rf, rg, rb)) => {
304             eq_defaultness(*ld, *rd) && eq_fn_sig(lf, rf) && eq_generics(lg, rg) && both(lb, rb, |l, r| eq_block(l, r))
305         },
306         (TyAlias(ld, lg, lb, lt), TyAlias(rd, rg, rb, rt)) => {
307             eq_defaultness(*ld, *rd)
308                 && eq_generics(lg, rg)
309                 && over(lb, rb, |l, r| eq_generic_bound(l, r))
310                 && both(lt, rt, |l, r| eq_ty(l, r))
311         },
312         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
313         _ => false,
314     }
315 }
316
317 pub fn eq_assoc_item_kind(l: &AssocItemKind, r: &AssocItemKind) -> bool {
318     use AssocItemKind::*;
319     match (l, r) {
320         (Const(ld, lt, le), Const(rd, rt, re)) => eq_defaultness(*ld, *rd) && eq_ty(lt, rt) && eq_expr_opt(le, re),
321         (Fn(ld, lf, lg, lb), Fn(rd, rf, rg, rb)) => {
322             eq_defaultness(*ld, *rd) && eq_fn_sig(lf, rf) && eq_generics(lg, rg) && both(lb, rb, |l, r| eq_block(l, r))
323         },
324         (TyAlias(ld, lg, lb, lt), TyAlias(rd, rg, rb, rt)) => {
325             eq_defaultness(*ld, *rd)
326                 && eq_generics(lg, rg)
327                 && over(lb, rb, |l, r| eq_generic_bound(l, r))
328                 && both(lt, rt, |l, r| eq_ty(l, r))
329         },
330         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
331         _ => false,
332     }
333 }
334
335 pub fn eq_variant(l: &Variant, r: &Variant) -> bool {
336     l.is_placeholder == r.is_placeholder
337         && over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))
338         && eq_vis(&l.vis, &r.vis)
339         && eq_id(l.ident, r.ident)
340         && eq_variant_data(&l.data, &r.data)
341         && both(&l.disr_expr, &r.disr_expr, |l, r| eq_expr(&l.value, &r.value))
342 }
343
344 pub fn eq_variant_data(l: &VariantData, r: &VariantData) -> bool {
345     use VariantData::*;
346     match (l, r) {
347         (Unit(_), Unit(_)) => true,
348         (Struct(l, _), Struct(r, _)) | (Tuple(l, _), Tuple(r, _)) => over(l, r, |l, r| eq_struct_field(l, r)),
349         _ => false,
350     }
351 }
352
353 pub fn eq_struct_field(l: &StructField, r: &StructField) -> bool {
354     l.is_placeholder == r.is_placeholder
355         && over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))
356         && eq_vis(&l.vis, &r.vis)
357         && both(&l.ident, &r.ident, |l, r| eq_id(*l, *r))
358         && eq_ty(&l.ty, &r.ty)
359 }
360
361 pub fn eq_fn_sig(l: &FnSig, r: &FnSig) -> bool {
362     eq_fn_decl(&l.decl, &r.decl) && eq_fn_header(&l.header, &r.header)
363 }
364
365 pub fn eq_fn_header(l: &FnHeader, r: &FnHeader) -> bool {
366     matches!(l.unsafety, Unsafe::No) == matches!(r.unsafety, Unsafe::No)
367         && l.asyncness.is_async() == r.asyncness.is_async()
368         && matches!(l.constness, Const::No) == matches!(r.constness, Const::No)
369         && eq_ext(&l.ext, &r.ext)
370 }
371
372 pub fn eq_generics(l: &Generics, r: &Generics) -> bool {
373     over(&l.params, &r.params, |l, r| eq_generic_param(l, r))
374         && over(&l.where_clause.predicates, &r.where_clause.predicates, |l, r| {
375             eq_where_predicate(l, r)
376         })
377 }
378
379 pub fn eq_where_predicate(l: &WherePredicate, r: &WherePredicate) -> bool {
380     use WherePredicate::*;
381     match (l, r) {
382         (BoundPredicate(l), BoundPredicate(r)) => {
383             over(&l.bound_generic_params, &r.bound_generic_params, |l, r| {
384                 eq_generic_param(l, r)
385             }) && eq_ty(&l.bounded_ty, &r.bounded_ty)
386                 && over(&l.bounds, &r.bounds, |l, r| eq_generic_bound(l, r))
387         },
388         (RegionPredicate(l), RegionPredicate(r)) => {
389             eq_id(l.lifetime.ident, r.lifetime.ident) && over(&l.bounds, &r.bounds, |l, r| eq_generic_bound(l, r))
390         },
391         (EqPredicate(l), EqPredicate(r)) => eq_ty(&l.lhs_ty, &r.lhs_ty) && eq_ty(&l.rhs_ty, &r.rhs_ty),
392         _ => false,
393     }
394 }
395
396 pub fn eq_use_tree(l: &UseTree, r: &UseTree) -> bool {
397     eq_path(&l.prefix, &r.prefix) && eq_use_tree_kind(&l.kind, &r.kind)
398 }
399
400 pub fn eq_use_tree_kind(l: &UseTreeKind, r: &UseTreeKind) -> bool {
401     use UseTreeKind::*;
402     match (l, r) {
403         (Glob, Glob) => true,
404         (Simple(l, _, _), Simple(r, _, _)) => both(l, r, |l, r| eq_id(*l, *r)),
405         (Nested(l), Nested(r)) => over(l, r, |(l, _), (r, _)| eq_use_tree(l, r)),
406         _ => false,
407     }
408 }
409
410 pub fn eq_anon_const(l: &AnonConst, r: &AnonConst) -> bool {
411     eq_expr(&l.value, &r.value)
412 }
413
414 pub fn eq_defaultness(l: Defaultness, r: Defaultness) -> bool {
415     matches!(
416         (l, r),
417         (Defaultness::Final, Defaultness::Final) | (Defaultness::Default(_), Defaultness::Default(_))
418     )
419 }
420
421 pub fn eq_vis(l: &Visibility, r: &Visibility) -> bool {
422     use VisibilityKind::*;
423     match (&l.kind, &r.kind) {
424         (Public, Public) | (Inherited, Inherited) | (Crate(_), Crate(_)) => true,
425         (Restricted { path: l, .. }, Restricted { path: r, .. }) => eq_path(l, r),
426         _ => false,
427     }
428 }
429
430 pub fn eq_fn_decl(l: &FnDecl, r: &FnDecl) -> bool {
431     eq_fn_ret_ty(&l.output, &r.output)
432         && over(&l.inputs, &r.inputs, |l, r| {
433             l.is_placeholder == r.is_placeholder
434                 && eq_pat(&l.pat, &r.pat)
435                 && eq_ty(&l.ty, &r.ty)
436                 && over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))
437         })
438 }
439
440 pub fn eq_fn_ret_ty(l: &FnRetTy, r: &FnRetTy) -> bool {
441     match (l, r) {
442         (FnRetTy::Default(_), FnRetTy::Default(_)) => true,
443         (FnRetTy::Ty(l), FnRetTy::Ty(r)) => eq_ty(l, r),
444         _ => false,
445     }
446 }
447
448 pub fn eq_ty(l: &Ty, r: &Ty) -> bool {
449     use TyKind::*;
450     match (&l.kind, &r.kind) {
451         (Paren(l), _) => eq_ty(l, r),
452         (_, Paren(r)) => eq_ty(l, r),
453         (Never, Never) | (Infer, Infer) | (ImplicitSelf, ImplicitSelf) | (Err, Err) | (CVarArgs, CVarArgs) => true,
454         (Slice(l), Slice(r)) => eq_ty(l, r),
455         (Array(le, ls), Array(re, rs)) => eq_ty(le, re) && eq_expr(&ls.value, &rs.value),
456         (Ptr(l), Ptr(r)) => l.mutbl == r.mutbl && eq_ty(&l.ty, &r.ty),
457         (Rptr(ll, l), Rptr(rl, r)) => {
458             both(ll, rl, |l, r| eq_id(l.ident, r.ident)) && l.mutbl == r.mutbl && eq_ty(&l.ty, &r.ty)
459         },
460         (BareFn(l), BareFn(r)) => {
461             l.unsafety == r.unsafety
462                 && eq_ext(&l.ext, &r.ext)
463                 && over(&l.generic_params, &r.generic_params, |l, r| eq_generic_param(l, r))
464                 && eq_fn_decl(&l.decl, &r.decl)
465         },
466         (Tup(l), Tup(r)) => over(l, r, |l, r| eq_ty(l, r)),
467         (Path(lq, lp), Path(rq, rp)) => both(lq, rq, |l, r| eq_qself(l, r)) && eq_path(lp, rp),
468         (TraitObject(lg, ls), TraitObject(rg, rs)) => ls == rs && over(lg, rg, |l, r| eq_generic_bound(l, r)),
469         (ImplTrait(_, lg), ImplTrait(_, rg)) => over(lg, rg, |l, r| eq_generic_bound(l, r)),
470         (Typeof(l), Typeof(r)) => eq_expr(&l.value, &r.value),
471         (MacCall(l), MacCall(r)) => eq_mac_call(l, r),
472         _ => false,
473     }
474 }
475
476 pub fn eq_ext(l: &Extern, r: &Extern) -> bool {
477     use Extern::*;
478     match (l, r) {
479         (None, None) | (Implicit, Implicit) => true,
480         (Explicit(l), Explicit(r)) => eq_str_lit(l, r),
481         _ => false,
482     }
483 }
484
485 pub fn eq_str_lit(l: &StrLit, r: &StrLit) -> bool {
486     l.style == r.style && l.symbol == r.symbol && l.suffix == r.suffix
487 }
488
489 pub fn eq_poly_ref_trait(l: &PolyTraitRef, r: &PolyTraitRef) -> bool {
490     eq_path(&l.trait_ref.path, &r.trait_ref.path)
491         && over(&l.bound_generic_params, &r.bound_generic_params, |l, r| {
492             eq_generic_param(l, r)
493         })
494 }
495
496 pub fn eq_generic_param(l: &GenericParam, r: &GenericParam) -> bool {
497     use GenericParamKind::*;
498     l.is_placeholder == r.is_placeholder
499         && eq_id(l.ident, r.ident)
500         && over(&l.bounds, &r.bounds, |l, r| eq_generic_bound(l, r))
501         && match (&l.kind, &r.kind) {
502             (Lifetime, Lifetime) => true,
503             (Type { default: l }, Type { default: r }) => both(l, r, |l, r| eq_ty(l, r)),
504             (
505                 Const {
506                     ty: lt,
507                     kw_span: _,
508                     default: ld,
509                 },
510                 Const {
511                     ty: rt,
512                     kw_span: _,
513                     default: rd,
514                 },
515             ) => eq_ty(lt, rt) && both(ld, rd, |ld, rd| eq_anon_const(ld, rd)),
516             _ => false,
517         }
518         && over(&l.attrs, &r.attrs, |l, r| eq_attr(l, r))
519 }
520
521 pub fn eq_generic_bound(l: &GenericBound, r: &GenericBound) -> bool {
522     use GenericBound::*;
523     match (l, r) {
524         (Trait(ptr1, tbm1), Trait(ptr2, tbm2)) => tbm1 == tbm2 && eq_poly_ref_trait(ptr1, ptr2),
525         (Outlives(l), Outlives(r)) => eq_id(l.ident, r.ident),
526         _ => false,
527     }
528 }
529
530 pub fn eq_assoc_constraint(l: &AssocTyConstraint, r: &AssocTyConstraint) -> bool {
531     use AssocTyConstraintKind::*;
532     eq_id(l.ident, r.ident)
533         && match (&l.kind, &r.kind) {
534             (Equality { ty: l }, Equality { ty: r }) => eq_ty(l, r),
535             (Bound { bounds: l }, Bound { bounds: r }) => over(l, r, |l, r| eq_generic_bound(l, r)),
536             _ => false,
537         }
538 }
539
540 pub fn eq_mac_call(l: &MacCall, r: &MacCall) -> bool {
541     eq_path(&l.path, &r.path) && eq_mac_args(&l.args, &r.args)
542 }
543
544 pub fn eq_attr(l: &Attribute, r: &Attribute) -> bool {
545     use AttrKind::*;
546     l.style == r.style
547         && match (&l.kind, &r.kind) {
548             (DocComment(l1, l2), DocComment(r1, r2)) => l1 == r1 && l2 == r2,
549             (Normal(l, _), Normal(r, _)) => eq_path(&l.path, &r.path) && eq_mac_args(&l.args, &r.args),
550             _ => false,
551         }
552 }
553
554 pub fn eq_mac_args(l: &MacArgs, r: &MacArgs) -> bool {
555     use MacArgs::*;
556     match (l, r) {
557         (Empty, Empty) => true,
558         (Delimited(_, ld, lts), Delimited(_, rd, rts)) => ld == rd && lts.eq_unspanned(rts),
559         (Eq(_, lt), Eq(_, rt)) => lt.kind == rt.kind,
560         _ => false,
561     }
562 }