Skip to content

Commit c683145

Browse files
authored
Rollup merge of #163239 - chenyukang:yukang-fix-cfg-tail-expression-suggestion, r=estebank
Fix applicability level for cfg suggestion Mark the semicolon, block-wrapping, and if cfg! suggestions for attribute-bearing expressions as `MaybeIncorrect`. For example: ```rust fn native_file_attributes(metadata: &std::fs::Metadata) -> u32 { #[cfg(unix)] std::os::unix::fs::MetadataExt::mode(metadata) #[cfg(windows)] std::os::windows::fs::MetadataExt::file_attributes(metadata) } fn main() -> std::io::Result<()> { let metadata = std::fs::metadata(std::env::current_exe()?)?; println!( "native file attributes: {:#x}", native_file_attributes(&metadata) ); Ok(()) } ``` Adding ; discards the return value on Unix. Replacing #[cfg] with if cfg! also fails because disabled branches still undergo name resolution and type checking. Wrapping the expression in a block works here, but still can fail when multiple cfg branches are enabled and the first block returns a non-unit value. For example: ```rust fn value() -> u8 { #[cfg(all())] 1 #[cfg(all())] 2 } ``` r? @estebank
2 parents a88ba94 + 2ed1e37 commit c683145

2 files changed

Lines changed: 8 additions & 6 deletions

File tree

‎compiler/rustc_parse/src/diagnostics.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,8 +1656,8 @@ pub(crate) enum ExpectedSemiSugg {
16561656
style = "short"
16571657
)]
16581658
ChangeToSemi(#[primary_span] Span),
1659-
#[suggestion("add `;` here", code = ";", applicability = "machine-applicable", style = "short")]
1660-
AddSemi(#[primary_span] Span),
1659+
#[suggestion("add `;` here", code = ";", style = "short")]
1660+
AddSemi(#[primary_span] Span, #[applicability] Applicability),
16611661
}
16621662

16631663
#[derive(Diagnostic)]

‎compiler/rustc_parse/src/parser/diagnostics.rs‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ impl<'a> Parser<'a> {
439439
span,
440440
token: self.token,
441441
unexpected_token_label: Some(self.token.span),
442-
sugg: ExpectedSemiSugg::AddSemi(span),
442+
sugg: ExpectedSemiSugg::AddSemi(span, Applicability::MachineApplicable),
443443
});
444444
return Ok(guar);
445445
}
@@ -723,7 +723,8 @@ impl<'a> Parser<'a> {
723723
span,
724724
token: self.token,
725725
unexpected_token_label: Some(self.token.span),
726-
sugg: ExpectedSemiSugg::AddSemi(span),
726+
// A semicolon may discard the intended return value of a cfg-gated tail expression.
727+
sugg: ExpectedSemiSugg::AddSemi(span, Applicability::MaybeIncorrect),
727728
});
728729
let attr_span = match &expr.attrs[..] {
729730
[] => unreachable!(),
@@ -755,7 +756,7 @@ impl<'a> Parser<'a> {
755756
(expr.span.shrink_to_lo(), "{ ".to_string()),
756757
(expr.span.shrink_to_hi(), " }".to_string()),
757758
],
758-
Applicability::MachineApplicable,
759+
Applicability::MaybeIncorrect,
759760
);
760761

761762
// Special handling for `#[cfg(...)]` chains
@@ -811,7 +812,8 @@ impl<'a> Parser<'a> {
811812
"it seems like you are trying to provide different expressions depending on \
812813
`cfg`, consider using `if cfg!(..)`",
813814
sugg,
814-
Applicability::MachineApplicable,
815+
// Unlike `#[cfg]`, `if cfg!` still checks disabled branches.
816+
Applicability::MaybeIncorrect,
815817
);
816818
}
817819
}

0 commit comments

Comments
 (0)