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]

PARAMETERS

--edition
    Fixes warnings and errors specifically related to the current Rust edition.

--prepare-for
    Applies fixes to prepare the code for migration to a specified future Rust edition.

--all-features
    Activates all available features of the crate being fixed.

--no-default-features
    Prevents activation of the default feature of the crate.

--features
    Specifies a space-separated list of features to activate during the fix process.

--workspace
    Applies fixes to all packages within the current Cargo workspace.

-p ...
    Fixes only the specified packages by their name or path.

--lib
    Fixes only the library target of the current package.

--bin
    Fixes only the specified binary target of the current package.

--target
    Fixes code for a specific compilation target triple (e.g., x86_64-unknown-linux-gnu).

-v
    Enables verbose output, showing more details about the fix process.

DESCRIPTION

cargo fix is a powerful subcommand of the Rust package manager, Cargo, designed to automatically apply common compiler suggestions to fix code. Its primary purpose is to help Rust developers migrate code to newer Rust editions, fix deprecated syntax, or apply automatic suggestions from compiler lints (e.g., from rustc or Clippy).

The command works by internally running cargo check (or a similar build process) and parsing the structured diagnostics output from the Rust compiler. When the compiler identifies a warning or error for which it can suggest a deterministic fix, cargo fix applies these changes directly to the source code files. This automates routine code modernization and cleanup, significantly reducing manual effort. It is particularly valuable when upgrading large projects across Rust editions or when dealing with numerous minor warnings. Users are always encouraged to review the changes made by cargo fix, typically via version control diffs, before committing them.

CAVEATS

While powerful, cargo fix doesn't resolve all compilation issues; it only applies fixes for problems where the compiler provides an explicit suggestion. Always review the changes made by cargo fix using a version control system (e.g., git diff) before committing them, as automated changes, though usually safe, should be verified. In rare cases, applying one fix might reveal or introduce new warnings or errors that require manual intervention. It also typically requires a working internet connection for dependency resolution unless the --offline flag is used.

HOW IT WORKS

cargo fix operates by invoking the Rust compiler (rustc) in a way that generates machine-readable JSON output for its diagnostics. The compiler provides 'suggestions' within this output, detailing a specific span of code to modify and the replacement text. cargo fix then parses these suggestions and applies the necessary changes directly to the source files on disk. It intelligently handles multiple suggestions within a single file and ensures proper file encoding. This mechanism makes it highly reliable for applying compiler-recommended changes.

IDEMPOTENCY

When run multiple times on the same codebase, cargo fix is generally idempotent for a given set of compiler diagnostics. This means that once all fixable issues have been addressed, subsequent runs on the same codebase without code changes should ideally result in no further modifications. It applies deterministic changes based on the compiler's output, ensuring consistent results.

HISTORY

cargo fix was introduced as an experimental feature with the release of the Rust 2018 Edition. Its primary motivation was to provide an automated path for migrating existing Rust 2015 codebases to the new edition, addressing breaking changes and deprecations. Following its successful adoption, it became a stable and integral part of the Cargo toolchain. Its development is closely tied to the Rust compiler's diagnostic system, which is continually improved to provide more precise and actionable suggestions. This command has significantly streamlined the process of keeping Rust projects up-to-date with new language features, deprecations, and best practices across different Rust editions.

SEE ALSO

cargo(1), cargo check(1), cargo build(1), rustfmt(1), clippy(1)

Copied to clipboard