18

The official way to get Rustfmt to skip an item is #[rustfmt::skip], however I want it to skip an entire file. I tried this:

#![rustfmt::skip]

However you get this error

error[E0658]: non-builtin inner attributes are unstable

Here is the issue for that error.

Is there a workaround for this? Solutions I am not interested in:

  • Use unstable Rust
  • Tell Rustfmt not to process the file using something external to the file (rustfmt.toml for example)
Timmmm
  • 88,195
  • 71
  • 364
  • 509

2 Answers2

19

Currently Rustfmt traverses the mod tree itself so you can put the attribute on the file that declared the module you want to ignore.

#[rustfmt::skip]
mod dont_format_this_file;

Then dont_format_this_file.rs will be skipped.

However, Rustfmt 2 changed its behaviour so that it doesn't traverse the mod tree, so you have to specify dont_format_this_file.rs directly. In that case it seems likely that it will find the mod dont_format_this_file; declaration so this may not work when Rustfmt 2 is released.

Timmmm
  • 88,195
  • 71
  • 364
  • 509
3

To skip an entire file you can add this to the top of the file:

#![cfg_attr(rustfmt, rustfmt_skip)]

This attribute is outlined in the tips section of the rustfmt nightly docs. It just worked in my case but I'm unsure if thats because I'm using nightly or something.

chantey
  • 4,252
  • 1
  • 35
  • 40