Skip to content

Commit 2d20179

Browse files
committed
[manual_let_else]: only omit block if span is from same ctxt
1 parent 493ab53 commit 2d20179

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

clippy_lints/src/manual_let_else.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,9 @@ fn emit_manual_let_else(
136136
// for this to be machine applicable.
137137
let mut app = Applicability::HasPlaceholders;
138138
let (sn_expr, _) = snippet_with_context(cx, expr.span, span.ctxt(), "", &mut app);
139-
let (sn_else, _) = snippet_with_context(cx, else_body.span, span.ctxt(), "", &mut app);
139+
let (sn_else, else_is_mac_call) = snippet_with_context(cx, else_body.span, span.ctxt(), "", &mut app);
140140

141-
let else_bl = if matches!(else_body.kind, ExprKind::Block(..)) {
141+
let else_bl = if matches!(else_body.kind, ExprKind::Block(..)) && !else_is_mac_call {
142142
sn_else.into_owned()
143143
} else {
144144
format!("{{ {sn_else} }}")

tests/ui/manual_let_else_match.fixed

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,7 @@ fn not_fire() {
133133
[data @ .., 0, 0, 0, 0] | [data @ .., 0, 0] | [data @ ..] => data,
134134
};
135135
}
136+
137+
fn issue11579() {
138+
let Some(msg) = Some("hi") else { unreachable!("can't happen") };
139+
}

tests/ui/manual_let_else_match.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,11 @@ fn not_fire() {
170170
[data @ .., 0, 0, 0, 0] | [data @ .., 0, 0] | [data @ ..] => data,
171171
};
172172
}
173+
174+
fn issue11579() {
175+
let msg = match Some("hi") {
176+
//~^ ERROR: this could be rewritten as `let...else`
177+
Some(m) => m,
178+
_ => unreachable!("can't happen"),
179+
};
180+
}

tests/ui/manual_let_else_match.stderr

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,15 @@ LL | | _ => return,
9292
LL | | };
9393
| |______^ help: consider writing: `let ([data @ .., 0, 0, 0, 0] | [data @ .., 0, 0] | [data @ .., 0]) = data.as_slice() else { return };`
9494

95-
error: aborting due to 9 previous errors
95+
error: this could be rewritten as `let...else`
96+
--> $DIR/manual_let_else_match.rs:175:5
97+
|
98+
LL | / let msg = match Some("hi") {
99+
LL | |
100+
LL | | Some(m) => m,
101+
LL | | _ => unreachable!("can't happen"),
102+
LL | | };
103+
| |______^ help: consider writing: `let Some(msg) = Some("hi") else { unreachable!("can't happen") };`
104+
105+
error: aborting due to 10 previous errors
96106

0 commit comments

Comments
 (0)