Skip to content

Rustfmt adds extra bracket after cfg feature, breaking build #4630

Closed
@avanhatt

Description

@avanhatt

Running rustfmt on this snippet of code:

pub const fn vector_width() -> usize {
    #[cfg(feature="vec_width_2")]
    { 2 }
    // Default
    #[cfg(not(feature="vec_width_2"))]
    { 4 }
}

Rewrites it to the following, inserting two additional opening brackets {:

pub const fn vector_width() -> usize {
    #[cfg(feature = "vec_width_2")]
    {
        {        2
    }
    // Default
    #[cfg(not(feature = "vec_width_2"))]
    {
        {        4
    }
}

To Reproduce

For a minimal working example, I created a new lib project with the following lib.rs file (gist):

pub const fn vector_width() -> usize {
    #[cfg(feature="vec_width_2")]
    { 2 }
    // Default
    #[cfg(not(feature="vec_width_2"))]
    { 4 }
}

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        assert_eq!(2 + 2, 4);
    }
}

This project builds before cargo fmt but fails to build after:

fmt-bracket-bug (master) $ cargo build
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
fmt-bracket-bug (master) $ cargo fmt
fmt-bracket-bug (master) $ cargo build
   Compiling fmt-bracket-bug v0.1.0 (/Users/avh/research/fmt-bracket-bug)
error: this file contains an unclosed delimiter
  --> src/lib.rs:19:3
   |
1  | pub const fn vector_width() -> usize {
   |                                      - unclosed delimiter
2  |     #[cfg(feature = "vec_width_2")]
3  |     {
   |     - unclosed delimiter
4  |         {        2
   |         - this delimiter might not be properly closed...
5  |     }
   |     - ...as it matches this but it has different indentation
...
19 | }
   |   ^

error[E0308]: mismatched types
 --> src/lib.rs:1:32
  |
1 | pub const fn vector_width() -> usize {
  |              ------------      ^^^^^ expected `usize`, found `()`
  |              |
  |              implicitly returns `()` as its body has no tail or `return` expression

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0308`.
error: could not compile `fmt-bracket-bug`

To learn more, run the command again with --verbose.

The following is the diff after cargo fmt

fmt-bracket-bug (master) $ git diff
diff --git a/src/lib.rs b/src/lib.rs
index d4cc40e..a790f92 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,9 +1,13 @@
 pub const fn vector_width() -> usize {
-    #[cfg(feature="vec_width_2")]
-    { 2 }
+    #[cfg(feature = "vec_width_2")]
+    {
+        {        2
+    }
     // Default
-    #[cfg(not(feature="vec_width_2"))]
-    { 4 }
+    #[cfg(not(feature = "vec_width_2"))]
+    {
+        {        4
+    }
 }

Expected behavior

Rewrites for whitespace, without additional brackets:

pub const fn vector_width() -> usize {
    #[cfg(feature = "vec_width_2")]
    {
        2
    }
    // Default
    #[cfg(not(feature = "vec_width_2"))]
    {
        4
    }
}

Meta

  • rustfmt version: rustfmt 1.4.25-stable (0f29ff6 2020-11-11)
  • From where did you install rustfmt?: rustup
  • How do you run rustfmt: cargo fmt

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugPanic, non-idempotency, invalid code, etc.duplicate

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions