Clippy / rustfmt but for cargo.toml file?

Is there anything currently viable that is akin to Clippy and rustfmt but for the Cargo.toml file?

Some time ago, there was a project called cargo-toml-lint but it seems to have fallen into disrepair. (Last release was 3+ years ago.)

I'd love to see something that enforces a few common formatting rules and (IMHO) best practices for our Cargo.toml files. Some things I'd love to see:

  • Crates listed in alphabetical order.
  • TOML formatting is consistent (spaces inside { } pairs, no spaces inside [ ] pairs).
  • Top-level settings listed in a consistent order. (Perhaps same sequence as documentation.)
  • One blank line before new sections.

Is there such a thing?

Look into a project called taplo. It is a TOML formatter, inter and LSP.

1 Like

For linting manifest, Cargo has an unstable -Zcargo-lints system that sets up a good foundation of linting Cargo.toml and beyond: https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#lintscargo. We are looking forward to more lints. Here are the current supported lint rules.

For formatting manifest, there is an experimental tool you might be interested: https://crates.io/crates/cargo-cargofmt. The README cross-link to lots of prior arts, relevant tools, and discussions worth reading.

I personally like cargo-sort for this: GitHub - DevinR528/cargo-sort: Check if tables and items in a .toml file are lexically sorted
It puts all my dependencies in alphabetical order, which is the main thing I care about.

To expand on this, I'm intentionally developing cargo-cargofmt to

  • create a proposal for the official Cargo.toml style guide
  • be ready to integrate into cargo fmt

To this end, it tries to mirror rustfmts formatting behavior and reads rustfmt.toml. It doesn't have every rule yet (especially sorting) but we'll eventually get there.

It also calls cargo fmt so you only have one command you need to run.

See Existing formatters · crate-ci/cargo-cargofmt · Discussion #3 · GitHub

2 Likes