Skip to content

Commit 94c959a

Browse files
committed
is_body_identity_function: recognize |[x, y]| [x, y]
1 parent 6e5bb86 commit 94c959a

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

clippy_utils/src/lib.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1896,6 +1896,7 @@ pub fn is_must_use_func_call(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
18961896
/// * `|x| { return x }`
18971897
/// * `|x| { return x; }`
18981898
/// * `|(x, y)| (x, y)`
1899+
/// * `|[x, y]| [x, y]`
18991900
///
19001901
/// Consider calling [`is_expr_untyped_identity_function`] or [`is_expr_identity_function`] instead.
19011902
fn is_body_identity_function(cx: &LateContext<'_>, func: &Body<'_>) -> bool {
@@ -1906,9 +1907,9 @@ fn is_body_identity_function(cx: &LateContext<'_>, func: &Body<'_>) -> bool {
19061907
.get(pat.hir_id)
19071908
.is_some_and(|mode| matches!(mode.0, ByRef::Yes(_)))
19081909
{
1909-
// If a tuple `(x, y)` is of type `&(i32, i32)`, then due to match ergonomics,
1910-
// the inner patterns become references. Don't consider this the identity function
1911-
// as that changes types.
1910+
// If the parameter is `(x, y)` of type `&(T, T)`, or `[x, y]` of type `&[T; 2]`, then
1911+
// due to match ergonomics, the inner patterns become references. Don't consider this
1912+
// the identity function as that changes types.
19121913
return false;
19131914
}
19141915

@@ -1921,6 +1922,13 @@ fn is_body_identity_function(cx: &LateContext<'_>, func: &Body<'_>) -> bool {
19211922
{
19221923
pats.iter().zip(tup).all(|(pat, expr)| check_pat(cx, pat, expr))
19231924
},
1925+
(PatKind::Slice(before, slice, after), ExprKind::Array(arr))
1926+
if slice.is_none() && before.len() + after.len() == arr.len() =>
1927+
{
1928+
(before.iter().chain(after))
1929+
.zip(arr)
1930+
.all(|(pat, expr)| check_pat(cx, pat, expr))
1931+
},
19241932
_ => false,
19251933
}
19261934
}

0 commit comments

Comments
 (0)