LinuxCommandLibrary

cargo-clippy

Lint Rust code for common mistakes

TLDR

Run checks over the code in the current directory

$ cargo clippy
copy

Require that Cargo.lock is up to date
$ cargo clippy --locked
copy

Run checks on all packages in the workspace
$ cargo clippy --workspace
copy

Run checks for a package
$ cargo clippy --package [package]
copy

Run checks for a lint group (see )
$ cargo clippy -- [[-W|--warn]] clippy::[lint_group]
copy

Treat warnings as errors
$ cargo clippy -- [[-D|--deny]] warnings
copy

Run checks and ignore warnings
$ cargo clippy -- [[-A|--allow]] warnings
copy

Apply Clippy suggestions automatically
$ cargo clippy --fix
copy

SYNOPSIS

cargo clippy [OPTIONS] [--] [<PATH> ...]
Alias: cargo clippy [<SUBCOMMAND> ...]

PARAMETERS

--all
    Lint all packages (alias for --all-features --all-targets)

--all-features
    Activate all available features

--all-targets
    Lint all targets (lib/bin/tests/etc.)

-A, --allow <lints>
    Allow specified lints (comma-separated, e.g., clippy::style)

-D, --deny <lints>
    Deny specified lints (treat as errors)

-W, --warn <lints>
    Warn on specified lints

--fix
    Automatically apply lint fixes where possible

-p, --package <SPEC>...
    Package(s) to lint (e.g., foo:0.1)

--target <TRIPLE>
    Target triple to lint

-F, --forbid <lints>
    Forbid specified lints (errors, no fixes)

--
    Pass additional arguments to underlying rustc

DESCRIPTION

Cargo clippy is a powerful linter for Rust code, extending the compiler with hundreds of lints to detect common mistakes, improve code style, performance, and correctness.

It integrates seamlessly as a Cargo subcommand, allowing developers to check code during builds or CI pipelines. Lints are categorized (e.g., correctness, style, perf, pedantic) and can be configured via command-line flags, Cargo.toml profiles, or attributes.

Clippy catches subtle bugs like unnecessary unwraps, inefficient patterns, or suspicious API usage. It supports all Cargo features like workspaces, targets, and profiles. Output includes explanations and suggestions for fixes, making it essential for Rust best practices.

Usage is simple: run in a Rust project root to lint the entire crate or specify paths. It's stricter than cargo check but faster than full compilation.

CAVEATS

Requires clippy component installed (rustup component add clippy); nightly Rust for unstable lints (-Z unstable-options); may increase build time; fixes alter source code—review before committing.

INSTALLATION

Run rustup component add clippy to install; updates with rustup update.

CONFIGURATION

Use [lints.clippy] in Cargo.toml for per-profile rules, e.g., allow = ["clippy::collapsible_else_if"].

HISTORY

Clippy originated in 2015 as a Mozilla experiment by Oliver Schneider. Joined Rust repo in 2016, stabilized in Rust 1.29 (2018). Now maintained by Rust community with 600+ lints; regular updates via rustup.

SEE ALSO

cargo(1), rustup(1), rustc(1), cargo-check(1), cargo-fmt(1)

Copied to clipboard