Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion clippy_lints/src/manual_let_else.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,12 @@ fn emit_manual_let_else(cx: &LateContext<'_>, span: Span, expr: &Expr<'_>, pat:
} else {
format!("{{ {sn_else} }}")
};
let sugg = format!("let {sn_pat} = {sn_expr} else {else_bl};");
let sn_bl = if matches!(pat.kind, PatKind::Or(..)) {
format!("({sn_pat})")
} else {
sn_pat.into_owned()
};
let sugg = format!("let {sn_bl} = {sn_expr} else {else_bl};");
diag.span_suggestion(span, "consider writing", sugg, app);
},
);
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/manual_let_else_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ fn fire() {
Ok(v) => v,
Err(()) => return,
};

let f = Variant::Bar(1);

let _value = match f {
Variant::Bar(_) | Variant::Baz(_) => (),
_ => return,
};
}

fn not_fire() {
Expand Down
15 changes: 12 additions & 3 deletions tests/ui/manual_let_else_match.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ LL | / let v = match h() {
LL | | (Some(_), Some(_)) | (None, None) => continue,
LL | | (Some(v), None) | (None, Some(v)) => v,
LL | | };
| |__________^ help: consider writing: `let (Some(v), None) | (None, Some(v)) = h() else { continue };`
| |__________^ help: consider writing: `let ((Some(v), None) | (None, Some(v))) = h() else { continue };`

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

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

error: aborting due to 6 previous errors
error: this could be rewritten as `let...else`
--> $DIR/manual_let_else_match.rs:70:5
|
LL | / let _value = match f {
LL | | Variant::Bar(_) | Variant::Baz(_) => (),
LL | | _ => return,
LL | | };
| |______^ help: consider writing: `let (Variant::Bar(_) | Variant::Baz(_)) = f else { return };`

error: aborting due to 7 previous errors