Sub topic 12
Format on Save and Inline Lints in Your Editor
This chapter assumes you can run cargo fmt and cargo clippy from a terminal and know what rustfmt.toml and the [lints] table do. Here you move both tools into your editor, so problems show up while you type instead of in CI.
How the pieces fit
Editors do not run rustfmt or Clippy directly. They talk to rust-analyzer, a language server (a background program that answers editor requests about your code). rust-analyzer calls Cargo and rustfmt for you and sends the results back to the editor.
Both tools read the same configuration files as the command line: rustfmt.toml, clippy.toml, and the [lints] table in Cargo.toml. The editor and CI therefore agree, as long as they use the same toolchain.
Install the components
Make sure the toolchain has both components, then install the rust-analyzer extension for your editor.
rustup component add rustfmt clippy rust-analyzerIn VS Code, install the "rust-analyzer" extension. In Neovim, Helix, Zed, and Emacs, rust-analyzer is available through the editor's language server support. Most Rust setups in other editors follow the same pattern.
Format on save
rust-analyzer formats a file by calling rustfmt. Your editor decides when to ask for that. In VS Code, enable format on save for Rust files in .vscode/settings.json:
{
"[rust]": {
"editor.defaultFormatter": "rust-lang.rust-analyzer",
"editor.formatOnSave": true
}
}Committing this file gives everyone on the project the same behavior. Other editors have an equivalent option, usually called "format on save" or "auto-format".
Try it with the unformatted code from earlier in the tutorial:
fn main(){
let numbers=vec![1,2,3];
let total:i32=numbers.iter().sum();
println!("total = {}",total);
}Save the file. The editor rewrites it to the standard style without you running anything.
Inline Clippy lints
By default, rust-analyzer runs cargo check when you save. That reports compiler errors but not Clippy lints. To use Clippy instead, change the check command. In VS Code:
{
"rust-analyzer.check.command": "clippy"
}Older versions of rust-analyzer call this setting rust-analyzer.checkOnSave.command. If one name has no effect, try the other.
After this change, saving a file runs Clippy. Warnings appear as underlines in the code, as entries in the Problems panel, and as hover text. Many diagnostics offer a quick fix, which you can accept from the light bulb or code actions menu. This is the editor version of cargo clippy --fix.
To match CI, you can pass extra arguments:
{
"rust-analyzer.check.command": "clippy",
"rust-analyzer.check.allTargets": true
}allTargets includes tests, examples, and benchmarks, like --all-targets on the command line.
Common problems
- Format on save does nothing. Check that a Rust file is the active one and that the default formatter is set to rust-analyzer. Then confirm that
cargo fmtworks in a terminal. If the file has a syntax error, rustfmt cannot format it. - Editor output differs from CI. The two are probably using different toolchains. A
rust-toolchain.tomlfile in the project root pins the version for both. Config files in a different directory than you expect can also change the result. - No Clippy warnings appear. Confirm the check command is
clippy, then save the file to trigger a run. The first run on a large project can be slow. - Slow editing on large projects. Clippy runs a full check on each save. If that is too slow, keep
cargo checkin the editor and rely on CI for Clippy.
Exercise
Your editor shows no Clippy warnings, but cargo clippy in the terminal reports several. Which rust-analyzer setting do you check first, and what should its value be?
Show answer
Check the check command setting, rust-analyzer.check.command (older versions: rust-analyzer.checkOnSave.command). It should be clippy, because the default is check, which reports compiler errors but not Clippy lints.
Exercise
Format on save works for you but not for a teammate on the same repository. Name two things you could commit to the repository so that both of you get the same result.
Show answer
Commit .vscode/settings.json with the Rust default formatter and editor.formatOnSave set, and commit rust-toolchain.toml so both use the same toolchain. Committing rustfmt.toml also keeps the style settings the same.