diff --git a/compiler/rustc_mir_build/src/errors.rs b/compiler/rustc_mir_build/src/errors.rs index 87c69f60fd3..793d9ef18eb 100644 --- a/compiler/rustc_mir_build/src/errors.rs +++ b/compiler/rustc_mir_build/src/errors.rs @@ -1144,6 +1144,24 @@ pub(crate) enum SuggestEq<'tcx> { ty: Ty<'tcx>, manual_partialeq_impl: bool, }, + #[multipart_suggestion( + "{$manual_partialeq_impl -> + [false] if `{$ty}` manually implemented `PartialEq`, you could check + *[true] check + } for equality instead of pattern matching", + applicability = "maybe-incorrect", + style = "verbose" + )] + ReplaceLetElseWithIf { + #[suggestion_part(code = "if ")] + if_span: Span, + #[suggestion_part(code = " == ")] + eq: Span, + #[suggestion_part(code = " ")] + else_span: Span, + ty: Ty<'tcx>, + manual_partialeq_impl: bool, + }, } #[derive(Diagnostic)] diff --git a/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs b/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs index 2b9c3403b92..74d0d19c545 100644 --- a/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs +++ b/compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs @@ -283,6 +283,21 @@ impl<'tcx> ConstToPat<'tcx> { None } } + hir::Node::LetStmt(let_stmt) + if let Some(init) = let_stmt.init + && let Some(els) = let_stmt.els + && init.span.ctxt().is_root() + && els.span.ctxt().is_root() => + { + // `let PAT = expr else {` -> `if PAT == expr {`. + Some(SuggestEq::ReplaceLetElseWithIf { + if_span: let_stmt.span.until(let_stmt.pat.span), + eq: let_stmt.pat.span.between(init.span), + else_span: init.span.between(els.span), + ty, + manual_partialeq_impl, + }) + } _ => None, } } else { diff --git a/tests/ui/consts/const_in_pattern/suggest_equality_comparison_instead_of_pattern_matching.rs b/tests/ui/consts/const_in_pattern/suggest_equality_comparison_instead_of_pattern_matching.rs index f8ada6914fc..71a2d06adb7 100644 --- a/tests/ui/consts/const_in_pattern/suggest_equality_comparison_instead_of_pattern_matching.rs +++ b/tests/ui/consts/const_in_pattern/suggest_equality_comparison_instead_of_pattern_matching.rs @@ -26,7 +26,7 @@ mod partial_eq { } if let V = vec![] {} //~ ERROR: constant of non-structural type let V = vec![] else { return; }; //~ ERROR: constant of non-structural type - // ^ maybe suggest if V != vec![] { return; } ? + let V = Vec::new() else { return; }; //~ ERROR: constant of non-structural type } } diff --git a/tests/ui/consts/const_in_pattern/suggest_equality_comparison_instead_of_pattern_matching.stderr b/tests/ui/consts/const_in_pattern/suggest_equality_comparison_instead_of_pattern_matching.stderr index fc3af8104e5..38440af675f 100644 --- a/tests/ui/consts/const_in_pattern/suggest_equality_comparison_instead_of_pattern_matching.stderr +++ b/tests/ui/consts/const_in_pattern/suggest_equality_comparison_instead_of_pattern_matching.stderr @@ -107,6 +107,11 @@ note: the `PartialEq` trait must be derived, manual `impl`s are not sufficient; | LL | impl PartialEq for S { | ^^^^^^^^^^^^^^^^^^^^^^^ +help: check for equality instead of pattern matching + | +LL - let Some(C) = Some(S) else { return; }; +LL + if Some(C) == Some(S) { return; }; + | error: constant of non-structural type `Vec<()>` in a pattern --> $DIR/suggest_equality_comparison_instead_of_pattern_matching.rs:24:13 @@ -158,6 +163,26 @@ LL | let V = vec![] else { return; }; | = note: see https://doc.rust-lang.org/stable/std/marker/trait.StructuralPartialEq.html for details +error: constant of non-structural type `Vec<()>` in a pattern + --> $DIR/suggest_equality_comparison_instead_of_pattern_matching.rs:29:13 + | +LL | const V: Vec<()> = vec![]; + | ---------------- constant defined here +... +LL | let V = Vec::new() else { return; }; + | ^ constant of non-structural type + | + --> $SRC_DIR/alloc/src/vec/mod.rs:LL:COL + | + = note: `Vec<()>` is not usable in patterns + | + = note: see https://doc.rust-lang.org/stable/std/marker/trait.StructuralPartialEq.html for details +help: check for equality instead of pattern matching + | +LL - let V = Vec::new() else { return; }; +LL + if V == Vec::new() { return; }; + | + error: constant of non-structural type `not_partial_eq::S` in a pattern --> $DIR/suggest_equality_comparison_instead_of_pattern_matching.rs:40:18 | @@ -247,6 +272,11 @@ LL | let Some(C) = Some(S) else { return; }; | ^ constant of non-structural type | = note: see https://doc.rust-lang.org/stable/std/marker/trait.StructuralPartialEq.html for details +help: check for equality instead of pattern matching + | +LL - let Some(C) = Some(S) else { return; }; +LL + if Some(C) == Some(S) { return; }; + | -error: aborting due to 13 previous errors +error: aborting due to 14 previous errors