LinuxCommandLibrary

cargo-fix

Automatically fix Rust code based on suggestions

TLDR

Fix code even if it already has compiler errors

$ cargo fix --broken-code
copy

Fix code even if the working directory has changes
$ cargo fix --allow-dirty
copy

Migrate a package to the next Rust edition
$ cargo fix --edition
copy

Fix the package's library
$ cargo fix --lib
copy

Fix the specified integration test
$ cargo fix --test [name]
copy

Fix all members in the workspace
$ cargo fix --workspace
copy

SYNOPSIS

cargo fix [OPTIONS] [--] [<PATHS>...]

PARAMETERS

--allow-dirty
    Fix even if there are uncommitted changes

--allow-no-vcs
    Fix even without a .git directory

--allow-staged
    Allow staged files to be overwritten

--all-targets
    Fix all package targets (default)

--bench <NAME>
    Benchmark to fix

--benches
    Fix all benches

--bin <NAME>
    Binary to fix

--bins
    Fix all binaries

--broken-code
    Fix even non-compilable code

--compile-lib
    Compile only library, not full workspace

--dry-run
    Preview fixes without applying

--edition [<EDITION>]
    Fix for compatibility with specified edition

--edition-2021
    Equivalent to --edition 2021

--edition-2024
    Equivalent to --edition 2024

--example <NAME>
    Example to fix

--examples
    Fix all examples

--explain <OPT>
    Explain a specific lint error

--lib
    Fix library target

--manifest-path <PATH>
    Path to Cargo.toml

--package <SPEC>...
    Package(s) to fix

--test <NAME>
    Test to fix

--tests
    Fix all tests

--workspace
    Fix all workspace packages

-h, --help
    Print help

-Z <FLAG>...
    Unstable (nightly-only) flags

-V, --version
    Print version

DESCRIPTION

The cargo fix command is a Cargo subcommand that automatically applies fixes for lint warnings detected by Clippy, Rust's linter. It leverages the rustfix library to parse compiler diagnostic messages and rewrite source code with machine-applicable suggestions.

It is particularly useful for maintaining code hygiene, enforcing coding standards, and preparing Rust codebases for edition upgrades (e.g., 2018 to 2021 or 2024). By default, it targets the entire workspace or specified packages, compiling and fixing all targets unless restricted.

Usage involves running cargo fix in a Rust project directory, which stages fixes in Git if possible. It supports dry runs to preview changes and can handle dirty working directories with flags. Fixes are safe and reversible via version control, focusing only on diagnostics marked as fixable.

Ideal for CI/CD pipelines or pre-commit hooks to automate lint resolution without manual intervention.

CAVEATS

Requires Clippy installed (rustup component add clippy); fixes may alter semantics in rare cases; not all lints are fixable; nightly Rust needed for some edition features.

HOW IT WORKS

Compiles with Clippy, collects fixable diagnostics, applies rustfix patches sequentially, verifies compilation post-fix.

EDITION MODE

Use --edition to migrate deprecated syntax (e.g., try! to ?); run repeatedly until no changes.

HISTORY

Introduced in Cargo 0.27.0 (Rust 1.31, 2018) as experimental; stabilized shortly after. Evolved with Rust editions support in 1.56+ (2021 edition prep). Regularly updated with Cargo releases for new lints and rustfix improvements.

SEE ALSO

cargo(1), cargo-clippy(1), rustfix(1), clippy(1)

Copied to clipboard