rust-analyzer VSCode extension, since it seems to be the best one out there for Rust (as of 2022).I have the following two entries in my "settings.json" for VSCode:
{
"rust-analyzer.checkOnSave.command": "clippy",
"[rust]": {
"editor.formatOnSave": true,
},
}
clippy (the linter) runs automatically upon pressing Ctrl + s to save the file.rustfmt (the formatter) runs automatically upon pressing Ctrl + s to save the file.In addition to these two things, I also want cargo fix to run automatically upon pressing Ctrl + s to save the file, because doing that automatically cleans up unused imports.
How can I accomplish this?
(I want unused imports to be automatically cleaned up for the same reason that I want an automatic formatter. Manually removing unused imports is tedium and a complete waste of my time in the same way that manually adding the appropriate amount of tabs or spaces is.)
Clippy has a --fix option, that applies the suggested fixes automatically. So all you need is to change the the on-save check command. However, you need two arguments clippy and --fix, and for that you cannot use rust-analyzer.check.command and have to override the full command:
{
"rust-analyzer.check.overrideCommand": [
"cargo",
"clippy",
"--fix",
"--workspace",
"--message-format=json",
"--all-targets",
"--allow-dirty"
],
}
Beware, however, that it will apply all automatically-applicable suggestions, not just remove unused imports.
All of these flags are what rust-analyzer automatically adds to rust-analyzer.check.command, but when you use rust-analyzer.check.overrideCommand you have to specify them yourself. Here's a short description of them (the docs has more details):
--workspace - apply the command to all members in the Cargo workspace, and not just one package.--message-format=json - emit JSON as a response instead of human-readable output, so that rust-analyzer can analyze the response and show the errors.--all-targets - check all things, i.e. binaries, libraries, examples, tests and benchmarks.--allow-dirty - fix even if workspace has changes.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With