Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rustfmt breaks match arm starting with attributed block #4109

Closed
vallentin opened this issue Apr 7, 2020 · 0 comments · Fixed by #4124 or #5158
Closed

Rustfmt breaks match arm starting with attributed block #4109

vallentin opened this issue Apr 7, 2020 · 0 comments · Fixed by #4124 or #5158
Labels
bug Panic, non-idempotency, invalid code, etc.

Comments

@vallentin
Copy link
Contributor

vallentin commented Apr 7, 2020

If a match arm starts with a block with an attribute, rustfmt removes the brackets, resulting in code that cannot compile.

Before rustfmt:

match () {
        _ => {
            #[cfg(debug_assertions)]
            {
                println!("Foo");
            }
        }
    }

    match () {
        _ => {
            #[allow(unsafe_code)]
            unsafe {}
        }
    }

After rustfmt:

match () {
        _ =>
        #[cfg(debug_assertions)]
        {
            println!("Foo");
        }
    }

    match () {
        _ =>
            #[allow(unsafe_code)]
            unsafe {}
    }

Which results in the attributes being placed in invalid positions.

error[E0658]: attributes on expressions are experimental                                       
 --> src\main.rs:6:9
  |
6 |         #[cfg(debug_assertions)]
  |         ^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: for more information, see https://github.com/rust-lang/rust/issues/15701

error: removing an expression is not supported in this position
 --> src\main.rs:6:9
  |
6 |         #[cfg(debug_assertions)]
  |         ^^^^^^^^^^^^^^^^^^^^^^^^

error[E0658]: attributes on expressions are experimental
  --> src\main.rs:14:13
   |
14 |             #[allow(unsafe_code)]
   |             ^^^^^^^^^^^^^^^^^^^^^
   |
   = note: for more information, see https://github.com/rust-lang/rust/issues/15701

error: aborting due to 3 previous errors

I assume it's related to blocks, as the following code is not changed by rustfmt:

match () {
    _ => {
        #[cfg(debug_assertions)]
        println!("Foo");
    }
}

rustfmt 1.4.11-stable (9eb4b56 2020-01-29)
rustfmt 1.4.12-nightly (9f53665 2020-02-10)

@calebcartwright calebcartwright added the bug Panic, non-idempotency, invalid code, etc. label Apr 17, 2020
topecongiro added a commit to topecongiro/rustfmt that referenced this issue Apr 17, 2020
davidlattimore added a commit to davidlattimore/rustfmt that referenced this issue Dec 29, 2021
calebcartwright pushed a commit that referenced this issue Dec 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Panic, non-idempotency, invalid code, etc.
Projects
None yet
2 participants