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 dcc3059a7f44c..500d7d77071de 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -2290,7 +2290,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 802928452709d..9bc3e754126db 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