diff options
| author | Samuel Tardieu <sam@rfc1149.net> | 2025-08-22 16:55:42 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-08-22 16:55:42 +0000 |
| commit | ae97d31c76155f34a89f72cc2e46bd2f62ecbd2f (patch) | |
| tree | 280e43c8734c9b923e1286fe1d1578a1f331f54d | |
| parent | 12a4f4d27b55bcf5b22f29225ea3c86421b22008 (diff) | |
| parent | ebbea345726e04cd26b69b8334169ba0571b2d34 (diff) | |
| download | rust-ae97d31c76155f34a89f72cc2e46bd2f62ecbd2f.tar.gz rust-ae97d31c76155f34a89f72cc2e46bd2f62ecbd2f.zip | |
`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
| -rw-r--r-- | clippy_utils/src/ast_utils/mod.rs | 2 | ||||
| -rw-r--r-- | clippy_utils/src/hir_utils.rs | 2 | ||||
| -rw-r--r-- | clippy_utils/src/lib.rs | 11 |
3 files changed, 7 insertions, 8 deletions
diff --git a/clippy_utils/src/ast_utils/mod.rs b/clippy_utils/src/ast_utils/mod.rs index 40c00568a3b..ad69e6eb184 100644 --- a/clippy_utils/src/ast_utils/mod.rs +++ b/clippy_utils/src/ast_utils/mod.rs @@ -21,7 +21,7 @@ pub fn is_useless_with_eq_exprs(kind: BinOpKind) -> bool { } /// Checks if each element in the first slice is contained within the latter as per `eq_fn`. -pub fn unordered_over<X>(left: &[X], right: &[X], mut eq_fn: impl FnMut(&X, &X) -> bool) -> bool { +pub fn unordered_over<X, Y>(left: &[X], right: &[Y], mut eq_fn: impl FnMut(&X, &Y) -> bool) -> bool { left.len() == right.len() && left.iter().all(|l| right.iter().any(|r| eq_fn(l, r))) } diff --git a/clippy_utils/src/hir_utils.rs b/clippy_utils/src/hir_utils.rs index 8160443f413..b79e15cd717 100644 --- a/clippy_utils/src/hir_utils.rs +++ b/clippy_utils/src/hir_utils.rs @@ -757,7 +757,7 @@ pub fn both_some_and<X, Y>(l: Option<X>, r: Option<Y>, mut pred: impl FnMut(X, Y } /// Checks if two slices are equal as per `eq_fn`. -pub fn over<X>(left: &[X], right: &[X], mut eq_fn: impl FnMut(&X, &X) -> bool) -> bool { +pub fn over<X, Y>(left: &[X], right: &[Y], mut eq_fn: impl FnMut(&X, &Y) -> bool) -> bool { left.len() == right.len() && left.iter().zip(right).all(|(x, y)| eq_fn(x, y)) } diff --git a/clippy_utils/src/lib.rs b/clippy_utils/src/lib.rs index 8533fa85541..1e0833001cf 100644 --- a/clippy_utils/src/lib.rs +++ b/clippy_utils/src/lib.rs @@ -126,6 +126,7 @@ use rustc_span::{InnerSpan, Span}; use source::{SpanRangeExt, walk_span_to_context}; use visitors::{Visitable, for_each_unconsumed_temporary}; +use crate::ast_utils::unordered_over; use crate::consts::{ConstEvalCtxt, Constant, mir_to_const}; use crate::higher::Range; 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< (PatKind::Tuple(pats, dotdot), ExprKind::Tup(tup)) if dotdot.as_opt_usize().is_none() && pats.len() == tup.len() => { - zip(pats, tup).all(|(pat, expr)| is_expr_identity_of_pat(cx, pat, expr, by_hir)) + over(pats, tup, |pat, expr| is_expr_identity_of_pat(cx, pat, expr, by_hir)) }, (PatKind::Slice(before, None, after), ExprKind::Array(arr)) if before.len() + after.len() == arr.len() => { 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< if let ExprKind::Path(ident) = &ident.kind && qpath_res(&pat_ident, pat.hir_id) == qpath_res(ident, expr.hir_id) // check fields - && zip(field_pats, fields).all(|(pat, expr)| is_expr_identity_of_pat(cx, pat, expr,by_hir)) + && over(field_pats, fields, |pat, expr| is_expr_identity_of_pat(cx, pat, expr,by_hir)) { true } else { @@ -2017,10 +2018,8 @@ pub fn is_expr_identity_of_pat(cx: &LateContext<'_>, pat: &Pat<'_>, expr: &Expr< // check ident qpath_res(&pat_ident, pat.hir_id) == qpath_res(ident, expr.hir_id) // check fields - && field_pats.iter().all(|field_pat| { - fields.iter().any(|field| { - field_pat.ident == field.ident && is_expr_identity_of_pat(cx, field_pat.pat, field.expr, by_hir) - }) + && unordered_over(field_pats, fields, |field_pat, field| { + field_pat.ident == field.ident && is_expr_identity_of_pat(cx, field_pat.pat, field.expr, by_hir) }) }, _ => false, |
