cargo fix automatically applies non-breaking compiler suggestions to your code, resolving warnings and some errors without manual editing. You typically run it with the --allow-dirty flag to modify files in a working directory, or --edition to migrate code to a newer Rust edition.
For standard warnings like unused variables or deprecated syntax, run the command in your project root. By default, cargo fix only fixes code that compiles successfully, so ensure you have no hard errors first. If you want to see exactly what changes will be made before applying them, use the --dry-run flag.
# Preview changes without modifying files
cargo fix --dry-run
# Apply fixes to all warnings in the current workspace
cargo fix --allow-dirty
When migrating to a new Rust edition (e.g., 2021 to 2024), cargo fix can automatically update syntax and standard library usage. This is particularly useful for large codebases where manual updates are tedious. Always review the generated diff in your version control system before committing.
# Fix warnings and migrate to the 2024 edition
cargo fix --edition --allow-dirty
# Fix only a specific binary or library target
cargo fix --bin my_binary --allow-dirty
Be cautious with --allow-dirty; it modifies files even if you have uncommitted changes. If you are in a CI pipeline or a shared repository, prefer --dry-run to verify safety first. The tool relies on the compiler's --fix suggestions, so it cannot resolve logic errors or ambiguous code that requires human judgment. If cargo fix fails to apply a suggestion, it will report the specific warning and explain why, requiring manual intervention.
Remember that cargo fix does not run tests automatically. After applying fixes, always run cargo test to ensure the automated changes didn't introduce regressions. This tool is a productivity booster for routine cleanup but should never replace a careful code review.