Skip to content

Commit 5c98848

Browse files
committed
style: move precondition checking to the match guard
the match arms above put the "sanity" checks in the guard, and call only `check_pat` in the body. With this commit, the (tuple) struct cases follow that convention as well. Well, almost -- I think the ident check belongs to the second category of checks, so I put it in the body as well
1 parent 25f5006 commit 5c98848

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

clippy_utils/src/lib.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1931,19 +1931,26 @@ fn is_body_identity_function(cx: &LateContext<'_>, func: &Body<'_>) -> bool {
19311931
zip(before.iter().chain(after), arr).all(|(pat, expr)| check_pat(cx, pat, expr))
19321932
},
19331933
(PatKind::TupleStruct(pat_ident, field_pats, dotdot), ExprKind::Call(ident, fields))
1934-
if dotdot.as_opt_usize().is_none()
1935-
&& let ExprKind::Path(ident) = ident.kind =>
1934+
if dotdot.as_opt_usize().is_none() && field_pats.len() == fields.len() =>
19361935
{
1937-
field_pats.len() == fields.len()
1938-
&& qpath_res(&pat_ident, pat.hir_id) == qpath_res(&ident, expr.hir_id)
1936+
// check ident
1937+
if let ExprKind::Path(ident) = &ident.kind
1938+
&& qpath_res(&pat_ident, pat.hir_id) == qpath_res(ident, expr.hir_id)
1939+
// check fields
19391940
&& zip(field_pats, fields).all(|(pat, expr)| check_pat(cx, pat, expr))
1941+
{
1942+
true
1943+
} else {
1944+
false
1945+
}
19401946
},
19411947
(
19421948
PatKind::Struct(pat_ident, field_pats, false),
19431949
ExprKind::Struct(ident, fields, hir::StructTailExpr::None),
1944-
) => {
1945-
field_pats.len() == fields.len()
1946-
&& qpath_res(&pat_ident, pat.hir_id) == qpath_res(ident, expr.hir_id)
1950+
) if field_pats.len() == fields.len() => {
1951+
// check ident
1952+
qpath_res(&pat_ident, pat.hir_id) == qpath_res(ident, expr.hir_id)
1953+
// check fields
19471954
&& field_pats.iter().all(|field_pat| {
19481955
fields
19491956
.iter()

0 commit comments

Comments
 (0)