Skip to content

Commit 00904cb

Browse files
committed
Manage macros case + move to MaybeIncorrect when binding values
1 parent 53094de commit 00904cb

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

clippy_lints/src/matches.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,8 @@ declare_clippy_lint! {
285285
///
286286
/// **Why is this bad?** Readability and needless complexity.
287287
///
288-
/// **Known problems:** None.
288+
/// **Known problems:** Suggested replacements may be incorrect when `match`
289+
/// is actually binding temporary value, bringing a 'dropped while borrowed' error.
289290
///
290291
/// **Example:**
291292
/// ```rust
@@ -835,23 +836,22 @@ fn check_match_single_binding(cx: &LateContext<'_, '_>, ex: &Expr<'_>, arms: &[A
835836
};
836837

837838
// 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-
}
850-
}
839+
match match_body.kind {
840+
ExprKind::Block(block, _) => {
841+
// macro + expr_ty(body) == ()
842+
if block.span.from_expansion() && cx.tables.expr_ty(&match_body).is_unit() {
843+
snippet_body.push(';');
851844
}
852-
}
845+
},
846+
_ => {
847+
// expr_ty(body) == ()
848+
if cx.tables.expr_ty(&match_body).is_unit() {
849+
snippet_body.push(';');
850+
}
851+
},
853852
}
854853

854+
let mut applicability = Applicability::MaybeIncorrect;
855855
match arms[0].pat.kind {
856856
PatKind::Binding(..) | PatKind::Tuple(_, _) | PatKind::Struct(..) => {
857857
span_lint_and_sugg(
@@ -862,11 +862,11 @@ fn check_match_single_binding(cx: &LateContext<'_, '_>, ex: &Expr<'_>, arms: &[A
862862
"consider using `let` statement",
863863
format!(
864864
"let {} = {};\n{}",
865-
snippet(cx, bind_names, ".."),
866-
snippet(cx, matched_vars, ".."),
865+
snippet_with_applicability(cx, bind_names, "..", &mut applicability),
866+
snippet_with_applicability(cx, matched_vars, "..", &mut applicability),
867867
snippet_body
868868
),
869-
Applicability::MachineApplicable,
869+
applicability,
870870
);
871871
},
872872
PatKind::Wild => {

0 commit comments

Comments
 (0)