Cargo Clippy: Your Opinionated Rust Code Reviewer
cargo clippy is an automated code reviewer that goes beyond the compiler, catching subtle bugs, performance issues, and style violations. Run it in CI to enforce idiomatic Rust.
WHY IT EXISTS: The Rust compiler is excellent at ensuring memory safety and correctness, but it doesn't enforce style, identify performance pitfalls, or catch logical errors that are syntactically valid. Clippy was created to fill this gap, acting as a linter to improve overall code quality beyond what the compiler guarantees.
THE MENTAL MODEL: Imagine an extremely experienced Rust developer doing a code review on every commit. They wouldn't just check if it compiles; they'd point out non-idiomatic patterns, suggest faster alternatives, and flag code that looks suspicious or is overly complex. cargo clippy automates this process.
HOW IT WORKS: Clippy is a collection of over 800 lints, organized into categories like correctness, suspicious, style, perf, and complexity. Each category has a default level: allow (do nothing), warn (show a warning), or deny (cause a compilation error). You run it with cargo clippy and can configure which lints or categories to apply. For example, correctness lints are deny by default because they catch code that is almost certainly wrong.
WHEN TO USE IT: Use Clippy constantly during local development for immediate feedback. It is also critical to integrate into your Continuous Integration (CI) pipeline. Running cargo clippy -- -D warnings in CI will treat all warnings as errors, failing the build and preventing low-quality code from being merged.
WHEN NOT TO USE IT: Avoid enabling entire lint categories without consideration, especially restriction and pedantic. The restriction category is designed for highly specific, opt-in rules, like forbidding floating-point arithmetic in a certain module or banning the use of unwrap(). Enabling it wholesale will likely flag perfectly reasonable code and cause frustration.
ONE CANONICAL EXAMPLE: A common Clippy suggestion is to replace a manual loop with a more idiomatic iterator. If you write for i in 0..vec.len() { ... }, Clippy will suggest the safer for item in &vec { ... }. Another example from the restriction category is clippy::unwrap_used, which can be enabled to forbid .unwrap() calls in production code to prevent panics.
Read the original → doc.rust-lang.org
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.