Rust Reference ยท Tools
cargo clippy
Reference for cargo clippy: run the Rust linter, apply fixes, control lints with allow and deny, and see common warnings with before and after code.
cargo clippy checks your code like cargo check and then runs several hundred extra lints that look for bugs, slow patterns, needless complexity and non-idiomatic Rust. Each message names a lint, points at the code and usually suggests a replacement.
Lints are grouped by purpose: correctness (almost certainly a bug, deny by default), suspicious, style, complexity, perf, and the opt-in pedantic, nursery, restriction and cargo groups. Clippy ships as a rustup component. This page is a read-only command reference; the examples run the before and after versions of code that Clippy comments on.
$ cargo clippy $ cargo clippy --fix $ cargo clippy -- -D warnings
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Syntax
cargo clippy [cargo options] [-- <lint flags>]
| Name | Description |
|---|---|
--fix |
Apply machine-applicable suggestions (needs a clean Git tree unless --allow-dirty). |
--all-targets |
Lint tests, examples and benches as well as the library and binaries. |
--workspace |
Lint every workspace member. |
-- -D warnings |
Turn all warnings into errors (CI). |
-- -W clippy::pedantic |
Enable a lint group as warnings. |
-- -A clippy::lint_name |
Allow (silence) one lint. |
Exit status: non-zero when a lint is at deny level or when -D warnings catches anything.
Usage notes
- Control lints in code with
#[allow(clippy::name)],#[warn(...)],#[deny(...)]or#[expect(...)].expectcomplains if the lint no longer fires, which keeps suppressions honest. - Project-wide levels belong in the
[lints.clippy]table ofCargo.toml(stable since Rust 1.74), shared by workspaces with[workspace.lints]. - Do not enable
restrictionas a whole; its lints contradict each other. Pick individual ones such asclippy::unwrap_used. - Clippy lints have a stable name; new lints start in
nurseryorpedanticto avoid breaking builds on upgrade. - Some project settings (like the minimum supported Rust version) go in
clippy.toml; therust-versionfield inCargo.tomlis also honored. - Clippy runs on top of a normal build, so it uses the same cache; a first run after a toolchain update recompiles.
- A suggestion is advice, not law.
needless_pass_by_valueand friends sometimes make an API worse; allow them locally.
Related
- cargo fmt, cargo check, Cargo.toml
- Tutorial: Clippy and rustfmt