Introduction
Clippy is the official lint tool for Rust, shipped as a rustup component. It analyzes your code for common mistakes, style issues, and performance pitfalls, providing actionable suggestions that help both beginners and experienced Rust developers write more idiomatic and correct code.
What Clippy Does
- Provides over 700 lints organized into categories: correctness, style, complexity, performance, and pedantic
- Catches common bugs like unused results, redundant clones, and incorrect iterator usage
- Suggests idiomatic Rust patterns and simplifications for verbose code
- Integrates with cargo as a first-class subcommand via
cargo clippy - Supports auto-fix for many lints using
cargo clippy --fix
Architecture Overview
Clippy is built as a compiler plugin that hooks into the Rust compiler's analysis passes. It runs after type checking and borrow checking, giving it access to the full type information and HIR (High-level Intermediate Representation). Each lint is implemented as a separate pass that walks the syntax tree looking for specific patterns. Clippy ships with each Rust toolchain release and is kept in sync with compiler internals.
Self-Hosting & Configuration
- Install via
rustup component add clippyon any Rust toolchain - Run
cargo clippyin your project root to lint the entire workspace - Configure lint levels in
Cargo.tomlunder[lints.clippy]or via#[allow()]and#[deny()]attributes - Use
cargo clippy --fixto automatically apply safe suggestions - Add to CI with
cargo clippy -- -D warningsto fail the build on any lint warning
Key Features
- Official Rust project tool with guaranteed compatibility with each compiler release
- Categories from permissive (style) to strict (correctness, nursery) for gradual adoption
- Machine-applicable suggestions that integrate with
cargo fix - Works on individual crates or entire cargo workspaces including dependencies
- Extensive documentation with explanations and examples for every lint
Comparison with Similar Tools
- rustfmt — formats code style; Clippy analyzes logic and patterns rather than whitespace
- rust-analyzer — provides IDE features and some quick-fixes; Clippy offers deeper lint coverage
- cargo-audit — scans for security vulnerabilities in dependencies; Clippy focuses on code quality
- Miri — detects undefined behavior at runtime; Clippy catches issues statically at compile time
- dylint — allows writing custom Rust lints; Clippy provides the standard curated set
FAQ
Q: Does Clippy produce false positives?
A: Rarely. Most lints are well-tested, and you can suppress individual warnings with #[allow(clippy::lint_name)] when needed.
Q: Can I run Clippy only on changed files? A: Clippy respects cargo's incremental compilation, so only modified crates are re-checked. For CI, tools like cargo-nextest can parallelize.
Q: How do I enable stricter lints like pedantic?
A: Add #![warn(clippy::pedantic)] to your crate root or configure it in Cargo.toml under [lints.clippy].
Q: Is Clippy required for publishing to crates.io? A: No, but many open-source Rust projects enforce Clippy in CI as a quality gate before merging.