rust-lang / rust-lang/rustfmt

comma is removed from `#[cfg_attr(` which causes syntax error

Open
#7,058 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-attributes A-cfg C-bug I-invalid-code
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

this code compiles

#![allow(unused)]
#[cfg_attr(FALSE,)]
fn  _f()  {}

#[cfg_attr(not(FALSE),)]
fn  _g()  {}

fn  main()  {}

rustfmt will try to turn cfg_attr(x,) into cfg_attr(x) which does not compile

 #![allow(unused)]
-#[cfg_attr(FALSE,)]
-fn  _f()  {}
+#[cfg_attr(FALSE)]
+fn _f() {}

-#[cfg_attr(not(FALSE),)]
-fn  _g()  {}
+#[cfg_attr(not(FALSE))]
+fn _g() {}

-fn  main()  {}
+fn main() {}
#![allow(unused)]
#[cfg_attr(FALSE)]
fn _f() {}

#[cfg_attr(not(FALSE))]
fn _g() {}

fn main() {}
error: expected one of `(`, `,`, `::`, or `=`, found `<eof>`
 --> 330114539341046084715574591565618241811.rs:2:12
  |
2 | #[cfg_attr(FALSE)]
  |            ^^^^^ expected one of `(`, `,`, `::`, or `=`
  |
  = note: for more information, visit <https://doc.rust-lang.org/reference/conditional-compilation.html#the-cfg_attr-attribute>
help: must be of the form
  |
2 - #[cfg_attr(FALSE)]
2 + #[cfg_attr(predicate, attr1, attr2, ...)]
  |

error: expected `,`, found end of `cfg_attr` input
 --> 330114539341046084715574591565618241811.rs:5:22
  |
5 | #[cfg_attr(not(FALSE))]
  |                      ^ expected `,`
  |
  = note: for more information, visit <https://doc.rust-lang.org/reference/conditional-compilation.html#the-cfg_attr-attribute>
help: must be of the form
  |
5 - #[cfg_attr(not(FALSE))]
5 + #[cfg_attr(predicate, attr1, attr2, ...)]
  |

error: aborting due to 2 previous errors

rustfmt 1.10.0-nightly (fb6531d550 2026-08-23)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the Rust examples and the affected rustfmt version, then trace the rustfmt entry point that formats cfg_attr attributes. Done means formatting preserves the required trailing comma in cfg_attr(predicate,) and cfg_attr(predicate, attr1, ...) so the formatted examples still compile; no source file or test is named in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.