Rust Tooling and Code Quality
cargo fix
The cargo fix command automatically applies machine-applicable code suggestions emitted by rustc to your source files in place. It is used both for cleaning up everyday compiler warnings (such as unused imports, unnecessary mut bindings, or deprecated syntax) and for migrating codebases cleanly to a new Rust Edition (cargo fix --edition).
Source Code with Warnings or Legacy Edition Idioms
|
v `cargo fix` (or `cargo fix --edition`)
[ Invokes `rustc` to Collect Suggestions ]
|
v
[ Applies Patch Spans & Verifies Code Still Compiles ]
|
v
Updated Source Files
Everyday Warning Cleanup vs Edition Migration
| Command | Purpose |
|---|---|
cargo fix |
Applies machine-applicable rustc warning fixes across all targets in the current package (requires a clean Git working tree unless --allow-dirty is passed). |
cargo fix --allow-dirty --allow-staged |
Applies fixes even when the Git working directory has uncommitted or staged changes. |
cargo fix --edition |
Prepares a crate built on an older edition (e.g., 2021) to upgrade to the next edition (2024) by rewriting syntax that changes meaning in the new edition. |
cargo fix --edition-idioms |
Upgrades older idioms (such as explicit extern crate or legacy trait object syntax) to modern edition idioms. |
cargo clippy --fix |
Runs Clippy's extended lint suite and applies machine-applicable Clippy suggestions. |
Safety Guardrails Built into cargo fix
cargo fix is designed so it cannot silently break a compiling project:
- Git working tree check: By default,
cargo fixrefuses to run if your Git repository has uncommitted changes so you can always inspect its exact modifications withgit diffor revert them withgit checkout .. - Post-patch verification: After applying a batch of compiler suggestions to a file,
cargo fixre-runsrustcon that file. If the suggested rewrite fails to compile for any reason,cargo fixautomatically rolls back the change and leaves the original file intact!
Migrating a Package to the Rust 2024 Edition
To upgrade a package from edition = "2021" to edition = "2024", follow this 3-step sequence:
# Step 1: While `edition = "2021"` is still set in Cargo.toml, run `cargo fix --edition`:
cargo fix --edition
# Step 2: Update Cargo.toml to `edition = "2024"` (and optionally run `cargo fmt`):
# edition = "2024"
# Step 3: Verify all targets and tests compile cleanly under Rust 2024:
cargo test --all-targets
For example, when migrating to the Rust 2024 edition, cargo fix --edition automatically:
- Inserts
unsafeon#[no_mangle],#[export_name], and#[link_section]attributes (#[unsafe(no_mangle)]). - Wraps
extern "C"blocks inunsafe extern "C". - Adjusts
ref/mutbindings inside patterns that match by reference.
// In Rust 2024 edition, exporting an unmangled symbol requires `#[unsafe(no_mangle)]`,
// which `cargo fix --edition` rewrites automatically from `#[no_mangle]`:
#[unsafe(no_mangle)]
pub extern "C" fn exported_api_version() -> u32 {
2024
}
fn main() {
println!("Exported API version: {}", exported_api_version());
}
Exported API version: 2024