When `cargo fix --edition` migrates a Rust 2018 crate toward 2021, what does it change, and what must you still fix by hand?
answer
- the next edition's compatibility lints
- code valid on both editions
- the manifest stays on 2018
- only the active configuration
- doctests and macros left behind
basics
~20 sIt runs cargo check with the next edition's compatibility lints and applies their suggestions so the code compiles on both editions. It leaves the edition key, doctests, inactive cfg code, uninvoked macros and generated code to you.
solid answer
~40 s`cargo fix --edition` runs the equivalent of `cargo check` with the target edition's lint group, here `rust-2021-compatibility`, forced on, applies each suggestion so the code compiles on both editions, re-checks, and backs out any fix that breaks the build (`--broken-code` keeps it). It repeats until no new warnings appear and checks all targets of the active configuration. It does not touch the `edition` key, so you set it afterwards and run `cargo test`. By hand: doctests, which it never updates; code behind features or other targets unless you rerun with `--all-features` or `--target`; macros it cannot see or fix, meaning exported `macro_rules!` macros nothing in the crate expands, and proc macros; and Rust generated by build scripts.
code
console · 8 lines$ cargo fix --edition --allow-no-vcs
Migrating Cargo.toml from 2018 edition to 2021
Checking ledger v0.1.0 (/tmp/ledger)
Migrating src/lib.rs from 2018 edition to 2021
Fixed src/lib.rs (4 fixes)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.24s
$ grep edition Cargo.toml
edition = "2018"go deeper
Recall the order: cargo fix --edition first, then change the edition key yourself, then build and test.
Explain the mechanism: the next edition's compatibility lint group, fixes that compile on both editions, the check-fix-recheck loop and backing out failed fixes.
Show you know the blind spots from experience: doctests, feature- and target-gated code, macros never expanded in the crate, proc macros and build-script output.
Turn the blind spots into a migration checklist: per-configuration runs, separate commits for fixes, bump and formatting, and tests that exercise every exported macro.
## What the command does `cargo fix` takes the compiler's machine-applicable suggestions and writes them into your source. With `--edition` it targets the **next** edition: it runs the equivalent of `cargo check` with that edition's **compatibility lint group** forced to warn — for 2018 to 2021 the group is `rust-2021-compatibility`, which contains lints such as `array_into_iter`, `non_fmt_panics`, `bare_trait_objects`, `ellipsis_inclusive_range_patterns`, `rust_2021_incompatible_closure_captures` and `rust_2021_prelude_collisions`. The loop, as the edition guide describes it: 1. Check the crate with the migration lints enabled. 2. Apply each suggestion; the suggestions rewrite code into a form that compiles on **both** the current and the next edition. 3. Check again. If a fix breaks the build, back it out and print a warning. `--broken-code` keeps the broken change so you can finish it manually. 4. Repeat while new warnings keep appearing. Because the result is valid on both editions, you can migrate in several passes while the crate stays on 2018. ## A real run On a 2018 library with an `array.into_iter()`, a `0...9` pattern, `panic!(msg)` and a trait object without `dyn`, Cargo 1.99.1 reports `Fixed src/lib.rs (4 fixes)` and rewrites them to: - `readings.iter()` — keeping the old by-reference meaning of `into_iter()` on an array; - `0..=9`; - `panic!("{}", msg)`; - `&dyn std::fmt::Debug`. `Cargo.toml` still says `edition = "2018"`. The `cargo fix` documentation says so: `--edition` "will not update the edition in the Cargo.toml manifest, which must be updated manually". ## The full procedure 1. `cargo update`, so dependencies, especially proc macros, carry their latest fixes. 2. `cargo fix --edition`, adding `--all-features` and each relevant `--target` if code is gated. 3. Change the `edition` key to the next edition. 4. `cargo build` and `cargo test`, then `cargo fix` without `--edition` for any new warnings. 5. `cargo fmt`, ideally as a separate commit. ## What it leaves for you | Left behind | Why | What to do | |---|---|---| | **Doctests** | `cargo fix` does not update documentation tests | run `cargo test` after the bump and fix them by hand | | **Inactive configurations** | it checks only the code the current features and target compile | `--all-features`, `--features`, `--target` per platform | | **Macros not expanded in the crate** | lints fire on expansions; an exported `macro_rules!` nobody invokes produces no warning, and proc macros cannot be fixed at all | invoke every macro in tests, and test it from crates on other editions | | **Code generated by build scripts** | the generated text is not your source | change the generator | | **Fixes that failed** | backed out with a warning | read the warning, or rerun with `--broken-code` | In the same run the doctest `[1u32, 2, 3].into_iter().map(|r| *r)` was left alone; after switching to 2021, `cargo test` failed it with E0614 ("type `u32` cannot be dereferenced"), because `into_iter()` on an array now yields values. A function behind `#[cfg(feature = "audit")]` failed the same way under `cargo build --features audit`, because the plain run never compiled it. ## Reading what it prints - `Migrating src/lib.rs from 2018 edition to 2021` names each file it processed; a `Migrating Cargo.toml ...` line appears too, yet the `edition` key is left as it was. - `Fixed src/lib.rs (4 fixes)` counts the suggestions applied to that file. - Any warning printed after that is one it could **not** fix, with the lint that raised it; each needs a manual change. - Without a version-control system it refuses to start ("no VCS found for this package and `cargo fix` can potentially perform destructive changes") unless you pass `--allow-no-vcs`, and it refuses on a dirty working tree unless you pass `--allow-dirty`. Commit first, so the fixes are one reviewable diff. ## Scope and granularity - By default `cargo fix` behaves like `cargo check --all-targets`: library, binaries, tests, examples and benches. - In a workspace each package has its own edition, so migration naturally goes one package at a time; individual targets can be selected too. - `cargo fix --edition-idioms` is a different, optional step that rewrites code into the new edition's idioms; the edition guide warns that its lints can make incorrect suggestions. - Moving to 2021 also switches the package's default feature resolver, which `cargo fix` cannot migrate; that belongs to Cargo's resolver material.
- Why does `cargo fix --edition` rewrite `array.into_iter()` to `array.iter()` instead of leaving it alone?On 2018 that call resolves to `(&array).into_iter()` and yields references; on 2021 it yields values. The fix must keep today's meaning and compile on both editions, so it writes `iter()`, which yields references on every edition. After the bump you can switch to by-value iteration deliberately.
- How do you migrate a crate whose code differs per platform and per Cargo feature?Run `cargo fix --edition` once per configuration that compiles different code: `--all-features` (or `--features` one at a time) for feature-gated items, and `--target` for each platform with `#[cfg]` code, which can mean a machine or cross toolchain for that target. One run only sees what the current configuration compiles.
- What is `--broken-code` for?When a suggested fix makes the crate fail to compile, `cargo fix` normally reverts it and prints a warning. With `--broken-code` it keeps the change on disk, so you can inspect a mostly-right edit and finish it by hand instead of starting from scratch.
- cargo fix --edition also bumps the edition key in Cargo.toml.
- After cargo fix --edition, a green cargo check means the migration is finished.
- cargo fix migrates doctests along with the rest of the crate.
- One run covers code behind every Cargo feature and every target platform.
- cargo fix --edition can update proc macros automatically.