Skip to content

Commit ae97d31

Browse files
is_expr_identity_of_pat: simplify using (unordered_)over (#15450)
This makes the last arm significantly shorter, but marginally shortens the other ones as well - I first removed the length comparisons from the match guards since they were already present in `(unordered_)over`, but then brought them back since they are quite a helpful "guard clause" - It's a bit unfortunate that the `Slice`/`Array` arm can't use `over` as well, which creates some asymmetry, but changing the signature of `over` to accept arbitrary iterators felt like too much to me, especially because we'd lose the ability to compare the lengths of the two inputs (which could've been mitigated by `ExactLenIterator::len`, but that method is still unstable AFAIR) One other option would be to only use `unordered_over` in the last arm, and not `over`, but I think `over` is not that bad.. changelog: none
2 parents 12a4f4d + ebbea34 commit ae97d31

File tree

3 files changed

+7
-8
lines changed

3 files changed

+7
-8
lines changed

clippy_utils/src/ast_utils/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub fn is_useless_with_eq_exprs(kind: BinOpKind) -> bool {
2121
}
2222

2323
/// Checks if each element in the first slice is contained within the latter as per `eq_fn`.
24-
pub fn unordered_over<X>(left: &[X], right: &[X], mut eq_fn: impl FnMut(&X, &X) -> bool) -> bool {
24+
pub fn unordered_over<X, Y>(left: &[X], right: &[Y], mut eq_fn: impl FnMut(&X, &Y) -> bool) -> bool {
2525
left.len() == right.len() && left.iter().all(|l| right.iter().any(|r| eq_fn(l, r)))
2626
}
2727

clippy_utils/src/hir_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ pub fn both_some_and<X, Y>(l: Option<X>, r: Option<Y>, mut pred: impl FnMut(X, Y
757757
}
758758

759759
/// Checks if two slices are equal as per `eq_fn`.
760-
pub fn over<X>(left: &[X], right: &[X], mut eq_fn: impl FnMut(&X, &X) -> bool) -> bool {
760+
pub fn over<X, Y>(left: &[X], right: &[Y], mut eq_fn: impl FnMut(&X, &Y) -> bool) -> bool {
761761
left.len() == right.len() && left.iter().zip(right).all(|(x, y)| eq_fn(x, y))
762762
}
763763

clippy_utils/src/lib.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ use rustc_span::{InnerSpan, Span};
126126
use source::{SpanRangeExt, walk_span_to_context};
127127
use visitors::{Visitable, for_each_unconsumed_temporary};
128128

129+
use crate::ast_utils::unordered_over;
129130
use crate::consts::{ConstEvalCtxt, Constant, mir_to_const};
130131
use crate::higher::Range;
131132
use crate::ty::{adt_and_variant_of_res, can_partially_move_ty, expr_sig, is_copy, is_recursively_primitive_type};
@@ -1992,7 +1993,7 @@ pub fn is_expr_identity_of_pat(cx: &LateContext<'_>, pat: &Pat<'_>, expr: &Expr<
19921993
(PatKind::Tuple(pats, dotdot), ExprKind::Tup(tup))
19931994
if dotdot.as_opt_usize().is_none() && pats.len() == tup.len() =>
19941995
{
1995-
zip(pats, tup).all(|(pat, expr)| is_expr_identity_of_pat(cx, pat, expr, by_hir))
1996+
over(pats, tup, |pat, expr| is_expr_identity_of_pat(cx, pat, expr, by_hir))
19961997
},
19971998
(PatKind::Slice(before, None, after), ExprKind::Array(arr)) if before.len() + after.len() == arr.len() => {
19981999
zip(before.iter().chain(after), arr).all(|(pat, expr)| is_expr_identity_of_pat(cx, pat, expr, by_hir))
@@ -2004,7 +2005,7 @@ pub fn is_expr_identity_of_pat(cx: &LateContext<'_>, pat: &Pat<'_>, expr: &Expr<
20042005
if let ExprKind::Path(ident) = &ident.kind
20052006
&& qpath_res(&pat_ident, pat.hir_id) == qpath_res(ident, expr.hir_id)
20062007
// check fields
2007-
&& zip(field_pats, fields).all(|(pat, expr)| is_expr_identity_of_pat(cx, pat, expr,by_hir))
2008+
&& over(field_pats, fields, |pat, expr| is_expr_identity_of_pat(cx, pat, expr,by_hir))
20082009
{
20092010
true
20102011
} else {
@@ -2017,10 +2018,8 @@ pub fn is_expr_identity_of_pat(cx: &LateContext<'_>, pat: &Pat<'_>, expr: &Expr<
20172018
// check ident
20182019
qpath_res(&pat_ident, pat.hir_id) == qpath_res(ident, expr.hir_id)
20192020
// check fields
2020-
&& field_pats.iter().all(|field_pat| {
2021-
fields.iter().any(|field| {
2022-
field_pat.ident == field.ident && is_expr_identity_of_pat(cx, field_pat.pat, field.expr, by_hir)
2023-
})
2021+
&& unordered_over(field_pats, fields, |field_pat, field| {
2022+
field_pat.ident == field.ident && is_expr_identity_of_pat(cx, field_pat.pat, field.expr, by_hir)
20242023
})
20252024
},
20262025
_ => false,

0 commit comments

Comments
 (0)