Skip to content

Commit

Permalink
Auto merge of rust-lang#9966 - alex-semenyuk:manual_let_else_paren, r…
Browse files Browse the repository at this point in the history
…=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)
  • Loading branch information
bors committed Dec 1, 2022
2 parents 3b8a10c + 05477ff commit 53e1b45
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
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

0 comments on commit 53e1b45

Please sign in to comment.