Clippy adds Rust-specific linting on top of the compiler so review can catch suspicious patterns, needless complexity, and style issues before they reach a branch or CI job. Running it through Cargo keeps the check tied to the same package or workspace manifest that developers already use for builds and tests.
A rustup-managed toolchain usually includes Clippy, but minimal profiles may need the clippy component installed for the active toolchain. cargo clippy runs the default lint set from clippy::all, and arguments after -- are passed through to Clippy and rustc lint controls.
Treat warnings as local feedback first, then make them fail in review or CI once the project is ready to enforce them. Use narrow source-level allowances such as #[allow(clippy::lint_name)] only for intentional exceptions so future warnings remain visible.
Related: How to check Rust code with Cargo
Related: How to format Rust code with rustfmt
The directory should contain the Cargo.toml file for the package, or the root manifest for the workspace.
$ rustup component add clippy info: component clippy is up to date
$ cargo clippy
Checking hello-clippy v0.1.0 (/home/user/hello-clippy)
warning: length comparison to zero
--> src/main.rs:4:8
|
4 | if name.len() > 0 {
| ^^^^^^^^^^^^^^ help: using `!is_empty` is clearer and more explicit: `!name.is_empty()`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.96.0/index.html#len_zero
= note: `#[warn(clippy::len_zero)]` on by default
warning: `hello-clippy` (bin "hello-clippy") generated 1 warning (run `cargo clippy --fix --bin "hello-clippy" -p hello-clippy -- ` to apply 1 suggestion)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.11s
cargo clippy exits successfully for ordinary warnings. Read the first file and line before chasing later warnings, because one issue can trigger related diagnostics.
if !name.is_empty() { println!("{name}"); }
Prefer changing the code when the suggestion matches the project style. Use #[allow(clippy::len_zero)] on the smallest item, function, or module only when the warning is intentionally accepted.
$ cargo clippy --all-targets -- -D warnings
Checking hello-clippy v0.1.0 (/home/user/hello-clippy)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.04s
--all-targets includes tests, benches, examples, library targets, and binaries. Add --all-features, --features, or --no-default-features when the project uses feature combinations that CI also checks.