Skip to content

Commit

Permalink
[manual_let_else]: only omit block if span is from same ctxt
Browse files Browse the repository at this point in the history
  • Loading branch information
y21 committed Sep 29, 2023
1 parent 493ab53 commit 2d20179
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
4 changes: 2 additions & 2 deletions clippy_lints/src/manual_let_else.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ fn emit_manual_let_else(
// for this to be machine applicable.
let mut app = Applicability::HasPlaceholders;
let (sn_expr, _) = snippet_with_context(cx, expr.span, span.ctxt(), "", &mut app);
let (sn_else, _) = snippet_with_context(cx, else_body.span, span.ctxt(), "", &mut app);
let (sn_else, else_is_mac_call) = snippet_with_context(cx, else_body.span, span.ctxt(), "", &mut app);

let else_bl = if matches!(else_body.kind, ExprKind::Block(..)) {
let else_bl = if matches!(else_body.kind, ExprKind::Block(..)) && !else_is_mac_call {
sn_else.into_owned()
} else {
format!("{{ {sn_else} }}")
Expand Down
4 changes: 4 additions & 0 deletions tests/ui/manual_let_else_match.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,7 @@ fn not_fire() {
[data @ .., 0, 0, 0, 0] | [data @ .., 0, 0] | [data @ ..] => data,
};
}

fn issue11579() {
let Some(msg) = Some("hi") else { unreachable!("can't happen") };
}
8 changes: 8 additions & 0 deletions tests/ui/manual_let_else_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,11 @@ fn not_fire() {
[data @ .., 0, 0, 0, 0] | [data @ .., 0, 0] | [data @ ..] => data,
};
}

fn issue11579() {
let msg = match Some("hi") {
//~^ ERROR: this could be rewritten as `let...else`
Some(m) => m,
_ => unreachable!("can't happen"),
};
}
12 changes: 11 additions & 1 deletion tests/ui/manual_let_else_match.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,15 @@ LL | | _ => return,
LL | | };
| |______^ help: consider writing: `let ([data @ .., 0, 0, 0, 0] | [data @ .., 0, 0] | [data @ .., 0]) = data.as_slice() else { return };`

error: aborting due to 9 previous errors
error: this could be rewritten as `let...else`
--> $DIR/manual_let_else_match.rs:175:5
|
LL | / let msg = match Some("hi") {
LL | |
LL | | Some(m) => m,
LL | | _ => unreachable!("can't happen"),
LL | | };
| |______^ help: consider writing: `let Some(msg) = Some("hi") else { unreachable!("can't happen") };`

error: aborting due to 10 previous errors

0 comments on commit 2d20179

Please sign in to comment.