Can Rust's clippy do autocorrection / autofix?

11.5k Views Asked by At

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.

2

There are 2 best solutions below

3
On

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
0
On

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

cargo clippy --fix