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] [-- rustc/clippy_args...]

Common invocation examples:
cargo clippy
cargo clippy --fix
cargo clippy -- -D warnings
cargo clippy -p my_crate --target x86_64-unknown-linux-gnu

PARAMETERS

--package , -p
    Run clippy only for the specified package(s).

--workspace
    Run clippy for all packages in the current workspace.

--all-targets
    Check all targets (library, binaries, tests, examples, benchmarks).

--bin
    Check only the specified binary target.

--lib
    Check only the library target.

--example
    Check only the specified example target.

--test
    Check only the specified test target.

--bench
    Check only the specified benchmark target.

--features
    Space-separated list of features to activate.

--all-features
    Activate all available features.

--no-default-features
    Do not activate the default features.

--target
    Build for the specified target triple (e.g., x86_64-unknown-linux-gnu).

--release
    Build artifacts in release mode, with optimizations.

--profile
    Build artifacts with the specified profile.

--fix
    Automatically fix code when possible based on clippy suggestions.

--verbose, -v
    Use verbose output.

--quiet, -q
    Do not print cargo log messages.

--message-format
    Output messages in specified format (e.g., human, json, json-render-diagnostics, short).

-- ...
    Arguments following -- are passed directly to the underlying rustc and clippy invocation. This is commonly used for setting lint levels (e.g., -D warnings to treat all warnings as errors, or -D clippy::unwrap_used to deny specific lints).

DESCRIPTION

cargo-clippy is a powerful linting tool for the Rust programming language that extends the analysis capabilities of the standard Rust compiler (rustc). Unlike rustc, which focuses on syntax and type correctness, clippy performs deeper static analysis to identify common programming mistakes, stylistic deviations, potential bugs, and sub-optimal code patterns. It offers actionable suggestions to improve code quality, readability, and performance. clippy includes a vast collection of lints, which are highly configurable and categorized by severity (e.g., warn, deny). It integrates seamlessly with the Cargo build system, making it easy to incorporate into development workflows and CI/CD pipelines. Utilizing clippy helps enforce best practices, reduce technical debt, and elevate the overall standard of Rust code in projects.

CAVEATS

False Positives: While clippy is highly effective, it can occasionally flag code that is intentionally written in a specific way, leading to false positives. These can usually be suppressed using #[allow(clippy::lint_name)] attributes on the specific code.
Performance: Running clippy can be noticeably slower than a standard cargo build due to the extensive static analysis it performs. For quicker feedback, it's often integrated into CI pipelines or run selectively rather than on every minor code change.
Configuration Complexity: With hundreds of lints, configuring clippy to precisely suit a project's needs can initially seem daunting. However, its powerful customization options via clippy.toml or Cargo.toml allow for fine-grained control.

CONFIGURATION

cargo-clippy's behavior and lint settings can be extensively customized. The primary methods include:
1. A clippy.toml file placed in the project root directory.
2. Adding a [lints.clippy] section directly to your project's Cargo.toml file.
These configuration files allow developers to adjust the severity levels of specific lints, disable lints, or configure thresholds for certain checks (e.g., maximum function complexity, maximum number of arguments).

LINT LEVELS

Lints in clippy can be assigned various levels, determining how they are reported:
allow: The lint is ignored.
warn: The lint is reported as a warning.
deny: The lint is treated as an error, causing the build to fail.
forbid: Similar to deny, but also prevents overriding the lint level with #[allow(...)] attributes.
These levels can be set project-wide in configuration files or specified on the command line using the -- separator (e.g., cargo clippy -- -D clippy::unwrap_used).

HISTORY

cargo-clippy originated as an independent community-driven project named rust-clippy. Its significant utility and widespread adoption led to its official integration into the main Rust project repository (rust-lang/rust) in 2017. This integration solidified clippy's role as the official linter for the Rust programming language. Since then, it has been maintained and developed in tandem with the Rust compiler itself, ensuring continuous compatibility with new Rust versions and language features. Its consistent evolution has made it an indispensable tool for Rust developers striving for high-quality, idiomatic code.

SEE ALSO

cargo(1), rustc(1), rustfmt(1)

Copied to clipboard