• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use crate::consts::constant_simple;
2 use crate::macros::macro_backtrace;
3 use crate::source::{get_source_text, snippet_opt, walk_span_to_context, SpanRange};
4 use crate::tokenize_with_text;
5 use rustc_ast::ast::InlineAsmTemplatePiece;
6 use rustc_data_structures::fx::FxHasher;
7 use rustc_hir::def::Res;
8 use rustc_hir::HirIdMap;
9 use rustc_hir::{
10     ArrayLen, BinOpKind, BindingAnnotation, Block, BodyId, Closure, Expr, ExprField, ExprKind, FnRetTy, GenericArg,
11     GenericArgs, Guard, HirId, InlineAsmOperand, Let, Lifetime, LifetimeName, Pat, PatField, PatKind, Path,
12     PathSegment, PrimTy, QPath, Stmt, StmtKind, Ty, TyKind, TypeBinding,
13 };
14 use rustc_lexer::{tokenize, TokenKind};
15 use rustc_lint::LateContext;
16 use rustc_middle::ty::TypeckResults;
17 use rustc_span::{sym, BytePos, ExpnKind, MacroKind, Symbol, SyntaxContext};
18 use std::hash::{Hash, Hasher};
19 use std::ops::Range;
20 
21 /// Callback that is called when two expressions are not equal in the sense of `SpanlessEq`, but
22 /// other conditions would make them equal.
23 type SpanlessEqCallback<'a> = dyn FnMut(&Expr<'_>, &Expr<'_>) -> bool + 'a;
24 
25 /// Type used to check whether two ast are the same. This is different from the
26 /// operator `==` on ast types as this operator would compare true equality with
27 /// ID and span.
28 ///
29 /// Note that some expressions kinds are not considered but could be added.
30 pub struct SpanlessEq<'a, 'tcx> {
31     /// Context used to evaluate constant expressions.
32     cx: &'a LateContext<'tcx>,
33     maybe_typeck_results: Option<(&'tcx TypeckResults<'tcx>, &'tcx TypeckResults<'tcx>)>,
34     allow_side_effects: bool,
35     expr_fallback: Option<Box<SpanlessEqCallback<'a>>>,
36 }
37 
38 impl<'a, 'tcx> SpanlessEq<'a, 'tcx> {
new(cx: &'a LateContext<'tcx>) -> Self39     pub fn new(cx: &'a LateContext<'tcx>) -> Self {
40         Self {
41             cx,
42             maybe_typeck_results: cx.maybe_typeck_results().map(|x| (x, x)),
43             allow_side_effects: true,
44             expr_fallback: None,
45         }
46     }
47 
48     /// Consider expressions containing potential side effects as not equal.
49     #[must_use]
deny_side_effects(self) -> Self50     pub fn deny_side_effects(self) -> Self {
51         Self {
52             allow_side_effects: false,
53             ..self
54         }
55     }
56 
57     #[must_use]
expr_fallback(self, expr_fallback: impl FnMut(&Expr<'_>, &Expr<'_>) -> bool + 'a) -> Self58     pub fn expr_fallback(self, expr_fallback: impl FnMut(&Expr<'_>, &Expr<'_>) -> bool + 'a) -> Self {
59         Self {
60             expr_fallback: Some(Box::new(expr_fallback)),
61             ..self
62         }
63     }
64 
65     /// Use this method to wrap comparisons that may involve inter-expression context.
66     /// See `self.locals`.
inter_expr(&mut self) -> HirEqInterExpr<'_, 'a, 'tcx>67     pub fn inter_expr(&mut self) -> HirEqInterExpr<'_, 'a, 'tcx> {
68         HirEqInterExpr {
69             inner: self,
70             left_ctxt: SyntaxContext::root(),
71             right_ctxt: SyntaxContext::root(),
72             locals: HirIdMap::default(),
73         }
74     }
75 
eq_block(&mut self, left: &Block<'_>, right: &Block<'_>) -> bool76     pub fn eq_block(&mut self, left: &Block<'_>, right: &Block<'_>) -> bool {
77         self.inter_expr().eq_block(left, right)
78     }
79 
eq_expr(&mut self, left: &Expr<'_>, right: &Expr<'_>) -> bool80     pub fn eq_expr(&mut self, left: &Expr<'_>, right: &Expr<'_>) -> bool {
81         self.inter_expr().eq_expr(left, right)
82     }
83 
eq_path(&mut self, left: &Path<'_>, right: &Path<'_>) -> bool84     pub fn eq_path(&mut self, left: &Path<'_>, right: &Path<'_>) -> bool {
85         self.inter_expr().eq_path(left, right)
86     }
87 
eq_path_segment(&mut self, left: &PathSegment<'_>, right: &PathSegment<'_>) -> bool88     pub fn eq_path_segment(&mut self, left: &PathSegment<'_>, right: &PathSegment<'_>) -> bool {
89         self.inter_expr().eq_path_segment(left, right)
90     }
91 
eq_path_segments(&mut self, left: &[PathSegment<'_>], right: &[PathSegment<'_>]) -> bool92     pub fn eq_path_segments(&mut self, left: &[PathSegment<'_>], right: &[PathSegment<'_>]) -> bool {
93         self.inter_expr().eq_path_segments(left, right)
94     }
95 }
96 
97 pub struct HirEqInterExpr<'a, 'b, 'tcx> {
98     inner: &'a mut SpanlessEq<'b, 'tcx>,
99     left_ctxt: SyntaxContext,
100     right_ctxt: SyntaxContext,
101 
102     // When binding are declared, the binding ID in the left expression is mapped to the one on the
103     // right. For example, when comparing `{ let x = 1; x + 2 }` and `{ let y = 1; y + 2 }`,
104     // these blocks are considered equal since `x` is mapped to `y`.
105     pub locals: HirIdMap<HirId>,
106 }
107 
108 impl HirEqInterExpr<'_, '_, '_> {
eq_stmt(&mut self, left: &Stmt<'_>, right: &Stmt<'_>) -> bool109     pub fn eq_stmt(&mut self, left: &Stmt<'_>, right: &Stmt<'_>) -> bool {
110         match (&left.kind, &right.kind) {
111             (&StmtKind::Local(l), &StmtKind::Local(r)) => {
112                 // This additional check ensures that the type of the locals are equivalent even if the init
113                 // expression or type have some inferred parts.
114                 if let Some((typeck_lhs, typeck_rhs)) = self.inner.maybe_typeck_results {
115                     let l_ty = typeck_lhs.pat_ty(l.pat);
116                     let r_ty = typeck_rhs.pat_ty(r.pat);
117                     if l_ty != r_ty {
118                         return false;
119                     }
120                 }
121 
122                 // eq_pat adds the HirIds to the locals map. We therefore call it last to make sure that
123                 // these only get added if the init and type is equal.
124                 both(&l.init, &r.init, |l, r| self.eq_expr(l, r))
125                     && both(&l.ty, &r.ty, |l, r| self.eq_ty(l, r))
126                     && both(&l.els, &r.els, |l, r| self.eq_block(l, r))
127                     && self.eq_pat(l.pat, r.pat)
128             },
129             (&StmtKind::Expr(l), &StmtKind::Expr(r)) | (&StmtKind::Semi(l), &StmtKind::Semi(r)) => self.eq_expr(l, r),
130             _ => false,
131         }
132     }
133 
134     /// Checks whether two blocks are the same.
135     #[expect(clippy::similar_names)]
eq_block(&mut self, left: &Block<'_>, right: &Block<'_>) -> bool136     fn eq_block(&mut self, left: &Block<'_>, right: &Block<'_>) -> bool {
137         use TokenKind::{BlockComment, LineComment, Semi, Whitespace};
138         if left.stmts.len() != right.stmts.len() {
139             return false;
140         }
141         let lspan = left.span.data();
142         let rspan = right.span.data();
143         if lspan.ctxt != SyntaxContext::root() && rspan.ctxt != SyntaxContext::root() {
144             // Don't try to check in between statements inside macros.
145             return over(left.stmts, right.stmts, |left, right| self.eq_stmt(left, right))
146                 && both(&left.expr, &right.expr, |left, right| self.eq_expr(left, right));
147         }
148         if lspan.ctxt != rspan.ctxt {
149             return false;
150         }
151 
152         let mut lstart = lspan.lo;
153         let mut rstart = rspan.lo;
154 
155         for (left, right) in left.stmts.iter().zip(right.stmts) {
156             if !self.eq_stmt(left, right) {
157                 return false;
158             }
159 
160             // Try to detect any `cfg`ed statements or empty macro expansions.
161             let Some(lstmt_span) = walk_span_to_context(left.span, lspan.ctxt) else {
162                 return false;
163             };
164             let Some(rstmt_span) = walk_span_to_context(right.span, rspan.ctxt) else {
165                 return false;
166             };
167             let lstmt_span = lstmt_span.data();
168             let rstmt_span = rstmt_span.data();
169 
170             if lstmt_span.lo < lstart && rstmt_span.lo < rstart {
171                 // Can happen when macros expand to multiple statements, or rearrange statements.
172                 // Nothing in between the statements to check in this case.
173                 continue;
174             }
175             if lstmt_span.lo < lstart || rstmt_span.lo < rstart {
176                 // Only one of the blocks had a weird macro.
177                 return false;
178             }
179             if !eq_span_tokens(self.inner.cx, lstart..lstmt_span.lo, rstart..rstmt_span.lo, |t| {
180                 !matches!(t, Whitespace | LineComment { .. } | BlockComment { .. } | Semi)
181             }) {
182                 return false;
183             }
184 
185             lstart = lstmt_span.hi;
186             rstart = rstmt_span.hi;
187         }
188 
189         let (lend, rend) = match (left.expr, right.expr) {
190             (Some(left), Some(right)) => {
191                 if !self.eq_expr(left, right) {
192                     return false;
193                 }
194                 let Some(lexpr_span) = walk_span_to_context(left.span, lspan.ctxt) else {
195                     return false;
196                 };
197                 let Some(rexpr_span) = walk_span_to_context(right.span, rspan.ctxt) else {
198                     return false;
199                 };
200                 (lexpr_span.lo(), rexpr_span.lo())
201             },
202             (None, None) => (lspan.hi, rspan.hi),
203             (Some(_), None) | (None, Some(_)) => return false,
204         };
205 
206         if lend < lstart && rend < rstart {
207             // Can happen when macros rearrange the input.
208             // Nothing in between the statements to check in this case.
209             return true;
210         } else if lend < lstart || rend < rstart {
211             // Only one of the blocks had a weird macro
212             return false;
213         }
214         eq_span_tokens(self.inner.cx, lstart..lend, rstart..rend, |t| {
215             !matches!(t, Whitespace | LineComment { .. } | BlockComment { .. } | Semi)
216         })
217     }
218 
should_ignore(&mut self, expr: &Expr<'_>) -> bool219     fn should_ignore(&mut self, expr: &Expr<'_>) -> bool {
220         macro_backtrace(expr.span).last().map_or(false, |macro_call| {
221             matches!(
222                 &self.inner.cx.tcx.get_diagnostic_name(macro_call.def_id),
223                 Some(sym::todo_macro | sym::unimplemented_macro)
224             )
225         })
226     }
227 
eq_array_length(&mut self, left: ArrayLen, right: ArrayLen) -> bool228     pub fn eq_array_length(&mut self, left: ArrayLen, right: ArrayLen) -> bool {
229         match (left, right) {
230             (ArrayLen::Infer(..), ArrayLen::Infer(..)) => true,
231             (ArrayLen::Body(l_ct), ArrayLen::Body(r_ct)) => self.eq_body(l_ct.body, r_ct.body),
232             (_, _) => false,
233         }
234     }
235 
eq_body(&mut self, left: BodyId, right: BodyId) -> bool236     pub fn eq_body(&mut self, left: BodyId, right: BodyId) -> bool {
237         // swap out TypeckResults when hashing a body
238         let old_maybe_typeck_results = self.inner.maybe_typeck_results.replace((
239             self.inner.cx.tcx.typeck_body(left),
240             self.inner.cx.tcx.typeck_body(right),
241         ));
242         let res = self.eq_expr(
243             self.inner.cx.tcx.hir().body(left).value,
244             self.inner.cx.tcx.hir().body(right).value,
245         );
246         self.inner.maybe_typeck_results = old_maybe_typeck_results;
247         res
248     }
249 
250     #[expect(clippy::similar_names)]
eq_expr(&mut self, left: &Expr<'_>, right: &Expr<'_>) -> bool251     pub fn eq_expr(&mut self, left: &Expr<'_>, right: &Expr<'_>) -> bool {
252         if !self.check_ctxt(left.span.ctxt(), right.span.ctxt()) {
253             return false;
254         }
255 
256         if let Some((typeck_lhs, typeck_rhs)) = self.inner.maybe_typeck_results {
257             if let (Some(l), Some(r)) = (
258                 constant_simple(self.inner.cx, typeck_lhs, left),
259                 constant_simple(self.inner.cx, typeck_rhs, right),
260             ) {
261                 if l == r {
262                     return true;
263                 }
264             }
265         }
266 
267         let is_eq = match (
268             reduce_exprkind(self.inner.cx, &left.kind),
269             reduce_exprkind(self.inner.cx, &right.kind),
270         ) {
271             (&ExprKind::AddrOf(lb, l_mut, le), &ExprKind::AddrOf(rb, r_mut, re)) => {
272                 lb == rb && l_mut == r_mut && self.eq_expr(le, re)
273             },
274             (&ExprKind::Continue(li), &ExprKind::Continue(ri)) => {
275                 both(&li.label, &ri.label, |l, r| l.ident.name == r.ident.name)
276             },
277             (&ExprKind::Assign(ll, lr, _), &ExprKind::Assign(rl, rr, _)) => {
278                 self.inner.allow_side_effects && self.eq_expr(ll, rl) && self.eq_expr(lr, rr)
279             },
280             (&ExprKind::AssignOp(ref lo, ll, lr), &ExprKind::AssignOp(ref ro, rl, rr)) => {
281                 self.inner.allow_side_effects && lo.node == ro.node && self.eq_expr(ll, rl) && self.eq_expr(lr, rr)
282             },
283             (&ExprKind::Block(l, _), &ExprKind::Block(r, _)) => self.eq_block(l, r),
284             (&ExprKind::Binary(l_op, ll, lr), &ExprKind::Binary(r_op, rl, rr)) => {
285                 l_op.node == r_op.node && self.eq_expr(ll, rl) && self.eq_expr(lr, rr)
286                     || swap_binop(l_op.node, ll, lr).map_or(false, |(l_op, ll, lr)| {
287                         l_op == r_op.node && self.eq_expr(ll, rl) && self.eq_expr(lr, rr)
288                     })
289             },
290             (&ExprKind::Break(li, ref le), &ExprKind::Break(ri, ref re)) => {
291                 both(&li.label, &ri.label, |l, r| l.ident.name == r.ident.name)
292                     && both(le, re, |l, r| self.eq_expr(l, r))
293             },
294             (&ExprKind::Call(l_fun, l_args), &ExprKind::Call(r_fun, r_args)) => {
295                 self.inner.allow_side_effects && self.eq_expr(l_fun, r_fun) && self.eq_exprs(l_args, r_args)
296             },
297             (&ExprKind::Cast(lx, lt), &ExprKind::Cast(rx, rt)) | (&ExprKind::Type(lx, lt), &ExprKind::Type(rx, rt)) => {
298                 self.eq_expr(lx, rx) && self.eq_ty(lt, rt)
299             },
300             (&ExprKind::Field(l_f_exp, ref l_f_ident), &ExprKind::Field(r_f_exp, ref r_f_ident)) => {
301                 l_f_ident.name == r_f_ident.name && self.eq_expr(l_f_exp, r_f_exp)
302             },
303             (&ExprKind::Index(la, li), &ExprKind::Index(ra, ri)) => self.eq_expr(la, ra) && self.eq_expr(li, ri),
304             (&ExprKind::If(lc, lt, ref le), &ExprKind::If(rc, rt, ref re)) => {
305                 self.eq_expr(lc, rc) && self.eq_expr(lt, rt) && both(le, re, |l, r| self.eq_expr(l, r))
306             },
307             (&ExprKind::Let(l), &ExprKind::Let(r)) => {
308                 self.eq_pat(l.pat, r.pat) && both(&l.ty, &r.ty, |l, r| self.eq_ty(l, r)) && self.eq_expr(l.init, r.init)
309             },
310             (ExprKind::Lit(l), ExprKind::Lit(r)) => l.node == r.node,
311             (&ExprKind::Loop(lb, ref ll, ref lls, _), &ExprKind::Loop(rb, ref rl, ref rls, _)) => {
312                 lls == rls && self.eq_block(lb, rb) && both(ll, rl, |l, r| l.ident.name == r.ident.name)
313             },
314             (&ExprKind::Match(le, la, ref ls), &ExprKind::Match(re, ra, ref rs)) => {
315                 ls == rs
316                     && self.eq_expr(le, re)
317                     && over(la, ra, |l, r| {
318                         self.eq_pat(l.pat, r.pat)
319                             && both(&l.guard, &r.guard, |l, r| self.eq_guard(l, r))
320                             && self.eq_expr(l.body, r.body)
321                     })
322             },
323             (
324                 &ExprKind::MethodCall(l_path, l_receiver, l_args, _),
325                 &ExprKind::MethodCall(r_path, r_receiver, r_args, _),
326             ) => {
327                 self.inner.allow_side_effects
328                     && self.eq_path_segment(l_path, r_path)
329                     && self.eq_expr(l_receiver, r_receiver)
330                     && self.eq_exprs(l_args, r_args)
331             },
332             (&ExprKind::Repeat(le, ll), &ExprKind::Repeat(re, rl)) => {
333                 self.eq_expr(le, re) && self.eq_array_length(ll, rl)
334             },
335             (ExprKind::Ret(l), ExprKind::Ret(r)) => both(l, r, |l, r| self.eq_expr(l, r)),
336             (ExprKind::Path(l), ExprKind::Path(r)) => self.eq_qpath(l, r),
337             (&ExprKind::Struct(l_path, lf, ref lo), &ExprKind::Struct(r_path, rf, ref ro)) => {
338                 self.eq_qpath(l_path, r_path)
339                     && both(lo, ro, |l, r| self.eq_expr(l, r))
340                     && over(lf, rf, |l, r| self.eq_expr_field(l, r))
341             },
342             (&ExprKind::Tup(l_tup), &ExprKind::Tup(r_tup)) => self.eq_exprs(l_tup, r_tup),
343             (&ExprKind::Unary(l_op, le), &ExprKind::Unary(r_op, re)) => l_op == r_op && self.eq_expr(le, re),
344             (&ExprKind::Array(l), &ExprKind::Array(r)) => self.eq_exprs(l, r),
345             (&ExprKind::DropTemps(le), &ExprKind::DropTemps(re)) => self.eq_expr(le, re),
346             (&ExprKind::OffsetOf(l_container, l_fields), &ExprKind::OffsetOf(r_container, r_fields)) => {
347                 self.eq_ty(l_container, r_container) && over(l_fields, r_fields, |l, r| l.name == r.name)
348             },
349             _ => false,
350         };
351         (is_eq && (!self.should_ignore(left) || !self.should_ignore(right)))
352             || self.inner.expr_fallback.as_mut().map_or(false, |f| f(left, right))
353     }
354 
eq_exprs(&mut self, left: &[Expr<'_>], right: &[Expr<'_>]) -> bool355     fn eq_exprs(&mut self, left: &[Expr<'_>], right: &[Expr<'_>]) -> bool {
356         over(left, right, |l, r| self.eq_expr(l, r))
357     }
358 
eq_expr_field(&mut self, left: &ExprField<'_>, right: &ExprField<'_>) -> bool359     fn eq_expr_field(&mut self, left: &ExprField<'_>, right: &ExprField<'_>) -> bool {
360         left.ident.name == right.ident.name && self.eq_expr(left.expr, right.expr)
361     }
362 
eq_guard(&mut self, left: &Guard<'_>, right: &Guard<'_>) -> bool363     fn eq_guard(&mut self, left: &Guard<'_>, right: &Guard<'_>) -> bool {
364         match (left, right) {
365             (Guard::If(l), Guard::If(r)) => self.eq_expr(l, r),
366             (Guard::IfLet(l), Guard::IfLet(r)) => {
367                 self.eq_pat(l.pat, r.pat) && both(&l.ty, &r.ty, |l, r| self.eq_ty(l, r)) && self.eq_expr(l.init, r.init)
368             },
369             _ => false,
370         }
371     }
372 
eq_generic_arg(&mut self, left: &GenericArg<'_>, right: &GenericArg<'_>) -> bool373     fn eq_generic_arg(&mut self, left: &GenericArg<'_>, right: &GenericArg<'_>) -> bool {
374         match (left, right) {
375             (GenericArg::Const(l), GenericArg::Const(r)) => self.eq_body(l.value.body, r.value.body),
376             (GenericArg::Lifetime(l_lt), GenericArg::Lifetime(r_lt)) => Self::eq_lifetime(l_lt, r_lt),
377             (GenericArg::Type(l_ty), GenericArg::Type(r_ty)) => self.eq_ty(l_ty, r_ty),
378             (GenericArg::Infer(l_inf), GenericArg::Infer(r_inf)) => self.eq_ty(&l_inf.to_ty(), &r_inf.to_ty()),
379             _ => false,
380         }
381     }
382 
eq_lifetime(left: &Lifetime, right: &Lifetime) -> bool383     fn eq_lifetime(left: &Lifetime, right: &Lifetime) -> bool {
384         left.res == right.res
385     }
386 
eq_pat_field(&mut self, left: &PatField<'_>, right: &PatField<'_>) -> bool387     fn eq_pat_field(&mut self, left: &PatField<'_>, right: &PatField<'_>) -> bool {
388         let (PatField { ident: li, pat: lp, .. }, PatField { ident: ri, pat: rp, .. }) = (&left, &right);
389         li.name == ri.name && self.eq_pat(lp, rp)
390     }
391 
392     /// Checks whether two patterns are the same.
eq_pat(&mut self, left: &Pat<'_>, right: &Pat<'_>) -> bool393     fn eq_pat(&mut self, left: &Pat<'_>, right: &Pat<'_>) -> bool {
394         match (&left.kind, &right.kind) {
395             (&PatKind::Box(l), &PatKind::Box(r)) => self.eq_pat(l, r),
396             (&PatKind::Struct(ref lp, la, ..), &PatKind::Struct(ref rp, ra, ..)) => {
397                 self.eq_qpath(lp, rp) && over(la, ra, |l, r| self.eq_pat_field(l, r))
398             },
399             (&PatKind::TupleStruct(ref lp, la, ls), &PatKind::TupleStruct(ref rp, ra, rs)) => {
400                 self.eq_qpath(lp, rp) && over(la, ra, |l, r| self.eq_pat(l, r)) && ls == rs
401             },
402             (&PatKind::Binding(lb, li, _, ref lp), &PatKind::Binding(rb, ri, _, ref rp)) => {
403                 let eq = lb == rb && both(lp, rp, |l, r| self.eq_pat(l, r));
404                 if eq {
405                     self.locals.insert(li, ri);
406                 }
407                 eq
408             },
409             (PatKind::Path(l), PatKind::Path(r)) => self.eq_qpath(l, r),
410             (&PatKind::Lit(l), &PatKind::Lit(r)) => self.eq_expr(l, r),
411             (&PatKind::Tuple(l, ls), &PatKind::Tuple(r, rs)) => ls == rs && over(l, r, |l, r| self.eq_pat(l, r)),
412             (&PatKind::Range(ref ls, ref le, li), &PatKind::Range(ref rs, ref re, ri)) => {
413                 both(ls, rs, |a, b| self.eq_expr(a, b)) && both(le, re, |a, b| self.eq_expr(a, b)) && (li == ri)
414             },
415             (&PatKind::Ref(le, ref lm), &PatKind::Ref(re, ref rm)) => lm == rm && self.eq_pat(le, re),
416             (&PatKind::Slice(ls, ref li, le), &PatKind::Slice(rs, ref ri, re)) => {
417                 over(ls, rs, |l, r| self.eq_pat(l, r))
418                     && over(le, re, |l, r| self.eq_pat(l, r))
419                     && both(li, ri, |l, r| self.eq_pat(l, r))
420             },
421             (&PatKind::Wild, &PatKind::Wild) => true,
422             _ => false,
423         }
424     }
425 
426     #[expect(clippy::similar_names)]
eq_qpath(&mut self, left: &QPath<'_>, right: &QPath<'_>) -> bool427     fn eq_qpath(&mut self, left: &QPath<'_>, right: &QPath<'_>) -> bool {
428         match (left, right) {
429             (&QPath::Resolved(ref lty, lpath), &QPath::Resolved(ref rty, rpath)) => {
430                 both(lty, rty, |l, r| self.eq_ty(l, r)) && self.eq_path(lpath, rpath)
431             },
432             (&QPath::TypeRelative(lty, lseg), &QPath::TypeRelative(rty, rseg)) => {
433                 self.eq_ty(lty, rty) && self.eq_path_segment(lseg, rseg)
434             },
435             (&QPath::LangItem(llang_item, ..), &QPath::LangItem(rlang_item, ..)) => llang_item == rlang_item,
436             _ => false,
437         }
438     }
439 
eq_path(&mut self, left: &Path<'_>, right: &Path<'_>) -> bool440     pub fn eq_path(&mut self, left: &Path<'_>, right: &Path<'_>) -> bool {
441         match (left.res, right.res) {
442             (Res::Local(l), Res::Local(r)) => l == r || self.locals.get(&l) == Some(&r),
443             (Res::Local(_), _) | (_, Res::Local(_)) => false,
444             _ => over(left.segments, right.segments, |l, r| self.eq_path_segment(l, r)),
445         }
446     }
447 
eq_path_parameters(&mut self, left: &GenericArgs<'_>, right: &GenericArgs<'_>) -> bool448     fn eq_path_parameters(&mut self, left: &GenericArgs<'_>, right: &GenericArgs<'_>) -> bool {
449         if left.parenthesized == right.parenthesized {
450             over(left.args, right.args, |l, r| self.eq_generic_arg(l, r)) // FIXME(flip1995): may not work
451                 && over(left.bindings, right.bindings, |l, r| self.eq_type_binding(l, r))
452         } else {
453             false
454         }
455     }
456 
eq_path_segments(&mut self, left: &[PathSegment<'_>], right: &[PathSegment<'_>]) -> bool457     pub fn eq_path_segments(&mut self, left: &[PathSegment<'_>], right: &[PathSegment<'_>]) -> bool {
458         left.len() == right.len() && left.iter().zip(right).all(|(l, r)| self.eq_path_segment(l, r))
459     }
460 
eq_path_segment(&mut self, left: &PathSegment<'_>, right: &PathSegment<'_>) -> bool461     pub fn eq_path_segment(&mut self, left: &PathSegment<'_>, right: &PathSegment<'_>) -> bool {
462         // The == of idents doesn't work with different contexts,
463         // we have to be explicit about hygiene
464         left.ident.name == right.ident.name && both(&left.args, &right.args, |l, r| self.eq_path_parameters(l, r))
465     }
466 
eq_ty(&mut self, left: &Ty<'_>, right: &Ty<'_>) -> bool467     pub fn eq_ty(&mut self, left: &Ty<'_>, right: &Ty<'_>) -> bool {
468         match (&left.kind, &right.kind) {
469             (&TyKind::Slice(l_vec), &TyKind::Slice(r_vec)) => self.eq_ty(l_vec, r_vec),
470             (&TyKind::Array(lt, ll), &TyKind::Array(rt, rl)) => self.eq_ty(lt, rt) && self.eq_array_length(ll, rl),
471             (TyKind::Ptr(l_mut), TyKind::Ptr(r_mut)) => l_mut.mutbl == r_mut.mutbl && self.eq_ty(l_mut.ty, r_mut.ty),
472             (TyKind::Ref(_, l_rmut), TyKind::Ref(_, r_rmut)) => {
473                 l_rmut.mutbl == r_rmut.mutbl && self.eq_ty(l_rmut.ty, r_rmut.ty)
474             },
475             (TyKind::Path(l), TyKind::Path(r)) => self.eq_qpath(l, r),
476             (&TyKind::Tup(l), &TyKind::Tup(r)) => over(l, r, |l, r| self.eq_ty(l, r)),
477             (&TyKind::Infer, &TyKind::Infer) => true,
478             _ => false,
479         }
480     }
481 
eq_type_binding(&mut self, left: &TypeBinding<'_>, right: &TypeBinding<'_>) -> bool482     fn eq_type_binding(&mut self, left: &TypeBinding<'_>, right: &TypeBinding<'_>) -> bool {
483         left.ident.name == right.ident.name && self.eq_ty(left.ty(), right.ty())
484     }
485 
check_ctxt(&mut self, left: SyntaxContext, right: SyntaxContext) -> bool486     fn check_ctxt(&mut self, left: SyntaxContext, right: SyntaxContext) -> bool {
487         if self.left_ctxt == left && self.right_ctxt == right {
488             return true;
489         } else if self.left_ctxt == left || self.right_ctxt == right {
490             // Only one context has changed. This can only happen if the two nodes are written differently.
491             return false;
492         } else if left != SyntaxContext::root() {
493             let mut left_data = left.outer_expn_data();
494             let mut right_data = right.outer_expn_data();
495             loop {
496                 use TokenKind::{BlockComment, LineComment, Whitespace};
497                 if left_data.macro_def_id != right_data.macro_def_id
498                     || (matches!(left_data.kind, ExpnKind::Macro(MacroKind::Bang, name) if name == sym::cfg)
499                         && !eq_span_tokens(self.inner.cx, left_data.call_site, right_data.call_site, |t| {
500                             !matches!(t, Whitespace | LineComment { .. } | BlockComment { .. })
501                         }))
502                 {
503                     // Either a different chain of macro calls, or different arguments to the `cfg` macro.
504                     return false;
505                 }
506                 let left_ctxt = left_data.call_site.ctxt();
507                 let right_ctxt = right_data.call_site.ctxt();
508                 if left_ctxt == SyntaxContext::root() && right_ctxt == SyntaxContext::root() {
509                     break;
510                 }
511                 if left_ctxt == SyntaxContext::root() || right_ctxt == SyntaxContext::root() {
512                     // Different lengths for the expansion stack. This can only happen if nodes are written differently,
513                     // or shouldn't be compared to start with.
514                     return false;
515                 }
516                 left_data = left_ctxt.outer_expn_data();
517                 right_data = right_ctxt.outer_expn_data();
518             }
519         }
520         self.left_ctxt = left;
521         self.right_ctxt = right;
522         true
523     }
524 }
525 
526 /// Some simple reductions like `{ return }` => `return`
reduce_exprkind<'hir>(cx: &LateContext<'_>, kind: &'hir ExprKind<'hir>) -> &'hir ExprKind<'hir>527 fn reduce_exprkind<'hir>(cx: &LateContext<'_>, kind: &'hir ExprKind<'hir>) -> &'hir ExprKind<'hir> {
528     if let ExprKind::Block(block, _) = kind {
529         match (block.stmts, block.expr) {
530             // From an `if let` expression without an `else` block. The arm for the implicit wild pattern is an empty
531             // block with an empty span.
532             ([], None) if block.span.is_empty() => &ExprKind::Tup(&[]),
533             // `{}` => `()`
534             ([], None) => match snippet_opt(cx, block.span) {
535                 // Don't reduce if there are any tokens contained in the braces
536                 Some(snip)
537                     if tokenize(&snip)
538                         .map(|t| t.kind)
539                         .filter(|t| {
540                             !matches!(
541                                 t,
542                                 TokenKind::LineComment { .. } | TokenKind::BlockComment { .. } | TokenKind::Whitespace
543                             )
544                         })
545                         .ne([TokenKind::OpenBrace, TokenKind::CloseBrace].iter().copied()) =>
546                 {
547                     kind
548                 },
549                 _ => &ExprKind::Tup(&[]),
550             },
551             ([], Some(expr)) => match expr.kind {
552                 // `{ return .. }` => `return ..`
553                 ExprKind::Ret(..) => &expr.kind,
554                 _ => kind,
555             },
556             ([stmt], None) => match stmt.kind {
557                 StmtKind::Expr(expr) | StmtKind::Semi(expr) => match expr.kind {
558                     // `{ return ..; }` => `return ..`
559                     ExprKind::Ret(..) => &expr.kind,
560                     _ => kind,
561                 },
562                 _ => kind,
563             },
564             _ => kind,
565         }
566     } else {
567         kind
568     }
569 }
570 
swap_binop<'a>( binop: BinOpKind, lhs: &'a Expr<'a>, rhs: &'a Expr<'a>, ) -> Option<(BinOpKind, &'a Expr<'a>, &'a Expr<'a>)>571 fn swap_binop<'a>(
572     binop: BinOpKind,
573     lhs: &'a Expr<'a>,
574     rhs: &'a Expr<'a>,
575 ) -> Option<(BinOpKind, &'a Expr<'a>, &'a Expr<'a>)> {
576     match binop {
577         BinOpKind::Add | BinOpKind::Eq | BinOpKind::Ne | BinOpKind::BitAnd | BinOpKind::BitXor | BinOpKind::BitOr => {
578             Some((binop, rhs, lhs))
579         },
580         BinOpKind::Lt => Some((BinOpKind::Gt, rhs, lhs)),
581         BinOpKind::Le => Some((BinOpKind::Ge, rhs, lhs)),
582         BinOpKind::Ge => Some((BinOpKind::Le, rhs, lhs)),
583         BinOpKind::Gt => Some((BinOpKind::Lt, rhs, lhs)),
584         BinOpKind::Mul // Not always commutative, e.g. with matrices. See issue #5698
585         | BinOpKind::Shl
586         | BinOpKind::Shr
587         | BinOpKind::Rem
588         | BinOpKind::Sub
589         | BinOpKind::Div
590         | BinOpKind::And
591         | BinOpKind::Or => None,
592     }
593 }
594 
595 /// Checks if the two `Option`s are both `None` or some equal values as per
596 /// `eq_fn`.
both<X>(l: &Option<X>, r: &Option<X>, mut eq_fn: impl FnMut(&X, &X) -> bool) -> bool597 pub fn both<X>(l: &Option<X>, r: &Option<X>, mut eq_fn: impl FnMut(&X, &X) -> bool) -> bool {
598     l.as_ref()
599         .map_or_else(|| r.is_none(), |x| r.as_ref().map_or(false, |y| eq_fn(x, y)))
600 }
601 
602 /// Checks if two slices are equal as per `eq_fn`.
over<X>(left: &[X], right: &[X], mut eq_fn: impl FnMut(&X, &X) -> bool) -> bool603 pub fn over<X>(left: &[X], right: &[X], mut eq_fn: impl FnMut(&X, &X) -> bool) -> bool {
604     left.len() == right.len() && left.iter().zip(right).all(|(x, y)| eq_fn(x, y))
605 }
606 
607 /// Counts how many elements of the slices are equal as per `eq_fn`.
count_eq<X: Sized>( left: &mut dyn Iterator<Item = X>, right: &mut dyn Iterator<Item = X>, mut eq_fn: impl FnMut(&X, &X) -> bool, ) -> usize608 pub fn count_eq<X: Sized>(
609     left: &mut dyn Iterator<Item = X>,
610     right: &mut dyn Iterator<Item = X>,
611     mut eq_fn: impl FnMut(&X, &X) -> bool,
612 ) -> usize {
613     left.zip(right).take_while(|(l, r)| eq_fn(l, r)).count()
614 }
615 
616 /// Checks if two expressions evaluate to the same value, and don't contain any side effects.
eq_expr_value(cx: &LateContext<'_>, left: &Expr<'_>, right: &Expr<'_>) -> bool617 pub fn eq_expr_value(cx: &LateContext<'_>, left: &Expr<'_>, right: &Expr<'_>) -> bool {
618     SpanlessEq::new(cx).deny_side_effects().eq_expr(left, right)
619 }
620 
621 /// Type used to hash an ast element. This is different from the `Hash` trait
622 /// on ast types as this
623 /// trait would consider IDs and spans.
624 ///
625 /// All expressions kind are hashed, but some might have a weaker hash.
626 pub struct SpanlessHash<'a, 'tcx> {
627     /// Context used to evaluate constant expressions.
628     cx: &'a LateContext<'tcx>,
629     maybe_typeck_results: Option<&'tcx TypeckResults<'tcx>>,
630     s: FxHasher,
631 }
632 
633 impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
new(cx: &'a LateContext<'tcx>) -> Self634     pub fn new(cx: &'a LateContext<'tcx>) -> Self {
635         Self {
636             cx,
637             maybe_typeck_results: cx.maybe_typeck_results(),
638             s: FxHasher::default(),
639         }
640     }
641 
finish(self) -> u64642     pub fn finish(self) -> u64 {
643         self.s.finish()
644     }
645 
hash_block(&mut self, b: &Block<'_>)646     pub fn hash_block(&mut self, b: &Block<'_>) {
647         for s in b.stmts {
648             self.hash_stmt(s);
649         }
650 
651         if let Some(e) = b.expr {
652             self.hash_expr(e);
653         }
654 
655         std::mem::discriminant(&b.rules).hash(&mut self.s);
656     }
657 
658     #[expect(clippy::too_many_lines)]
hash_expr(&mut self, e: &Expr<'_>)659     pub fn hash_expr(&mut self, e: &Expr<'_>) {
660         let simple_const = self
661             .maybe_typeck_results
662             .and_then(|typeck_results| constant_simple(self.cx, typeck_results, e));
663 
664         // const hashing may result in the same hash as some unrelated node, so add a sort of
665         // discriminant depending on which path we're choosing next
666         simple_const.hash(&mut self.s);
667         if simple_const.is_some() {
668             return;
669         }
670 
671         std::mem::discriminant(&e.kind).hash(&mut self.s);
672 
673         match e.kind {
674             ExprKind::AddrOf(kind, m, e) => {
675                 std::mem::discriminant(&kind).hash(&mut self.s);
676                 m.hash(&mut self.s);
677                 self.hash_expr(e);
678             },
679             ExprKind::Continue(i) => {
680                 if let Some(i) = i.label {
681                     self.hash_name(i.ident.name);
682                 }
683             },
684             ExprKind::Assign(l, r, _) => {
685                 self.hash_expr(l);
686                 self.hash_expr(r);
687             },
688             ExprKind::AssignOp(ref o, l, r) => {
689                 std::mem::discriminant(&o.node).hash(&mut self.s);
690                 self.hash_expr(l);
691                 self.hash_expr(r);
692             },
693             ExprKind::Block(b, _) => {
694                 self.hash_block(b);
695             },
696             ExprKind::Binary(op, l, r) => {
697                 std::mem::discriminant(&op.node).hash(&mut self.s);
698                 self.hash_expr(l);
699                 self.hash_expr(r);
700             },
701             ExprKind::Break(i, ref j) => {
702                 if let Some(i) = i.label {
703                     self.hash_name(i.ident.name);
704                 }
705                 if let Some(j) = *j {
706                     self.hash_expr(j);
707                 }
708             },
709             ExprKind::DropTemps(e) | ExprKind::Yield(e, _) => {
710                 self.hash_expr(e);
711             },
712             ExprKind::Call(fun, args) => {
713                 self.hash_expr(fun);
714                 self.hash_exprs(args);
715             },
716             ExprKind::Cast(e, ty) | ExprKind::Type(e, ty) => {
717                 self.hash_expr(e);
718                 self.hash_ty(ty);
719             },
720             ExprKind::Closure(&Closure {
721                 capture_clause, body, ..
722             }) => {
723                 std::mem::discriminant(&capture_clause).hash(&mut self.s);
724                 // closures inherit TypeckResults
725                 self.hash_expr(self.cx.tcx.hir().body(body).value);
726             },
727             ExprKind::Field(e, ref f) => {
728                 self.hash_expr(e);
729                 self.hash_name(f.name);
730             },
731             ExprKind::Index(a, i) => {
732                 self.hash_expr(a);
733                 self.hash_expr(i);
734             },
735             ExprKind::InlineAsm(asm) => {
736                 for piece in asm.template {
737                     match piece {
738                         InlineAsmTemplatePiece::String(s) => s.hash(&mut self.s),
739                         InlineAsmTemplatePiece::Placeholder {
740                             operand_idx,
741                             modifier,
742                             span: _,
743                         } => {
744                             operand_idx.hash(&mut self.s);
745                             modifier.hash(&mut self.s);
746                         },
747                     }
748                 }
749                 asm.options.hash(&mut self.s);
750                 for (op, _op_sp) in asm.operands {
751                     match op {
752                         InlineAsmOperand::In { reg, expr } => {
753                             reg.hash(&mut self.s);
754                             self.hash_expr(expr);
755                         },
756                         InlineAsmOperand::Out { reg, late, expr } => {
757                             reg.hash(&mut self.s);
758                             late.hash(&mut self.s);
759                             if let Some(expr) = expr {
760                                 self.hash_expr(expr);
761                             }
762                         },
763                         InlineAsmOperand::InOut { reg, late, expr } => {
764                             reg.hash(&mut self.s);
765                             late.hash(&mut self.s);
766                             self.hash_expr(expr);
767                         },
768                         InlineAsmOperand::SplitInOut {
769                             reg,
770                             late,
771                             in_expr,
772                             out_expr,
773                         } => {
774                             reg.hash(&mut self.s);
775                             late.hash(&mut self.s);
776                             self.hash_expr(in_expr);
777                             if let Some(out_expr) = out_expr {
778                                 self.hash_expr(out_expr);
779                             }
780                         },
781                         InlineAsmOperand::Const { anon_const } | InlineAsmOperand::SymFn { anon_const } => {
782                             self.hash_body(anon_const.body);
783                         },
784                         InlineAsmOperand::SymStatic { path, def_id: _ } => self.hash_qpath(path),
785                     }
786                 }
787             },
788             ExprKind::OffsetOf(container, fields) => {
789                 self.hash_ty(container);
790                 for field in fields {
791                     self.hash_name(field.name);
792                 }
793             },
794             ExprKind::Let(Let { pat, init, ty, .. }) => {
795                 self.hash_expr(init);
796                 if let Some(ty) = ty {
797                     self.hash_ty(ty);
798                 }
799                 self.hash_pat(pat);
800             },
801             ExprKind::Err(_) => {},
802             ExprKind::Lit(l) => {
803                 l.node.hash(&mut self.s);
804             },
805             ExprKind::Loop(b, ref i, ..) => {
806                 self.hash_block(b);
807                 if let Some(i) = *i {
808                     self.hash_name(i.ident.name);
809                 }
810             },
811             ExprKind::If(cond, then, ref else_opt) => {
812                 self.hash_expr(cond);
813                 self.hash_expr(then);
814                 if let Some(e) = *else_opt {
815                     self.hash_expr(e);
816                 }
817             },
818             ExprKind::Match(e, arms, ref s) => {
819                 self.hash_expr(e);
820 
821                 for arm in arms {
822                     self.hash_pat(arm.pat);
823                     if let Some(ref e) = arm.guard {
824                         self.hash_guard(e);
825                     }
826                     self.hash_expr(arm.body);
827                 }
828 
829                 s.hash(&mut self.s);
830             },
831             ExprKind::MethodCall(path, receiver, args, ref _fn_span) => {
832                 self.hash_name(path.ident.name);
833                 self.hash_expr(receiver);
834                 self.hash_exprs(args);
835             },
836             ExprKind::ConstBlock(ref l_id) => {
837                 self.hash_body(l_id.body);
838             },
839             ExprKind::Repeat(e, len) => {
840                 self.hash_expr(e);
841                 self.hash_array_length(len);
842             },
843             ExprKind::Ret(ref e) => {
844                 if let Some(e) = *e {
845                     self.hash_expr(e);
846                 }
847             },
848             ExprKind::Become(f) => {
849                 self.hash_expr(f);
850             },
851             ExprKind::Path(ref qpath) => {
852                 self.hash_qpath(qpath);
853             },
854             ExprKind::Struct(path, fields, ref expr) => {
855                 self.hash_qpath(path);
856 
857                 for f in fields {
858                     self.hash_name(f.ident.name);
859                     self.hash_expr(f.expr);
860                 }
861 
862                 if let Some(e) = *expr {
863                     self.hash_expr(e);
864                 }
865             },
866             ExprKind::Tup(tup) => {
867                 self.hash_exprs(tup);
868             },
869             ExprKind::Array(v) => {
870                 self.hash_exprs(v);
871             },
872             ExprKind::Unary(lop, le) => {
873                 std::mem::discriminant(&lop).hash(&mut self.s);
874                 self.hash_expr(le);
875             },
876         }
877     }
878 
hash_exprs(&mut self, e: &[Expr<'_>])879     pub fn hash_exprs(&mut self, e: &[Expr<'_>]) {
880         for e in e {
881             self.hash_expr(e);
882         }
883     }
884 
hash_name(&mut self, n: Symbol)885     pub fn hash_name(&mut self, n: Symbol) {
886         n.hash(&mut self.s);
887     }
888 
hash_qpath(&mut self, p: &QPath<'_>)889     pub fn hash_qpath(&mut self, p: &QPath<'_>) {
890         match *p {
891             QPath::Resolved(_, path) => {
892                 self.hash_path(path);
893             },
894             QPath::TypeRelative(_, path) => {
895                 self.hash_name(path.ident.name);
896             },
897             QPath::LangItem(lang_item, ..) => {
898                 std::mem::discriminant(&lang_item).hash(&mut self.s);
899             },
900         }
901         // self.maybe_typeck_results.unwrap().qpath_res(p, id).hash(&mut self.s);
902     }
903 
hash_pat(&mut self, pat: &Pat<'_>)904     pub fn hash_pat(&mut self, pat: &Pat<'_>) {
905         std::mem::discriminant(&pat.kind).hash(&mut self.s);
906         match pat.kind {
907             PatKind::Binding(BindingAnnotation(by_ref, mutability), _, _, pat) => {
908                 std::mem::discriminant(&by_ref).hash(&mut self.s);
909                 std::mem::discriminant(&mutability).hash(&mut self.s);
910                 if let Some(pat) = pat {
911                     self.hash_pat(pat);
912                 }
913             },
914             PatKind::Box(pat) => self.hash_pat(pat),
915             PatKind::Lit(expr) => self.hash_expr(expr),
916             PatKind::Or(pats) => {
917                 for pat in pats {
918                     self.hash_pat(pat);
919                 }
920             },
921             PatKind::Path(ref qpath) => self.hash_qpath(qpath),
922             PatKind::Range(s, e, i) => {
923                 if let Some(s) = s {
924                     self.hash_expr(s);
925                 }
926                 if let Some(e) = e {
927                     self.hash_expr(e);
928                 }
929                 std::mem::discriminant(&i).hash(&mut self.s);
930             },
931             PatKind::Ref(pat, mu) => {
932                 self.hash_pat(pat);
933                 std::mem::discriminant(&mu).hash(&mut self.s);
934             },
935             PatKind::Slice(l, m, r) => {
936                 for pat in l {
937                     self.hash_pat(pat);
938                 }
939                 if let Some(pat) = m {
940                     self.hash_pat(pat);
941                 }
942                 for pat in r {
943                     self.hash_pat(pat);
944                 }
945             },
946             PatKind::Struct(ref qpath, fields, e) => {
947                 self.hash_qpath(qpath);
948                 for f in fields {
949                     self.hash_name(f.ident.name);
950                     self.hash_pat(f.pat);
951                 }
952                 e.hash(&mut self.s);
953             },
954             PatKind::Tuple(pats, e) => {
955                 for pat in pats {
956                     self.hash_pat(pat);
957                 }
958                 e.hash(&mut self.s);
959             },
960             PatKind::TupleStruct(ref qpath, pats, e) => {
961                 self.hash_qpath(qpath);
962                 for pat in pats {
963                     self.hash_pat(pat);
964                 }
965                 e.hash(&mut self.s);
966             },
967             PatKind::Wild => {},
968         }
969     }
970 
hash_path(&mut self, path: &Path<'_>)971     pub fn hash_path(&mut self, path: &Path<'_>) {
972         match path.res {
973             // constant hash since equality is dependant on inter-expression context
974             // e.g. The expressions `if let Some(x) = foo() {}` and `if let Some(y) = foo() {}` are considered equal
975             // even though the binding names are different and they have different `HirId`s.
976             Res::Local(_) => 1_usize.hash(&mut self.s),
977             _ => {
978                 for seg in path.segments {
979                     self.hash_name(seg.ident.name);
980                     self.hash_generic_args(seg.args().args);
981                 }
982             },
983         }
984     }
985 
hash_stmt(&mut self, b: &Stmt<'_>)986     pub fn hash_stmt(&mut self, b: &Stmt<'_>) {
987         std::mem::discriminant(&b.kind).hash(&mut self.s);
988 
989         match &b.kind {
990             StmtKind::Local(local) => {
991                 self.hash_pat(local.pat);
992                 if let Some(init) = local.init {
993                     self.hash_expr(init);
994                 }
995                 if let Some(els) = local.els {
996                     self.hash_block(els);
997                 }
998             },
999             StmtKind::Item(..) => {},
1000             StmtKind::Expr(expr) | StmtKind::Semi(expr) => {
1001                 self.hash_expr(expr);
1002             },
1003         }
1004     }
1005 
hash_guard(&mut self, g: &Guard<'_>)1006     pub fn hash_guard(&mut self, g: &Guard<'_>) {
1007         match g {
1008             Guard::If(expr) | Guard::IfLet(Let { init: expr, .. }) => {
1009                 self.hash_expr(expr);
1010             },
1011         }
1012     }
1013 
hash_lifetime(&mut self, lifetime: &Lifetime)1014     pub fn hash_lifetime(&mut self, lifetime: &Lifetime) {
1015         lifetime.ident.name.hash(&mut self.s);
1016         std::mem::discriminant(&lifetime.res).hash(&mut self.s);
1017         if let LifetimeName::Param(param_id) = lifetime.res {
1018             param_id.hash(&mut self.s);
1019         }
1020     }
1021 
hash_ty(&mut self, ty: &Ty<'_>)1022     pub fn hash_ty(&mut self, ty: &Ty<'_>) {
1023         std::mem::discriminant(&ty.kind).hash(&mut self.s);
1024         self.hash_tykind(&ty.kind);
1025     }
1026 
hash_tykind(&mut self, ty: &TyKind<'_>)1027     pub fn hash_tykind(&mut self, ty: &TyKind<'_>) {
1028         match ty {
1029             TyKind::Slice(ty) => {
1030                 self.hash_ty(ty);
1031             },
1032             &TyKind::Array(ty, len) => {
1033                 self.hash_ty(ty);
1034                 self.hash_array_length(len);
1035             },
1036             TyKind::Ptr(ref mut_ty) => {
1037                 self.hash_ty(mut_ty.ty);
1038                 mut_ty.mutbl.hash(&mut self.s);
1039             },
1040             TyKind::Ref(lifetime, ref mut_ty) => {
1041                 self.hash_lifetime(lifetime);
1042                 self.hash_ty(mut_ty.ty);
1043                 mut_ty.mutbl.hash(&mut self.s);
1044             },
1045             TyKind::BareFn(bfn) => {
1046                 bfn.unsafety.hash(&mut self.s);
1047                 bfn.abi.hash(&mut self.s);
1048                 for arg in bfn.decl.inputs {
1049                     self.hash_ty(arg);
1050                 }
1051                 std::mem::discriminant(&bfn.decl.output).hash(&mut self.s);
1052                 match bfn.decl.output {
1053                     FnRetTy::DefaultReturn(_) => {},
1054                     FnRetTy::Return(ty) => {
1055                         self.hash_ty(ty);
1056                     },
1057                 }
1058                 bfn.decl.c_variadic.hash(&mut self.s);
1059             },
1060             TyKind::Tup(ty_list) => {
1061                 for ty in *ty_list {
1062                     self.hash_ty(ty);
1063                 }
1064             },
1065             TyKind::Path(ref qpath) => self.hash_qpath(qpath),
1066             TyKind::OpaqueDef(_, arg_list, in_trait) => {
1067                 self.hash_generic_args(arg_list);
1068                 in_trait.hash(&mut self.s);
1069             },
1070             TyKind::TraitObject(_, lifetime, _) => {
1071                 self.hash_lifetime(lifetime);
1072             },
1073             TyKind::Typeof(anon_const) => {
1074                 self.hash_body(anon_const.body);
1075             },
1076             TyKind::Err(_) | TyKind::Infer | TyKind::Never => {},
1077         }
1078     }
1079 
hash_array_length(&mut self, length: ArrayLen)1080     pub fn hash_array_length(&mut self, length: ArrayLen) {
1081         match length {
1082             ArrayLen::Infer(..) => {},
1083             ArrayLen::Body(anon_const) => self.hash_body(anon_const.body),
1084         }
1085     }
1086 
hash_body(&mut self, body_id: BodyId)1087     pub fn hash_body(&mut self, body_id: BodyId) {
1088         // swap out TypeckResults when hashing a body
1089         let old_maybe_typeck_results = self.maybe_typeck_results.replace(self.cx.tcx.typeck_body(body_id));
1090         self.hash_expr(self.cx.tcx.hir().body(body_id).value);
1091         self.maybe_typeck_results = old_maybe_typeck_results;
1092     }
1093 
hash_generic_args(&mut self, arg_list: &[GenericArg<'_>])1094     fn hash_generic_args(&mut self, arg_list: &[GenericArg<'_>]) {
1095         for arg in arg_list {
1096             match *arg {
1097                 GenericArg::Lifetime(l) => self.hash_lifetime(l),
1098                 GenericArg::Type(ty) => self.hash_ty(ty),
1099                 GenericArg::Const(ref ca) => self.hash_body(ca.value.body),
1100                 GenericArg::Infer(ref inf) => self.hash_ty(&inf.to_ty()),
1101             }
1102         }
1103     }
1104 }
1105 
hash_stmt(cx: &LateContext<'_>, s: &Stmt<'_>) -> u641106 pub fn hash_stmt(cx: &LateContext<'_>, s: &Stmt<'_>) -> u64 {
1107     let mut h = SpanlessHash::new(cx);
1108     h.hash_stmt(s);
1109     h.finish()
1110 }
1111 
is_bool(ty: &Ty<'_>) -> bool1112 pub fn is_bool(ty: &Ty<'_>) -> bool {
1113     if let TyKind::Path(QPath::Resolved(_, path)) = ty.kind {
1114         matches!(path.res, Res::PrimTy(PrimTy::Bool))
1115     } else {
1116         false
1117     }
1118 }
1119 
hash_expr(cx: &LateContext<'_>, e: &Expr<'_>) -> u641120 pub fn hash_expr(cx: &LateContext<'_>, e: &Expr<'_>) -> u64 {
1121     let mut h = SpanlessHash::new(cx);
1122     h.hash_expr(e);
1123     h.finish()
1124 }
1125 
1126 #[expect(clippy::similar_names)]
eq_span_tokens( cx: &LateContext<'_>, left: impl SpanRange, right: impl SpanRange, pred: impl Fn(TokenKind) -> bool, ) -> bool1127 fn eq_span_tokens(
1128     cx: &LateContext<'_>,
1129     left: impl SpanRange,
1130     right: impl SpanRange,
1131     pred: impl Fn(TokenKind) -> bool,
1132 ) -> bool {
1133     fn f(cx: &LateContext<'_>, left: Range<BytePos>, right: Range<BytePos>, pred: impl Fn(TokenKind) -> bool) -> bool {
1134         if let Some(lsrc) = get_source_text(cx, left)
1135             && let Some(lsrc) = lsrc.as_str()
1136             && let Some(rsrc) = get_source_text(cx, right)
1137             && let Some(rsrc) = rsrc.as_str()
1138         {
1139             let pred = |t: &(_, _)| pred(t.0);
1140             let map = |(_, x)| x;
1141 
1142             let ltok = tokenize_with_text(lsrc)
1143                 .filter(pred)
1144                 .map(map);
1145             let rtok = tokenize_with_text(rsrc)
1146                 .filter(pred)
1147                 .map(map);
1148             ltok.eq(rtok)
1149         } else {
1150             // Unable to access the source. Conservatively assume the blocks aren't equal.
1151             false
1152         }
1153     }
1154     f(cx, left.into_range(), right.into_range(), pred)
1155 }
1156