Fix manual_let_else produces a wrong suggestion with or-patterns

This commit is contained in:
alex-semenyuk 2022-11-26 19:21:07 +03:00
parent d822110d3b
commit 05477ff8df
3 changed files with 25 additions and 4 deletions

View File

@ -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);
},
);

View File

@ -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() {

View File

@ -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
@ -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
@ -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