Skip to content

Commit 2aa4f3d

Browse files
committed
Better semi colon adding management
1 parent c19ca03 commit 2aa4f3d

File tree

1 file changed

+44
-36
lines changed

1 file changed

+44
-36
lines changed

clippy_lints/src/matches.rs

Lines changed: 44 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -833,46 +833,54 @@ fn check_match_single_binding(cx: &LateContext<'_, '_>, ex: &Expr<'_>, arms: &[A
833833
} else {
834834
snippet_block(cx, match_body.span, "..").to_owned().to_string()
835835
};
836-
if let ExprKind::Block(block, _) = &arms[0].body.kind {
837-
// Do we need to add ';' to suggestion ?
838-
if block.stmts.len() == 1 {
839-
if let StmtKind::Semi(_) = block.stmts.get(0).unwrap().kind {
840-
if !snippet_body.starts_with('{') && !snippet_body.ends_with('}') {
841-
snippet_body.push(';');
836+
837+
// Do we need to add ';' to suggestion ?
838+
if_chain! {
839+
if let ExprKind::Block(block, _) = &arms[0].body.kind;
840+
if block.stmts.len() == 1;
841+
if let StmtKind::Semi(s) = block.stmts.get(0).unwrap().kind;
842+
then {
843+
match s.kind {
844+
ExprKind::Block(_, _) => (),
845+
_ => {
846+
// expr_ty(body) == ()
847+
if cx.tables.expr_ty(&arms[0].body).is_unit() {
848+
snippet_body.push(';');
849+
}
842850
}
843851
}
844852
}
853+
}
845854

846-
match arms[0].pat.kind {
847-
PatKind::Binding(..) | PatKind::Tuple(_, _) | PatKind::Struct(..) => {
848-
span_lint_and_sugg(
849-
cx,
850-
MATCH_SINGLE_BINDING,
851-
expr.span,
852-
"this match could be written as a `let` statement",
853-
"consider using `let` statement",
854-
format!(
855-
"let {} = {};\n{}",
856-
snippet(cx, bind_names, ".."),
857-
snippet(cx, matched_vars, ".."),
858-
snippet_body
859-
),
860-
Applicability::MachineApplicable,
861-
);
862-
},
863-
PatKind::Wild => {
864-
span_lint_and_sugg(
865-
cx,
866-
MATCH_SINGLE_BINDING,
867-
expr.span,
868-
"this match could be replaced by its body itself",
869-
"consider using the match body instead",
870-
snippet_body,
871-
Applicability::MachineApplicable,
872-
);
873-
},
874-
_ => (),
875-
}
855+
match arms[0].pat.kind {
856+
PatKind::Binding(..) | PatKind::Tuple(_, _) | PatKind::Struct(..) => {
857+
span_lint_and_sugg(
858+
cx,
859+
MATCH_SINGLE_BINDING,
860+
expr.span,
861+
"this match could be written as a `let` statement",
862+
"consider using `let` statement",
863+
format!(
864+
"let {} = {};\n{}",
865+
snippet(cx, bind_names, ".."),
866+
snippet(cx, matched_vars, ".."),
867+
snippet_body
868+
),
869+
Applicability::MachineApplicable,
870+
);
871+
},
872+
PatKind::Wild => {
873+
span_lint_and_sugg(
874+
cx,
875+
MATCH_SINGLE_BINDING,
876+
expr.span,
877+
"this match could be replaced by its body itself",
878+
"consider using the match body instead",
879+
snippet_body,
880+
Applicability::MachineApplicable,
881+
);
882+
},
883+
_ => (),
876884
}
877885
}
878886

0 commit comments

Comments
 (0)