CARGO-FIX(1)
π CARGO-FIX(1) β General Commands Manual π
hiii bestie π«Ά welcome to the cargo-fix page, straight outta the General Commands Manual. this is the one that makes rustc's little suggestions come true. buckle up, it's long, but we're getting through it together β¨
β¨ NAME β¨
cargo-fix β Automatically fix lint warnings reported by rustc
πΈ SYNOPSIS πΈ
cargo fix [options]
π¦ DESCRIPTION π¦
okay so here's the vibe: this Cargo subcommand takes rustc's suggestions β the ones that come bundled inside diagnostics like warnings β and automatically applies them to your source code. it's literally built to automate the stuff rustc was already telling you to go fix. the compiler already knew, it was just waiting for you to do it. not anymore π
when you run cargo fix, under the hood it executes cargo-check(1). any warnings that apply to your crate get fixed automatically (if that's possible), and every remaining warning shows up when the check process finishes. so like, if you wanna apply all the fixes to the current package, you just run:
cargo fix
which behaves the same as cargo check --all-targets. same energy, different name β¨
now, real talk: cargo fix is only capable of fixing code that's normally compiled with cargo check. so if some code is conditionally enabled with optional features, you gotta turn those features on for that code to even get analyzed:
cargo fix --features foo
same deal with other cfg expressions, like platform-specific code β you'll need to pass --target to fix code for that given target:
cargo fix --target x86_64-pc-windows-gnu
if you hit any problems with cargo fix, or you've got questions, or you wanna request a feature, please please please don't hesitate to file an issue at https://github.com/rust-lang/cargo π«Ά
π Edition migration π
plot twist bestie, the cargo fix subcommand can also be used to migrate a package from one edition to the next (edition guide: https://doc.rust-lang.org/edition-guide/editions/transitioning-an-existing-project-to-a-new-edition.html). the general procedure is:
- Run
cargo fix --edition. consider also using the--all-featuresflag if your project has multiple features. you may also want to runcargo fix --editionmultiple times with different--targetflags if your project has platform-specific code gated bycfgattributes. - Modify
Cargo.tomlto set theeditionfield (docs:https://doc.rust-lang.org/cargo/reference/manifest.html#the-edition-field) to the new edition. - Run your project tests to verify that everything still works. if new warnings get issued, you may wanna consider running
cargo fixagain (without the--editionflag) to apply any suggestions the compiler hands you.
and hopefully that's it!! π just keep in mind those caveats from above β cargo fix cannot update code for inactive features or cfg expressions. also, in some rare cases the compiler is unable to automatically migrate all code to the new edition, and that may require manual changes after building with the new edition. so like, eyes open, okay? π
π₯ OPTIONS π₯
π Fix options π
--broken-codeβ fix code even if it already has compiler errors. this is useful ifcargo fixfails to apply the changes. it'll apply the changes and leave the broken code sitting in the working directory for you to inspect and manually fix. (yes, it's called broken code. it knows. π«£)--editionβ apply changes that will update the code to the next edition. this will not update the edition in theCargo.tomlmanifest β that one has to be updated manually aftercargo fix --editionhas finished. no cap.--edition-idiomsβ apply suggestions that will update code to the preferred style for the current edition.--allow-no-vcsβ fix code even if a VCS was not detected.--allow-dirtyβ fix code even if the working directory has changes.--allow-stagedβ fix code even if the working directory has staged changes.
π§ Package Selection π§
by default, when no package selection options are given, the packages selected depend on the selected manifest file (based on the current working directory if --manifest-path is not given). if the manifest is the root of a workspace, then the workspace's default members are selected. otherwise, only the package defined by the manifest gets selected.
the default members of a workspace can be set explicitly with the workspace.default-members key in the root manifest. if that's not set, a virtual workspace will include all workspace members (equivalent to passing --workspace), and a non-virtual workspace will include only the root crate itself.
-p*spec*β¦,--package*spec*β¦ β fix only the specified packages. seecargo-pkgid(1)for the SPEC format. this flag may be specified multiple times and supports common Unix glob patterns like*,?and[]. however β to avoid your shell accidentally expanding glob patterns before Cargo handles them, you must use single quotes or double quotes around each pattern. the shell is not your friend here bestie π--workspaceβ fix all members in the workspace.--allβ deprecated alias for--workspace. it's giving legacy β¨--exclude*SPEC*β¦ β exclude the specified packages. must be used in conjunction with the--workspaceflag. this flag may be specified multiple times and supports common Unix glob patterns like*,?and[]. however β to avoid your shell accidentally expanding glob patterns before Cargo handles them, you must use single quotes or double quotes around each pattern.
π Target Selection π
when no target selection options are given, cargo fix will fix all targets (--all-targets implied). binaries are skipped if they have required-features that are missing.
passing target selection flags will fix only the specified targets.
note that --bin, --example, --test and --bench flags also support common Unix glob patterns like *, ? and []. however β to avoid your shell accidentally expanding glob patterns before Cargo handles them, you must use single quotes or double quotes around each glob pattern. (yes this warning is everywhere. it's important. iykyk.)
--libβ fix the package's library.--bin*name*β¦ β fix the specified binary. this flag may be specified multiple times and supports common Unix glob patterns.--binsβ fix all binary targets.--example*name*β¦ β fix the specified example. this flag may be specified multiple times and supports common Unix glob patterns.--examplesβ fix all example targets.--test*name*β¦ β fix the specified integration test. this flag may be specified multiple times and supports common Unix glob patterns.--testsβ fix all targets that have thetest = truemanifest flag set. by default this includes the library and binaries built as unittests, and integration tests. be aware that this will also build any required dependencies, so the lib target may be built twice (once as a unittest, and once as a dependency for binaries, integration tests, etc.). targets may be enabled or disabled by setting thetestflag in the manifest settings for the target.--bench*name*β¦ β fix the specified benchmark. this flag may be specified multiple times and supports common Unix glob patterns.--benchesβ fix all targets that have thebench = truemanifest flag set. by default this includes the library and binaries built as benchmarks, and bench targets. be aware that this will also build any required dependencies, so the lib target may be built twice (once as a benchmark, and once as a dependency for binaries, benchmarks, etc.). targets may be enabled or disabled by setting thebenchflag in the manifest settings for the target.--all-targetsβ fix all targets. this is equivalent to specifying--lib --bins --tests --benches --examples. the full package, bestie, everything gets the treatment π
π Feature Selection π
the feature flags let you control which features are enabled. when no feature options are given, the default feature is activated for every selected package.
see the features documentation at https://doc.rust-lang.org/cargo/reference/features.html#command-line-feature-options for more details.
-Ffeatures,--featuresfeatures β space or comma separated list of features to activate. features of workspace members may be enabled withpackage-name/feature-namesyntax. this flag may be specified multiple times, which enables all specified features.--all-featuresβ activate all available features of all selected packages.--no-default-featuresβ do not activate the default feature of the selected packages.
π Compilation Options π
--targettriple β fix for the given architecture. the default is the host architecture. the general format of the triple is<arch><sub>-<vendor>-<sys>-<abi>. runrustc --print target-listfor a list of supported targets. this flag may be specified multiple times.
this may also be specified with the build.target config value (https://doc.rust-lang.org/cargo/reference/config.html).
note that specifying this flag makes Cargo run in a different mode where the target artifacts are placed in a separate directory. see the build cache documentation at https://doc.rust-lang.org/cargo/reference/build-cache.html for more details.
-r,--releaseβ fix optimized artifacts with thereleaseprofile. see also the--profileoption for choosing a specific profile by name.--profilename β fix with the given profile.
as a special case, specifying the test profile will also enable checking in test mode, which will enable checking tests and enable the test cfg option. see rustc tests at https://doc.rust-lang.org/rustc/tests/index.html for more detail.
see the reference at https://doc.rust-lang.org/cargo/reference/profiles.html for more details on profiles.
--timings=fmtsβ output information about how long each compilation takes, and track concurrency information over time. accepts an optional comma-separated list of output formats;--timingswithout an argument will default to--timings=html. specifying an output format (rather than the default) is unstable and requires-Zunstable-options. valid output formats:html(unstable, requires-Zunstable-options): write a human-readable filecargo-timing.htmlto thetarget/cargo-timingsdirectory with a report of the compilation. also write a report to the same directory with a timestamp in the filename if you want to look at older runs. HTML output is suitable for human consumption only, and does not provide machine-readable timing data.json(unstable, requires-Zunstable-options): emit machine-readable JSON information about timing information.
β Output Options β
--target-dirdirectory β directory for all generated artifacts and intermediate files. may also be specified with theCARGO_TARGET_DIRenvironment variable, or thebuild.target-dirconfig value (https://doc.rust-lang.org/cargo/reference/config.html). defaults totargetin the root of the workspace.
Display Options β¨
-v,--verboseβ okay so this turns on the yapping. Use it once for verbose output, and twice for βvery verboseβ output, which is the extra deluxe version that also shows stuff like dependency warnings and build script output. You can also set it with theterm.verboseconfig value<https://doc.rust-lang.org/cargo/reference/config.html>. Bestie, it's giving βtell me everythingβ energy.-q,--quietβ shhh π€« this tells Cargo not to print cargo log messages. May also be specified with theterm.quietconfig value<https://doc.rust-lang.org/cargo/reference/config.html>.--colorwhen β controls when colored output is used. Valid values:auto(default): Automatically detect if color support is available on the terminal. iykyk.always: Always display colors. rainbow time π.never: Never display colors. no color, no drama. May also be specified with theterm.colorconfig value<https://doc.rust-lang.org/cargo/reference/config.html>.
--message-formatfmt β the output format for diagnostic messages. Can be specified multiple times and consists of comma-separated values. Valid values:human(default): Display in a human-readable text format. Conflicts withshortandjson. So don't try to mix these, okay?short: Emit shorter, human-readable text messages. Conflicts withhumanandjson.json: Emit JSON messages to stdout. See the reference<https://doc.rust-lang.org/cargo/reference/external-tools.html#json-messages>for more details. Conflicts withhumanandshort.json-diagnostic-short: Ensure the rendered field of JSON messages contains the βshortβ rendering from rustc. Cannot be used withhumanorshort.json-diagnostic-rendered-ansi: Ensure the rendered field of JSON messages contains embedded ANSI color codes for respecting rustcβs default color scheme. Cannot be used withhumanorshort.json-render-diagnostics: Instruct Cargo to not include rustc diagnostics in JSON messages printed, but instead Cargo itself should render the JSON diagnostics coming from rustc. Cargoβs own JSON diagnostics and others coming from rustc are still emitted. Cannot be used withhumanorshort.
Manifest Options π¦
--manifest-pathpath β Path to theCargo.tomlfile. By default, Cargo searches for theCargo.tomlfile in the current directory or any parent directory. Like it's looking for its emotional support manifest.--ignore-rust-versionβ Ignorerust-versionspecification in packages. Not me ignoring my own boundaries, but okay.--lockedβ Asserts that the exact same dependencies and versions are used as when the existingCargo.lockfile was originally generated. Cargo will exit with an error when either of the following scenarios arises:- The lock file is missing.
- Cargo attempted to change the lock file due to a different dependency resolution. It may be used in environments where deterministic builds are desired, such as in CI pipelines. Very demure, very mindful, very reproducible. π
--offlineβ Prevents Cargo from accessing the network for any reason. Without this flag, Cargo will stop with an error if it needs to access the network and the network is not available. With this flag, Cargo will attempt to proceed without the network if possible. Beware that this may result in different dependency resolution than online mode. Cargo will restrict itself to crates that are downloaded locally, even if there might be a newer version as indicated in the local copy of the index. See thecargo-fetch(1)command to download dependencies before going offline. May also be specified with thenet.offlineconfig value<https://doc.rust-lang.org/cargo/reference/config.html>. So like, airplane mode but for builds βοΈ.--frozenβ Equivalent to specifying both--lockedand--offline. Two for one, slay.--lockfile-pathPATH β Changes the path of the lockfile from the default (<workspace_root>/Cargo.lock) to PATH. PATH must end withCargo.lock(e.g.--lockfile-path /tmp/temporary-lockfile/Cargo.lock). Note that providing--lockfile-pathwill ignore existing lockfile at the default path, and instead will either use the lockfile from PATH, or write a new lockfile into the provided PATH if it doesnβt exist. This flag can be used to run most commands in read-only directories, writing lockfile into the provided PATH.
This option is only available on the nightly channel<https://doc.rust-lang.org/book/appendix-07-nightly-rust.html>and requires the-Z unstable-optionsflag to enable (see #14421<https://github.com/rust-lang/cargo/issues/14421>). So bestie, nightly only, okay? Don't try this on stable and then act surprised. π
Common Options π
+toolchainβ If Cargo has been installed with rustup, and the first argument tocargobegins with+, it will be interpreted as a rustup toolchain name (such as+stableor+nightly). See the rustup documentation<https://rust-lang.github.io/rustup/overrides.html>for more information about how toolchain overrides work. Toolchain switch-up, main character energy.--configKEY=VALUE or PATH β Overrides a Cargo configuration value. The argument should be in TOML syntax ofKEY=VALUE, or provided as a path to an extra configuration file. This flag may be specified multiple times. See the command-line overrides section<https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>for more information. Config but make it spontaneous.-CPATH β Changes the current working directory before executing any specified operations. This affects things like where cargo looks by default for the project manifest (Cargo.toml), as well as the directories searched for discovering.cargo/config.toml, for example. This option must appear before the command name, for examplecargo -C path/to/my-project build. This option is only available on the nightly channel<https://doc.rust-lang.org/book/appendix-07-nightly-rust.html>and requires the-Z unstable-optionsflag to enable (see #10098<https://github.com/rust-lang/cargo/issues/10098>). Nightly gate again, bestie. Set the vibe before you pull up.-h,--helpβ Prints help information. When in doubt, ask for help. It's giving healthy communication. π«Ά-Zflag β Unstable (nightly-only) flags to Cargo. Runcargo -Z helpfor details. The forbidden knowledge corner π
Miscellaneous Options πΈ
-jN,--jobsN β Number of parallel jobs to run. May also be specified with thebuild.jobsconfig value<https://doc.rust-lang.org/cargo/reference/config.html>. Defaults to the number of logical CPUs. If negative, it sets the maximum number of parallel jobs to the number of logical CPUs plus provided value. If a stringdefaultis provided, it sets the value back to defaults. Should not be0. Zero jobs? Absolutely not, that's not a vibe.--keep-goingβ Build as many crates in the dependency graph as possible, rather than aborting the build on the first one that fails to build. For example if the current package depends on dependenciesfailsandworks, one of which fails to build,cargo fix -j1may or may not build the one that succeeds (depending on which one of the two builds Cargo picked to run first), whereascargo fix -j1 --keep-goingwould definitely run both builds, even if the one run first fails. Keep going, keep glowing, don't let one flop stop the whole show. π₯
ENVIRONMENT π
See the reference <https://doc.rust-lang.org/cargo/reference/environment-variables.html> for details on environment variables that Cargo reads. The tea is all there, bestie.
EXIT STATUS β¨
0: Cargo succeeded. We love a win. π101: Cargo failed to complete. Not the ending we wanted, but at least it's consistent. π
EXAMPLES π
- Apply compiler suggestions to the local package:
cargo fix
- Update a package to prepare it for the next edition:
cargo fix --edition
- Apply suggested idioms for the current edition:
cargo fix --edition-idioms
SEE ALSO π¦
And that's the tea from the CARGO-FIX(1) manual page footer β source/version: CARGO-FIX(1), date: not listed here, but the energy is eternal. πβ¨
π¦ where this page comes from
- ubuntu jammy: cargo-1.84 1.84.1+dfsg0ubuntu1~bpo10-0ubuntu4.22.04.1 Β· Apache-2.0 WITH LLVM exception
- ubuntu noble: cargo-1.84 1.84.1+dfsg0ubuntu1~bpo2-0ubuntu2.24.04.1 Β· Apache-2.0 WITH LLVM exception
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms