Skip to content

Commit 0a73668

Browse files
committed
manual_let_else: only add () around PatKind::Or at the top level
At the top level, () are required, but on the levels below they are not.
1 parent 652b4c7 commit 0a73668

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

clippy_lints/src/manual_let_else.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ fn emit_manual_let_else(
159159
} else {
160160
format!("{{ {sn_else} }}")
161161
};
162-
let sn_bl = replace_in_pattern(cx, span, local, pat, &mut app);
162+
let sn_bl = replace_in_pattern(cx, span, local, pat, &mut app, true);
163163
let sugg = format!("let {sn_bl} = {sn_expr} else {else_bl};");
164164
diag.span_suggestion(span, "consider writing", sugg, app);
165165
},
@@ -173,6 +173,7 @@ fn replace_in_pattern(
173173
local: &Pat<'_>,
174174
pat: &Pat<'_>,
175175
app: &mut Applicability,
176+
top_level: bool,
176177
) -> String {
177178
let mut bindings_count = 0;
178179
pat.each_binding_or_first(&mut |_, _, _, _| bindings_count += 1);
@@ -191,16 +192,20 @@ fn replace_in_pattern(
191192
PatKind::Or(pats) => {
192193
let patterns = pats
193194
.iter()
194-
.map(|pat| replace_in_pattern(cx, span, local, pat, app))
195+
.map(|pat| replace_in_pattern(cx, span, local, pat, app, false))
195196
.collect::<Vec<_>>();
196197
let or_pat = patterns.join(" | ");
197-
return format!("({or_pat})");
198+
if top_level {
199+
return format!("({or_pat})");
200+
} else {
201+
return or_pat;
202+
}
198203
},
199204
// Replace the variable name iff `TupleStruct` has one argument like `Variant(v)`.
200205
PatKind::TupleStruct(ref w, args, dot_dot_pos) => {
201206
let mut args = args
202207
.iter()
203-
.map(|pat| replace_in_pattern(cx, span, local, pat, app))
208+
.map(|pat| replace_in_pattern(cx, span, local, pat, app, false))
204209
.collect::<Vec<_>>();
205210
if let Some(pos) = dot_dot_pos.as_opt_usize() {
206211
args.insert(pos, "..".to_owned());

tests/ui/manual_let_else_match.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ fn fire() {
7272
_ => return,
7373
};
7474

75+
let _value = match Some(build_enum()) {
76+
Some(Variant::Bar(v) | Variant::Baz(v)) => v,
77+
_ => return,
78+
};
79+
7580
let data = [1_u8, 2, 3, 4, 0, 0, 0, 0];
7681
let data = match data.as_slice() {
7782
[data @ .., 0, 0, 0, 0] | [data @ .., 0, 0] | [data @ .., 0] => data,

tests/ui/manual_let_else_match.stderr

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,22 @@ LL | | };
6464
| |______^ help: consider writing: `let (Variant::Bar(_value) | Variant::Baz(_value)) = f else { return };`
6565

6666
error: this could be rewritten as `let...else`
67-
--> $DIR/manual_let_else_match.rs:76:5
67+
--> $DIR/manual_let_else_match.rs:75:5
68+
|
69+
LL | / let _value = match Some(build_enum()) {
70+
LL | | Some(Variant::Bar(v) | Variant::Baz(v)) => v,
71+
LL | | _ => return,
72+
LL | | };
73+
| |______^ help: consider writing: `let Some(Variant::Bar(_value) | Variant::Baz(_value)) = Some(build_enum()) else { return };`
74+
75+
error: this could be rewritten as `let...else`
76+
--> $DIR/manual_let_else_match.rs:81:5
6877
|
6978
LL | / let data = match data.as_slice() {
7079
LL | | [data @ .., 0, 0, 0, 0] | [data @ .., 0, 0] | [data @ .., 0] => data,
7180
LL | | _ => return,
7281
LL | | };
7382
| |______^ help: consider writing: `let ([data @ .., 0, 0, 0, 0] | [data @ .., 0, 0] | [data @ .., 0]) = data.as_slice() else { return };`
7483

75-
error: aborting due to 8 previous errors
84+
error: aborting due to 9 previous errors
7685

0 commit comments

Comments
 (0)