Skip to content

Commit d93889a

Browse files
committed
Add expr_fallback to SpanlessEq
1 parent 51b2fb3 commit d93889a

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

clippy_lints/src/utils/hir_utils.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub struct SpanlessEq<'a, 'tcx> {
2626
cx: &'a LateContext<'tcx>,
2727
maybe_typeck_results: Cell<Option<&'tcx TypeckResults<'tcx>>>,
2828
allow_side_effects: bool,
29+
expr_fallback: Option<Box<dyn Fn(&Expr<'_>, &Expr<'_>) -> bool + 'a>>,
2930
}
3031

3132
impl<'a, 'tcx> SpanlessEq<'a, 'tcx> {
@@ -34,6 +35,7 @@ impl<'a, 'tcx> SpanlessEq<'a, 'tcx> {
3435
cx,
3536
maybe_typeck_results: Cell::new(cx.maybe_typeck_results()),
3637
allow_side_effects: true,
38+
expr_fallback: None,
3739
}
3840
}
3941

@@ -45,6 +47,13 @@ impl<'a, 'tcx> SpanlessEq<'a, 'tcx> {
4547
}
4648
}
4749

50+
pub fn expr_fallback(self, expr_fallback: impl Fn(&Expr<'_>, &Expr<'_>) -> bool + 'a) -> Self {
51+
Self {
52+
expr_fallback: Some(Box::new(expr_fallback)),
53+
..self
54+
}
55+
}
56+
4857
/// Checks whether two statements are the same.
4958
pub fn eq_stmt(&self, left: &Stmt<'_>, right: &Stmt<'_>) -> bool {
5059
match (&left.kind, &right.kind) {
@@ -82,7 +91,7 @@ impl<'a, 'tcx> SpanlessEq<'a, 'tcx> {
8291
}
8392
}
8493

85-
match (&reduce_exprkind(&left.kind), &reduce_exprkind(&right.kind)) {
94+
let is_eq = match (&reduce_exprkind(&left.kind), &reduce_exprkind(&right.kind)) {
8695
(&ExprKind::AddrOf(lb, l_mut, ref le), &ExprKind::AddrOf(rb, r_mut, ref re)) => {
8796
lb == rb && l_mut == r_mut && self.eq_expr(le, re)
8897
},
@@ -156,7 +165,8 @@ impl<'a, 'tcx> SpanlessEq<'a, 'tcx> {
156165
(&ExprKind::Array(l), &ExprKind::Array(r)) => self.eq_exprs(l, r),
157166
(&ExprKind::DropTemps(ref le), &ExprKind::DropTemps(ref re)) => self.eq_expr(le, re),
158167
_ => false,
159-
}
168+
};
169+
is_eq || self.expr_fallback.as_ref().map_or(false, |f| f(left, right))
160170
}
161171

162172
fn eq_exprs(&self, left: &[Expr<'_>], right: &[Expr<'_>]) -> bool {

0 commit comments

Comments
 (0)