Skip to content

Commit 1270bea

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 af44a07 commit 1270bea

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

clippy_utils/src/lib.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1983,16 +1983,25 @@ pub fn is_expr_identity_of_pat(cx: &LateContext<'_>, pat: &Pat<'_>, expr: &Expr<
19831983
zip(before.iter().chain(after), arr).all(|(pat, expr)| is_expr_identity_of_pat(cx, pat, expr, by_hir))
19841984
},
19851985
(PatKind::TupleStruct(pat_ident, field_pats, dotdot), ExprKind::Call(ident, fields))
1986-
if dotdot.as_opt_usize().is_none()
1987-
&& let ExprKind::Path(ident) = ident.kind =>
1986+
if dotdot.as_opt_usize().is_none() && field_pats.len() == fields.len() =>
19881987
{
1989-
field_pats.len() == fields.len()
1990-
&& qpath_res(&pat_ident, pat.hir_id) == qpath_res(&ident, expr.hir_id)
1991-
&& zip(field_pats, fields).all(|(pat, expr)| is_expr_identity_of_pat(cx, pat, expr, by_hir))
1992-
},
1993-
(PatKind::Struct(pat_ident, field_pats, false), ExprKind::Struct(ident, fields, hir::StructTailExpr::None)) => {
1994-
field_pats.len() == fields.len()
1988+
// check ident
1989+
if let ExprKind::Path(ident) = &ident.kind
19951990
&& qpath_res(&pat_ident, pat.hir_id) == qpath_res(ident, expr.hir_id)
1991+
// check fields
1992+
&& zip(field_pats, fields).all(|(pat, expr)| is_expr_identity_of_pat(cx, pat, expr,by_hir))
1993+
{
1994+
true
1995+
} else {
1996+
false
1997+
}
1998+
},
1999+
(PatKind::Struct(pat_ident, field_pats, false), ExprKind::Struct(ident, fields, hir::StructTailExpr::None))
2000+
if field_pats.len() == fields.len() =>
2001+
{
2002+
// check ident
2003+
qpath_res(&pat_ident, pat.hir_id) == qpath_res(ident, expr.hir_id)
2004+
// check fields
19962005
&& field_pats.iter().all(|field_pat| {
19972006
fields.iter().any(|field| {
19982007
field_pat.ident == field.ident && is_expr_identity_of_pat(cx, field_pat.pat, field.expr, by_hir)

0 commit comments

Comments
 (0)