Skip to content

Fix applicability level for cfg suggestion - #163239

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
chenyukang:yukang-fix-cfg-tail-expression-suggestion
Sep 25, 2026
Merged

rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
chenyukang:yukang-fix-cfg-tail-expression-suggestion

Conversation

@chenyukang

Copy link
Copy Markdown
Member

Mark the semicolon, block-wrapping, and if cfg! suggestions for attribute-bearing expressions as MaybeIncorrect.

For example:

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:

fn value() -> u8 {
    #[cfg(all())]
    1

    #[cfg(all())]
    2
}

r? @estebank

@rustbot

rustbot commented Sep 24, 2026

Copy link
Copy Markdown
Collaborator

The parser was modified, potentially altering the grammar of (stable) Rust
which would be a breaking change.

cc @fmease

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 24, 2026
@estebank

Copy link
Copy Markdown
Contributor

@bors r+

@rust-bors

rust-bors Bot commented Sep 24, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 2ed1e37 has been approved by estebank

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 24, 2026
rust-bors Bot pushed a commit that referenced this pull request Sep 25, 2026
Rollup of 7 pull requests

Successful merges:

 - #160068 (Preserve explicit register names in inline asm diagnostics)
 - #163239 (Fix applicability level for cfg suggestion)
 - #163025 (make `Complex` ABI GCC-compatible on sparc)
 - #163076 (powerpc64: don't pass unions in FPRs on ELFv1)
 - #163268 ([rustdoc] Correctly check that an item is not `doc(hidden)` with `--generate-link-to-definition`)
 - #163288 (Tweak Layout::from_size_align_unchecked safety comment)
 - #163292 (moving div_ceil() to associated items macro)
@rust-bors
rust-bors Bot merged commit c683145 into rust-lang:main Sep 25, 2026
13 checks passed
@rustbot rustbot added this to the 1.100.0 milestone Sep 25, 2026
rust-bors Bot pushed a commit that referenced this pull request Sep 25, 2026
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants