CARGO-FIX(1)
π CARGO-FIX(1) π
General Commands Manual Β· cargo-fix
okay bestie, gather round, because this one is a banger. it's cargo-fix, and it's literally the "hey compiler, you tell me what's wrong and I'll fix it for you" subcommand. no cap. π«Ά
β¨ NAME β¨
cargo-fix β Automatically fix lint warnings reported by rustc π
πΈ SYNOPSIS πΈ
cargo fix [options]
π¦ DESCRIPTION π¦
so like. this Cargo subcommand will automatically take rustc's suggestions from diagnostics (you know, the warnings) and apply them to your source code. the whole point is to automate the annoying little tasks that rustc already knows how to tell you to fix. it's giving "compiler does your homework." iconic. π
when you run cargo fix, under the hood it executes cargo-check(1). any warnings that apply to your crate get automatically fixed (if possible!!) and all the remaining warnings get displayed once the check process finishes. so if you wanna apply all fixes to the current package, you literally just run:
cargo fix
and that behaves the same as cargo check --all-targets. slay. β¨
now, important tea: 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 enable those features for that code to even get analyzed:
cargo fix --features foo
same energy with other cfg expressions, like platform-specific code β you'll need to pass --target to fix code for the given target:
cargo fix --target x86_64-pc-windows-gnu
and if you hit any problems with cargo fix, or you've got questions, or feature requests, please please please don't hesitate to file an issue at https://github.com/rust-lang/cargo β they want to hear from you fr fr. π
π Edition migration π
the cargo fix subcommand can also be used to migrate a package from one edition to the next. (edition guide, if you need it: 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 (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 are issued, you may want to consider running
cargo fixagain (without the--editionflag) to apply any suggestions given by the compiler.
and hopefully that's it!! just keep in mind the caveats mentioned 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. lowkey tragic but we move. π
π OPTIONS π
π οΈ Fix options π οΈ
--broken-codeβ fix code even if it already has compiler errors. useful ifcargo fixfails to apply the changes. it will apply the changes and leave the broken code in the working directory for you to inspect and manually fix. (yes, deliberately leaving a mess. it's for you to look at. π)--editionβ apply changes that will update the code to the next edition. this will not update the edition in theCargo.tomlmanifest, which must be updated manually aftercargo fix --editionhas finished. do not forget this part, bestie, I am begging. π--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 (including staged 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 will be selected. π§
the default members of a workspace can be set explicitly with the workspace.default-members key in the root manifest. if this is 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 β heads up β to avoid your shell accidentally expanding glob patterns before Cargo handles them, you must use single quotes or double quotes around each pattern. quote it or lose it. π--workspaceβ fix all members in the workspace.--allβ deprecated alias for--workspace.--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. (same rule, same drama. π« )
π― 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,--testand--benchflags 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. π
--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. all of them. the whole lineup. the full squad. π
π 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 for more details: https://doc.rust-lang.org/cargo/reference/features.html#command-line-feature-options
-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. go big or go home.--no-default-featuresβ do not activate thedefaultfeature of the selected packages.
βοΈ Compilation Options βοΈ
--targettriple β fix for the specified target architecture. flag may be specified multiple times. the default is the host architecture. the general format of the triple is<arch><sub>-<vendor>-<sys>-<abi>.
possible values:
- any supported target in
rustc --print target-list. "host-tuple", which will internally be substituted by the host's target. this can be particularly useful if you're cross-compiling some crates, and don't want to specify your host's machine as a target (for instance, anxtaskin a shared project that may be worked on by many hosts).- a path to a custom target specification. see Custom Target Lookup Path for more information: https://doc.rust-lang.org/rustc/targets/custom.html#custom-target-lookup-path
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 for more details: https://doc.rust-lang.org/cargo/reference/build-cache.html
-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 for more detail: https://doc.rust-lang.org/rustc/tests/index.html
see the reference for more details on profiles: https://doc.rust-lang.org/cargo/reference/profiles.html
--timingsβ output information on how long each compilation takes, and track concurrency information over time. β±οΈ
a file cargo-timing.html will be written to the target/cargo-timings directory at the end of the build. an additional report with a timestamp in its filename is also written if you want to look at a previous run. these reports are suitable for human consumption only, and do not provide machine-readable timing data. (it's for your eyes, not for a robot's. π«Ά)
π Output Options π
--target-dirdirectory β where all the generated artifacts and the intermediate files go, bestie. You can also set this with theCARGO_TARGET_DIRenvironment variable, or with thebuild.target-dirconfig value <https://doc.rust-lang.org/cargo/reference/config.html>. Defaults totargetin the root of the workspace. π
π Display Options π
-v,--verboseβ verbose output, pls. Pass it twice for "very verbose" output, which throws in the extra 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>.-q,--quietβ do not print cargo log messages. May also be specified with theterm.quietconfig value <https://doc.rust-lang.org/cargo/reference/config.html>. Shhh π€«--colorwhen β controls when colored output is used. Valid values:auto(default): automatically detects if color support is available on the terminal.always: always display colors. πnever: never display colors.
May also be specified with the term.color config value <https://doc.rust-lang.org/cargo/reference/config.html>.
--message-formatfmt β the output format for diagnostic messages. Can be specified multiple times, and it's made of comma-separated values. Valid values:human(default): display in a human-readable text format. Conflicts withshortandjson.short: emit shorter, human-readable text messages. Conflicts withhumanandjson.json: emit JSON messages tostdout. See the reference <https://doc.rust-lang.org/cargo/reference/external-tools.html#json-messages> for more details. Conflicts withhumanandshort.json-diagnostic-short: ensures therenderedfield of JSON messages contains the "short" rendering fromrustc. Cannot be used withhumanorshort.json-diagnostic-rendered-ansi: ensures therenderedfield of JSON messages contains embedded ANSI color codes, so it respectsrustc's default color scheme. Cannot be used withhumanorshort.json-render-diagnostics: tells Cargo to not includerustcdiagnostics in the JSON messages it prints, and instead have Cargo itself render the JSON diagnostics coming fromrustc. Cargo's own JSON diagnostics and others coming fromrustcare still emitted. Cannot be used withhumanorshort.
Not me having six whole message formats to choose from. Pick your fighter, bestie π
π Manifest Options π
--manifest-pathpath β the path to theCargo.tomlfile. By default, Cargo goes looking for theCargo.tomlfile in the current directory or any parent directory. π¦--ignore-rust-versionβ ignore therust-versionspecification in packages.--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 these scenarios shows up:- 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. No surprises allowed, periodt. π
--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 isn't 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 the cargo-fetch(1) command to download dependencies before going offline.
May also be specified with the net.offline config value <https://doc.rust-lang.org/cargo/reference/config.html>.
--frozenβ equivalent to specifying both--lockedand--offline. Two flags in a trench coat π§
π«Ά 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. --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.-CPATH β changes the current working directory before executing any specified operations. This affects things like wherecargolooks 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-options flag to enable (see #10098 <https://github.com/rust-lang/cargo/issues/10098>).
-h,--helpβ prints help information. π-Zflag β unstable (nightly-only) flags to Cargo. Runcargo -Z helpfor details. iykyk π₯
π 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 the provided value. If a stringdefaultis provided, it sets the value back to defaults. Should not be0.--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 dependencies fails and works, one of which fails to build, cargo fix -j1 may or may not build the one that succeeds (depending on which one of the two builds Cargo picked to run first), whereas cargo fix -j1 --keep-going would definitely run both builds, even if the one run first fails.
She does not give up. Main character energy fr fr β¨
πΈ ENVIRONMENT πΈ
See the reference <https://doc.rust-lang.org/cargo/reference/environment-variables.html> for details on environment variables that Cargo reads. π
β EXIT STATUS β
0: Cargo succeeded. π101: Cargo failed to complete. π
π 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 whole page, bestie!! This one's from CARGO-FIX(1) β Cargo source, version and date as printed in the footer of the original manpage, which we are respectfully not re-typing because it's the footer's whole job. Go fix some code, you icon πβ¨π
π¦ where this page comes from
- alpine 3.24: cargo-doc 1.96.1-r0 Β· Apache-2.0 OR MIT
- alpine 3.24: chromium-cargo-doc 1.97.0-r0 Β· Apache-2.0 OR MIT
- centos-stream 10: cargo 1.99.1-1.el10 Β· (Apache-2.0 OR MIT) AND (Artistic-2.0 AND BSD-3-Clause AND ISC AND MIT AND MPL-2.0 AND Unicode-3.0)
- centos-stream 9: cargo 1.99.1-1.el9 Β· (Apache-2.0 OR MIT) AND (Artistic-2.0 AND BSD-3-Clause AND ISC AND MIT AND MPL-2.0 AND Unicode-3.0)
- debian bookworm: cargo-web 1.96.0+dfsg1-1~deb12u2 Β· MIT OR Apache-2.0
- debian trixie: cargo-web 1.96.0+dfsg1-1~deb13u1+b1 Β· MIT OR Apache-2.0
- fedora 43: cargo 1.98.1-1.fc43
- fedora 43: cargo 1.99.0-1.fc43
- fedora 43: cargo 1.99.1-2.fc43 Β· (Apache-2.0 OR MIT) AND (Artistic-2.0 AND BSD-3-Clause AND ISC AND MIT AND MPL-2.0 AND Unicode-3.0)
- fedora 44: cargo 1.94.1-1.fc44 Β· (Apache-2.0 OR MIT) AND (Artistic-2.0 AND BSD-3-Clause AND ISC AND MIT AND MPL-2.0 AND Unicode-3.0)
- fedora 44: cargo 1.98.1-1.fc44
- fedora 44: cargo 1.99.0-1.fc44
- fedora 44: cargo 1.99.1-2.fc44 Β· (Apache-2.0 OR MIT) AND (Artistic-2.0 AND BSD-3-Clause AND ISC AND MIT AND MPL-2.0 AND Unicode-3.0)
- nixos 26.05: cargo 1.95.0
- opensuse-leap 16.0: cargo1.94 1.94.1-160000.1.1 Β· Apache-2.0 OR MIT
- opensuse-leap 16.0: cargo1.94 1.94.1-160000.1.1 Β· Apache-2.0 OR MIT
- opensuse-leap 16.0: cargo1.94 1.94.1-160000.1.1 Β· Apache-2.0 OR MIT
- opensuse-leap 16.0: cargo1.94 1.94.1-160000.1.1 Β· Apache-2.0 OR MIT
- opensuse-leap 16.0: cargo1.95 1.95.0-160000.1.1 Β· Apache-2.0 OR MIT
- opensuse-leap 16.0: cargo1.95 1.95.0-160000.1.1 Β· Apache-2.0 OR MIT
- opensuse-leap 16.0: cargo1.95 1.95.0-160000.1.1 Β· Apache-2.0 OR MIT
- opensuse-leap 16.0: cargo1.95 1.95.0-160000.1.1 Β· Apache-2.0 OR MIT
- opensuse-leap 16.0: cargo1.96 1.96.1-160000.1.1 Β· Apache-2.0 OR MIT
- opensuse-leap 16.0: cargo1.96 1.96.1-160000.1.1 Β· Apache-2.0 OR MIT
- opensuse-leap 16.0: cargo1.96 1.96.1-160000.1.1 Β· Apache-2.0 OR MIT
- opensuse-leap 16.0: cargo1.96 1.96.1-160000.1.1 Β· Apache-2.0 OR MIT
- opensuse-leap 16.0: cargo1.97 1.97.0-160000.1.1 Β· Apache-2.0 OR MIT
- opensuse-leap 16.0: cargo1.97 1.97.0-160000.1.1 Β· Apache-2.0 OR MIT
- opensuse-leap 16.0: cargo1.97 1.97.0-160000.1.1 Β· Apache-2.0 OR MIT
- opensuse-leap 16.0: cargo1.97 1.97.0-160000.1.1 Β· Apache-2.0 OR MIT
- opensuse-leap 16.0: cargo1.98 1.98.0-160000.1.1 Β· Apache-2.0 OR MIT
- opensuse-leap 16.0: cargo1.98 1.98.0-160000.1.1 Β· Apache-2.0 OR MIT
- opensuse-leap 16.0: cargo1.98 1.98.0-160000.1.1 Β· Apache-2.0 OR MIT
- opensuse-leap 16.0: cargo1.98 1.98.0-160000.1.1 Β· Apache-2.0 OR MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms