Skip to content

Commit ba43632

Browse files
committed
Auto merge of rust-lang#11736 - gernot-ohner:issue-10267, r=flip1995
Make SpanlessEq more consistent 1) Remove wildcard as requested in rust-lang/rust-clippy#10267. 2) Implement `hir_utils::eq_expr` for `ExprKind::Closure`, `ExprKind::ConstBlock`, `ExprKind::InlineAsm` and `ExprKind::Yield`. 3) Reorder branches of `hir_utils::eq_expr` to be in alphabetical order. --- changelog: none
2 parents 6d9516a + 171845d commit ba43632

File tree

1 file changed

+83
-33
lines changed

1 file changed

+83
-33
lines changed

clippy_utils/src/hir_utils.rs

+83-33
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ impl HirEqInterExpr<'_, '_, '_> {
247247
res
248248
}
249249

250-
#[expect(clippy::similar_names)]
250+
#[expect(clippy::similar_names, clippy::too_many_lines)]
251251
pub fn eq_expr(&mut self, left: &Expr<'_>, right: &Expr<'_>) -> bool {
252252
if !self.check_ctxt(left.span.ctxt(), right.span.ctxt()) {
253253
return false;
@@ -271,9 +271,7 @@ impl HirEqInterExpr<'_, '_, '_> {
271271
(&ExprKind::AddrOf(lb, l_mut, le), &ExprKind::AddrOf(rb, r_mut, re)) => {
272272
lb == rb && l_mut == r_mut && self.eq_expr(le, re)
273273
},
274-
(&ExprKind::Continue(li), &ExprKind::Continue(ri)) => {
275-
both(&li.label, &ri.label, |l, r| l.ident.name == r.ident.name)
276-
},
274+
(&ExprKind::Array(l), &ExprKind::Array(r)) => self.eq_exprs(l, r),
277275
(&ExprKind::Assign(ll, lr, _), &ExprKind::Assign(rl, rr, _)) => {
278276
self.inner.allow_side_effects && self.eq_expr(ll, rl) && self.eq_expr(lr, rr)
279277
},
@@ -294,9 +292,15 @@ impl HirEqInterExpr<'_, '_, '_> {
294292
(&ExprKind::Call(l_fun, l_args), &ExprKind::Call(r_fun, r_args)) => {
295293
self.inner.allow_side_effects && self.eq_expr(l_fun, r_fun) && self.eq_exprs(l_args, r_args)
296294
},
297-
(&ExprKind::Cast(lx, lt), &ExprKind::Cast(rx, rt)) | (&ExprKind::Type(lx, lt), &ExprKind::Type(rx, rt)) => {
295+
(&ExprKind::Cast(lx, lt), &ExprKind::Cast(rx, rt)) => {
298296
self.eq_expr(lx, rx) && self.eq_ty(lt, rt)
299297
},
298+
(&ExprKind::Closure(_l), &ExprKind::Closure(_r)) => false,
299+
(&ExprKind::ConstBlock(lb), &ExprKind::ConstBlock(rb)) => self.eq_body(lb.body, rb.body),
300+
(&ExprKind::Continue(li), &ExprKind::Continue(ri)) => {
301+
both(&li.label, &ri.label, |l, r| l.ident.name == r.ident.name)
302+
},
303+
(&ExprKind::DropTemps(le), &ExprKind::DropTemps(re)) => self.eq_expr(le, re),
300304
(&ExprKind::Field(l_f_exp, ref l_f_ident), &ExprKind::Field(r_f_exp, ref r_f_ident)) => {
301305
l_f_ident.name == r_f_ident.name && self.eq_expr(l_f_exp, r_f_exp)
302306
},
@@ -329,24 +333,70 @@ impl HirEqInterExpr<'_, '_, '_> {
329333
&& self.eq_expr(l_receiver, r_receiver)
330334
&& self.eq_exprs(l_args, r_args)
331335
},
336+
(&ExprKind::OffsetOf(l_container, l_fields), &ExprKind::OffsetOf(r_container, r_fields)) => {
337+
self.eq_ty(l_container, r_container) && over(l_fields, r_fields, |l, r| l.name == r.name)
338+
},
339+
(ExprKind::Path(l), ExprKind::Path(r)) => self.eq_qpath(l, r),
332340
(&ExprKind::Repeat(le, ll), &ExprKind::Repeat(re, rl)) => {
333341
self.eq_expr(le, re) && self.eq_array_length(ll, rl)
334342
},
335343
(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),
337344
(&ExprKind::Struct(l_path, lf, ref lo), &ExprKind::Struct(r_path, rf, ref ro)) => {
338345
self.eq_qpath(l_path, r_path)
339346
&& both(lo, ro, |l, r| self.eq_expr(l, r))
340347
&& over(lf, rf, |l, r| self.eq_expr_field(l, r))
341348
},
342349
(&ExprKind::Tup(l_tup), &ExprKind::Tup(r_tup)) => self.eq_exprs(l_tup, r_tup),
350+
(&ExprKind::Type(le, lt), &ExprKind::Type(re, rt)) => self.eq_expr(le, re) && self.eq_ty(lt, rt),
343351
(&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,
352+
(&ExprKind::Yield(le, _), &ExprKind::Yield(re, _)) => return self.eq_expr(le, re),
353+
(
354+
// Else branches for branches above, grouped as per `match_same_arms`.
355+
| &ExprKind::AddrOf(..)
356+
| &ExprKind::Array(..)
357+
| &ExprKind::Assign(..)
358+
| &ExprKind::AssignOp(..)
359+
| &ExprKind::Binary(..)
360+
| &ExprKind::Become(..)
361+
| &ExprKind::Block(..)
362+
| &ExprKind::Break(..)
363+
| &ExprKind::Call(..)
364+
| &ExprKind::Cast(..)
365+
| &ExprKind::ConstBlock(..)
366+
| &ExprKind::Continue(..)
367+
| &ExprKind::DropTemps(..)
368+
| &ExprKind::Field(..)
369+
| &ExprKind::Index(..)
370+
| &ExprKind::If(..)
371+
| &ExprKind::Let(..)
372+
| &ExprKind::Lit(..)
373+
| &ExprKind::Loop(..)
374+
| &ExprKind::Match(..)
375+
| &ExprKind::MethodCall(..)
376+
| &ExprKind::OffsetOf(..)
377+
| &ExprKind::Path(..)
378+
| &ExprKind::Repeat(..)
379+
| &ExprKind::Ret(..)
380+
| &ExprKind::Struct(..)
381+
| &ExprKind::Tup(..)
382+
| &ExprKind::Type(..)
383+
| &ExprKind::Unary(..)
384+
| &ExprKind::Yield(..)
385+
386+
// --- Special cases that do not have a positive branch.
387+
388+
// `Err` represents an invalid expression, so let's never assume that
389+
// an invalid expressions is equal to anything.
390+
| &ExprKind::Err(..)
391+
392+
// For the time being, we always consider that two closures are unequal.
393+
// This behavior may change in the future.
394+
| &ExprKind::Closure(..)
395+
// For the time being, we always consider that two instances of InlineAsm are different.
396+
// This behavior may change in the future.
397+
| &ExprKind::InlineAsm(_)
398+
, _
399+
) => false,
350400
};
351401
(is_eq && (!self.should_ignore(left) || !self.should_ignore(right)))
352402
|| self.inner.expr_fallback.as_mut().map_or(false, |f| f(left, right))
@@ -684,6 +734,9 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
684734
self.hash_name(i.ident.name);
685735
}
686736
},
737+
ExprKind::Array(v) => {
738+
self.hash_exprs(v);
739+
},
687740
ExprKind::Assign(l, r, _) => {
688741
self.hash_expr(l);
689742
self.hash_expr(r);
@@ -693,6 +746,9 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
693746
self.hash_expr(l);
694747
self.hash_expr(r);
695748
},
749+
ExprKind::Become(f) => {
750+
self.hash_expr(f);
751+
},
696752
ExprKind::Block(b, _) => {
697753
self.hash_block(b);
698754
},
@@ -709,9 +765,6 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
709765
self.hash_expr(j);
710766
}
711767
},
712-
ExprKind::DropTemps(e) | ExprKind::Yield(e, _) => {
713-
self.hash_expr(e);
714-
},
715768
ExprKind::Call(fun, args) => {
716769
self.hash_expr(fun);
717770
self.hash_exprs(args);
@@ -727,6 +780,12 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
727780
// closures inherit TypeckResults
728781
self.hash_expr(self.cx.tcx.hir().body(body).value);
729782
},
783+
ExprKind::ConstBlock(ref l_id) => {
784+
self.hash_body(l_id.body);
785+
},
786+
ExprKind::DropTemps(e) | ExprKind::Yield(e, _) => {
787+
self.hash_expr(e);
788+
},
730789
ExprKind::Field(e, ref f) => {
731790
self.hash_expr(e);
732791
self.hash_name(f.name);
@@ -788,20 +847,13 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
788847
}
789848
}
790849
},
791-
ExprKind::OffsetOf(container, fields) => {
792-
self.hash_ty(container);
793-
for field in fields {
794-
self.hash_name(field.name);
795-
}
796-
},
797850
ExprKind::Let(Let { pat, init, ty, .. }) => {
798851
self.hash_expr(init);
799852
if let Some(ty) = ty {
800853
self.hash_ty(ty);
801854
}
802855
self.hash_pat(pat);
803856
},
804-
ExprKind::Err(_) => {},
805857
ExprKind::Lit(l) => {
806858
l.node.hash(&mut self.s);
807859
},
@@ -836,8 +888,14 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
836888
self.hash_expr(receiver);
837889
self.hash_exprs(args);
838890
},
839-
ExprKind::ConstBlock(ref l_id) => {
840-
self.hash_body(l_id.body);
891+
ExprKind::OffsetOf(container, fields) => {
892+
self.hash_ty(container);
893+
for field in fields {
894+
self.hash_name(field.name);
895+
}
896+
},
897+
ExprKind::Path(ref qpath) => {
898+
self.hash_qpath(qpath);
841899
},
842900
ExprKind::Repeat(e, len) => {
843901
self.hash_expr(e);
@@ -848,12 +906,6 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
848906
self.hash_expr(e);
849907
}
850908
},
851-
ExprKind::Become(f) => {
852-
self.hash_expr(f);
853-
},
854-
ExprKind::Path(ref qpath) => {
855-
self.hash_qpath(qpath);
856-
},
857909
ExprKind::Struct(path, fields, ref expr) => {
858910
self.hash_qpath(path);
859911

@@ -869,13 +921,11 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
869921
ExprKind::Tup(tup) => {
870922
self.hash_exprs(tup);
871923
},
872-
ExprKind::Array(v) => {
873-
self.hash_exprs(v);
874-
},
875924
ExprKind::Unary(lop, le) => {
876925
std::mem::discriminant(&lop).hash(&mut self.s);
877926
self.hash_expr(le);
878927
},
928+
ExprKind::Err(_) => {},
879929
}
880930
}
881931

0 commit comments

Comments
 (0)