From 2ed1e372dae36015873ba21a8ab6e8252d442af1 Mon Sep 17 00:00:00 2001 From: Yukang Date: Thu, 24 Sep 2026 10:49:27 +0800 Subject: [PATCH] Fix applicability level for cfg suggestion --- compiler/rustc_parse/src/diagnostics.rs | 4 ++-- compiler/rustc_parse/src/parser/diagnostics.rs | 10 ++++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/compiler/rustc_parse/src/diagnostics.rs b/compiler/rustc_parse/src/diagnostics.rs index c080fc9e95df7..54d91eca01327 100644 --- a/compiler/rustc_parse/src/diagnostics.rs +++ b/compiler/rustc_parse/src/diagnostics.rs @@ -1656,8 +1656,8 @@ pub(crate) enum ExpectedSemiSugg { style = "short" )] ChangeToSemi(#[primary_span] Span), - #[suggestion("add `;` here", code = ";", applicability = "machine-applicable", style = "short")] - AddSemi(#[primary_span] Span), + #[suggestion("add `;` here", code = ";", style = "short")] + AddSemi(#[primary_span] Span, #[applicability] Applicability), } #[derive(Diagnostic)] diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index 928518c88fec6..5f2052dc48e7b 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -439,7 +439,7 @@ impl<'a> Parser<'a> { span, token: self.token, unexpected_token_label: Some(self.token.span), - sugg: ExpectedSemiSugg::AddSemi(span), + sugg: ExpectedSemiSugg::AddSemi(span, Applicability::MachineApplicable), }); return Ok(guar); } @@ -723,7 +723,8 @@ impl<'a> Parser<'a> { span, token: self.token, unexpected_token_label: Some(self.token.span), - sugg: ExpectedSemiSugg::AddSemi(span), + // A semicolon may discard the intended return value of a cfg-gated tail expression. + sugg: ExpectedSemiSugg::AddSemi(span, Applicability::MaybeIncorrect), }); let attr_span = match &expr.attrs[..] { [] => unreachable!(), @@ -755,7 +756,7 @@ impl<'a> Parser<'a> { (expr.span.shrink_to_lo(), "{ ".to_string()), (expr.span.shrink_to_hi(), " }".to_string()), ], - Applicability::MachineApplicable, + Applicability::MaybeIncorrect, ); // Special handling for `#[cfg(...)]` chains @@ -811,7 +812,8 @@ impl<'a> Parser<'a> { "it seems like you are trying to provide different expressions depending on \ `cfg`, consider using `if cfg!(..)`", sugg, - Applicability::MachineApplicable, + // Unlike `#[cfg]`, `if cfg!` still checks disabled branches. + Applicability::MaybeIncorrect, ); } }