Sub topic 5
Finding problems with cargo clippy
This chapter assumes you can build a Cargo project and read basic Rust, including loops, vectors, and references. It covers running Clippy and reading its output. Lint groups, lint levels, and configuration come in later chapters.
What Clippy does
Clippy is a collection of lints (automated checks) for Rust code. It runs on top of the compiler, so it sees the same parsed program that rustc sees. It reports code that is probably a bug, code that is slower than it needs to be, and code that works but is not idiomatic.
rustfmt only changes layout. Clippy looks at meaning. A formatted program can still have many Clippy warnings.
Clippy ships as a rustup component. It is included in the default profile, but you can add it explicitly:
rustup component add clippyRunning Clippy
Run it from the root of a Cargo project:
cargo clippyClippy builds your crate the same way cargo check does, then prints the compiler's warnings together with its own. It does not produce a runnable binary.
By default, cargo clippy checks only your library and binary targets. Tests, examples, and benchmarks are skipped. To include them, add --all-targets:
cargo clippy --all-targetsA worked example
Create a new project with cargo new clippy-demo and replace src/main.rs with this code:
fn main() {
let v = vec![10, 20, 30];
for i in 0..v.len() {
println!("{}", v[i]);
}
}The program compiles and prints three numbers. Clippy still reports a problem with it, using the clippy::needless_range_loop lint. The index i is used only to read from v, so the loop does not need it.
Reading a diagnostic
Clippy output has the same shape as a compiler warning. It contains a level, a message, a source location, and usually a suggestion.
| Part | Meaning |
|---|---|
| `warning: the loop variable i is only used to index v` | The level and a short description of the problem |
| `--> src/main.rs:3:14` | File, line, and column of the code involved |
| `= help: for further information visit ...` | A link to the lint's documentation page |
| `= note: #[warn(clippy::needless_range_loop)] on by default` | The lint's name and why it fired |
| `help: consider using an iterator` | A suggested rewrite |
The lint name, here clippy::needless_range_loop, is the most useful part. You need it to look up the lint's documentation, and you need it later to change the lint's level.
The documentation page for each lint explains what the lint checks, why the pattern is a problem, and shows a "bad" and "good" example. Read it before you apply a suggestion.
Fixing the warning
The suggested rewrite iterates over the vector directly:
fn main() {
let v = vec![10, 20, 30];
for item in &v {
println!("{item}");
}
}This version has no index, so it cannot go out of bounds. It also lets the compiler skip bounds checks. Run cargo clippy again and the warning is gone.
Where Clippy fits in the workflow
Common mistakes
- Treating every suggestion as an order. Clippy gives advice. A few lints are subjective, and some suggestions make code less clear in a specific context. Read the lint's documentation and decide.
- Forgetting that Clippy caches results. If you run
cargo clippyon code that has not changed since the last run, Cargo may replay the earlier warnings from its cache. This is normal. Edit a file or runcargo clean -p your-crateto force a fresh check. - Expecting tests to be checked. Without
--all-targets, code under#[cfg(test)]is not linted. - Mixing up errors and warnings. Most Clippy lints are warnings, so the command still exits with status 0. A script cannot detect them from the exit code alone. The CI chapter shows how to turn warnings into failures.
Exercise
You run cargo clippy in a project and it prints no warnings, but you know your tests contain a loop like for i in 0..v.len(). Why was it not reported, and what command would report it?
Show answer
By default cargo clippy checks only the library and binary targets, so test code is not linted. Run cargo clippy --all-targets to include tests, examples, and benchmarks.
Exercise
In a Clippy diagnostic, which part do you need in order to look up a lint's documentation or later change its level?
Show answer
The lint name, such as clippy::needless_range_loop, which appears in the note line of the diagnostic.