Enforce `rustfmt` only on files that were already correctly formatted?

I'm looking for a way to introduce rustfmt into a CI pipeline, without forcing a one-big rustfmt event.

One idea that I had was to ignore formatting errors if a given file was already not correctly formatted (before the change under test). This way there would something preventing formatting from getting worse, but in a slower and more gentle way.

Is anyone aware of an existing project like that? Or any other projects/ideas aiming at similar goal?

1 Like

You might be able to do a big-but-less-impactful version by putting #![rustfmt::skip] in all the files, and just removing those as they get formatted.

That still has the "git log shows that change on every file" problem, though.

I do something similar to "every module is documented" condition.

There's a poorly_documented list which stores all yet undocumented module. There's a test which checks ∀ m ∈ Modules: documented(m) == m ∉ poorly_documented. That is, the test fails when the new undocumented module is added, or when an old module gets documented. The poorly_documented list is maintained manually, and initially included almost all modules.

Note that this requires that you run rustfmt from a #[test] rather than as a separate CI step, but I believe this is a good idea regardless.

I'm wondering how is this achieved. Can I take a look at a source code?

rustfmt test: https://github.com/rust-analyzer/rust-analyzer/blob/a3e5dcc1778d5703a20e7f11cc0268026d121b73/xtask/tests/tidy.rs#L33-L38

docs check: https://github.com/rust-analyzer/rust-analyzer/blob/a3e5dcc1778d5703a20e7f11cc0268026d121b73/xtask/tests/tidy.rs#L266-L351

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.