Skip to content

“attributes on expressions are experimental” error should be more accurate and helpful #147853

@kpreid

Description

@kpreid

Code

fn example(x: bool) {
    #[rustfmt::skip]
    if x { print!("yea"); } else { print!("nay"); }
    
    println!(".");
}

Current output

error[E0658]: attributes on expressions are experimental
 --> src/lib.rs:2:5
  |
2 |     #[rustfmt::skip]
  |     ^^^^^^^^^^^^^^^^

Desired output

error[E0658]: attributes on some kinds of expressions are experimental
 --> src/lib.rs:2:5
  |
2 |     #[rustfmt::skip]
  |     ^^^^^^^^^^^^^^^^
  = help: this attribute will be allowed if the expression statement ends with a semicolon:
  |     if x { print!("yea"); } else { print!("nay"); };
  |                                                    +

Rationale and extra context

There are two problems with this diagnostic:

  • The claim is significantly false; it is not the case that all attributes on all expressions are unstable. (For example, the above code would compile if rustfmt::skip was replaced by allow(deprecated), or if the expression was a function call.)
  • There are simple but unintuitive workarounds available which do not require substantially refactoring the code, such as:
    • Adding a semicolon to an expression-with-block statement
    • Parenthesizing an assignment (surprisingly only works for some attributes, such as allow)

Rust Version

rustc 1.90.0 (1159e78c4 2025-09-14)
binary: rustc
commit-hash: 1159e78c4747b02ef996e55082b704c09b970588
commit-date: 2025-09-14
host: aarch64-apple-darwin
release: 1.90.0
LLVM version: 20.1.8

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-attributesArea: Attributes (`#[…]`, `#![…]`)A-diagnosticsArea: Messages for errors, warnings, and lintsD-lack-of-suggestionDiagnostics: Adding a (structured) suggestion would increase the quality of the diagnostic.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions