Skip to content

Commit 53e1b45

Browse files
committed
Auto merge of rust-lang#9966 - alex-semenyuk:manual_let_else_paren, r=xFrednet
Fix manual_let_else produces a wrong suggestion with or-patterns Fix rust-lang#9938 changelog: Sugg: [`manual_let_else`]: Suggestions for or-patterns now include required brackets. [rust-lang#9966](rust-lang/rust-clippy#9966)
2 parents 3b8a10c + 05477ff commit 53e1b45

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

clippy_lints/src/manual_let_else.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,12 @@ fn emit_manual_let_else(cx: &LateContext<'_>, span: Span, expr: &Expr<'_>, pat:
151151
} else {
152152
format!("{{ {sn_else} }}")
153153
};
154-
let sugg = format!("let {sn_pat} = {sn_expr} else {else_bl};");
154+
let sn_bl = if matches!(pat.kind, PatKind::Or(..)) {
155+
format!("({sn_pat})")
156+
} else {
157+
sn_pat.into_owned()
158+
};
159+
let sugg = format!("let {sn_bl} = {sn_expr} else {else_bl};");
155160
diag.span_suggestion(span, "consider writing", sugg, app);
156161
},
157162
);

tests/ui/manual_let_else_match.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ fn fire() {
6464
Ok(v) => v,
6565
Err(()) => return,
6666
};
67+
68+
let f = Variant::Bar(1);
69+
70+
let _value = match f {
71+
Variant::Bar(_) | Variant::Baz(_) => (),
72+
_ => return,
73+
};
6774
}
6875

6976
fn not_fire() {

tests/ui/manual_let_else_match.stderr

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ LL | / let v = match h() {
2525
LL | | (Some(_), Some(_)) | (None, None) => continue,
2626
LL | | (Some(v), None) | (None, Some(v)) => v,
2727
LL | | };
28-
| |__________^ help: consider writing: `let (Some(v), None) | (None, Some(v)) = h() else { continue };`
28+
| |__________^ help: consider writing: `let ((Some(v), None) | (None, Some(v))) = h() else { continue };`
2929

3030
error: this could be rewritten as `let...else`
3131
--> $DIR/manual_let_else_match.rs:49:9
@@ -34,7 +34,7 @@ LL | / let v = match build_enum() {
3434
LL | | _ => continue,
3535
LL | | Variant::Bar(v) | Variant::Baz(v) => v,
3636
LL | | };
37-
| |__________^ help: consider writing: `let Variant::Bar(v) | Variant::Baz(v) = build_enum() else { continue };`
37+
| |__________^ help: consider writing: `let (Variant::Bar(v) | Variant::Baz(v)) = build_enum() else { continue };`
3838

3939
error: this could be rewritten as `let...else`
4040
--> $DIR/manual_let_else_match.rs:57:5
@@ -54,5 +54,14 @@ LL | | Err(()) => return,
5454
LL | | };
5555
| |______^ help: consider writing: `let Ok(v) = f().map_err(|_| ()) else { return };`
5656

57-
error: aborting due to 6 previous errors
57+
error: this could be rewritten as `let...else`
58+
--> $DIR/manual_let_else_match.rs:70:5
59+
|
60+
LL | / let _value = match f {
61+
LL | | Variant::Bar(_) | Variant::Baz(_) => (),
62+
LL | | _ => return,
63+
LL | | };
64+
| |______^ help: consider writing: `let (Variant::Bar(_) | Variant::Baz(_)) = f else { return };`
65+
66+
error: aborting due to 7 previous errors
5867

0 commit comments

Comments
 (0)