From e905b937060e3c920d85d1ae09d1e3a9a5f5a190 Mon Sep 17 00:00:00 2001 From: Edward Shen Date: Mon, 30 Jan 2023 14:09:40 -0800 Subject: [PATCH] Make the "extra if in let...else block" hint a suggestion --- compiler/rustc_parse/src/errors.rs | 2 +- compiler/rustc_parse/src/parser/expr.rs | 2 +- tests/ui/let-else/accidental-if.stderr | 6 +++--- ...e-that-let-else-does-not-interact-with-let-chains.stderr | 6 +++--- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_parse/src/errors.rs b/compiler/rustc_parse/src/errors.rs index 054b41b478d60..e63fc3c73b209 100644 --- a/compiler/rustc_parse/src/errors.rs +++ b/compiler/rustc_parse/src/errors.rs @@ -351,7 +351,7 @@ pub(crate) enum IfExpressionMissingThenBlockSub { } #[derive(Subdiagnostic)] -#[help(parse_extra_if_in_let_else)] +#[suggestion(parse_extra_if_in_let_else, applicability = "maybe-incorrect", code = "")] pub(crate) struct IfExpressionLetSomeSub { #[primary_span] pub if_span: Span, diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index 17d1e200b41aa..614cc31185599 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -2281,7 +2281,7 @@ impl<'a> Parser<'a> { block } else { let let_else_sub = matches!(cond.kind, ExprKind::Let(..)) - .then(|| IfExpressionLetSomeSub { if_span: lo }); + .then(|| IfExpressionLetSomeSub { if_span: lo.until(cond_span) }); self.sess.emit_err(IfExpressionMissingThenBlock { if_span: lo, diff --git a/tests/ui/let-else/accidental-if.stderr b/tests/ui/let-else/accidental-if.stderr index 5474a67aac45a..57e5259173078 100644 --- a/tests/ui/let-else/accidental-if.stderr +++ b/tests/ui/let-else/accidental-if.stderr @@ -10,10 +10,10 @@ help: add a block here LL | if let Some(y) = x else { | ^ help: remove the `if` if you meant to write a `let...else` statement - --> $DIR/accidental-if.rs:3:5 | -LL | if let Some(y) = x else { - | ^^ +LL - if let Some(y) = x else { +LL + let Some(y) = x else { + | error: aborting due to previous error diff --git a/tests/ui/rfc-2497-if-let-chains/ensure-that-let-else-does-not-interact-with-let-chains.stderr b/tests/ui/rfc-2497-if-let-chains/ensure-that-let-else-does-not-interact-with-let-chains.stderr index f34ccecdd45e6..e0e0eff8f71f9 100644 --- a/tests/ui/rfc-2497-if-let-chains/ensure-that-let-else-does-not-interact-with-let-chains.stderr +++ b/tests/ui/rfc-2497-if-let-chains/ensure-that-let-else-does-not-interact-with-let-chains.stderr @@ -38,10 +38,10 @@ help: add a block here LL | if let Some(n) = opt else { | ^ help: remove the `if` if you meant to write a `let...else` statement - --> $DIR/ensure-that-let-else-does-not-interact-with-let-chains.rs:24:5 | -LL | if let Some(n) = opt else { - | ^^ +LL - if let Some(n) = opt else { +LL + let Some(n) = opt else { + | error: this `if` expression is missing a block after the condition --> $DIR/ensure-that-let-else-does-not-interact-with-let-chains.rs:28:5