25

Is it possible to run cargo clippy with an option so it will fix warnings automatically?

From the help message, it does not look like this option is supported at the moment.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Sergey Potapov
  • 3,819
  • 3
  • 27
  • 46

2 Answers2

26

As of June 2021 the autofix capability has been stabilized, you can apply changes using the following command

cargo clippy --fix
Mitch
  • 3,342
  • 2
  • 21
  • 31
14

cargo fix can already apply some suggestions deriving from rustc's errors and warnings.

In nightly builds you can use cargo clippy --fix to apply some suggestions from Clippy. In some older Rust versions, the syntax is reversed: cargo fix --clippy.

If you are using a stable version of the Rust toolchain, you can opt-in to use a nightly build for just one command, by using +nightly to override the toolchain:

cargo +nightly clippy --fix -Z unstable-options
Peter Hall
  • 53,120
  • 14
  • 139
  • 204
  • 2
    doesn't work currently https://github.com/rust-lang/cargo/pull/7533 – Stargateur Apr 15 '20 at 02:47
  • 3
    `cargo-fix --clippy` has been removed. Work is underway to re-add it as `cargo-clippy --fix`: https://github.com/rust-lang/rust-clippy/pull/5363, but it is not yet merged. – Peter Hall Apr 15 '20 at 13:03
  • 5
    First you need to install clippy nightly (`rustup component add clippy --toolchain nightly-x86_64-unknown-linux-gnu`), and after that run it with: `cargo +nightly clippy --fix -Z unstable-options` – Patrick José Pereira Nov 04 '20 at 16:57