CARGO-FIX(1)
β¨ NAME β¨
cargo-fix β Automatically fix lint warnings reported by rustc. bestie she literally does your chores for you π
π¦ SYNOPSIS π¦
cargo fix [options]
πΈ DESCRIPTION πΈ
okay so picture this: this Cargo subcommand will automatically take rustc's suggestions from diagnostics like warnings and apply them straight to your source code. the whole point is to help automate the tasks that rustc itself already knows how to tell you to fix. it's giving "let the compiler do the talking, you just vibe" β¨
when you run cargo fix it's under the hood running cargo-check(1). any warnings that apply to your crate get automatically fixed (if possible), and all the leftover warnings get shown to you once the check process finishes. so like, if you wanna apply every fix to the current package, you just run:
cargo fix
and that behaves the same as cargo check --all-targets. no cap π«Ά
now, heads up: 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 energy 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 run into any problems with cargo fix, or you just have questions or feature requests, please don't hesitate to file an issue at <https://github.com/rust-lang/cargo>. they're nice, promise π
π¦ Edition migration π¦
not me casually mentioning that the cargo fix subcommand can also be used to migrate a package from one edition <https://doc.rust-lang.org/edition-guide/editions/transitioning-an-existing-project-to-a-new-edition.html> to the next. the general procedure is, and I'm quoting the lore here:
- 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 by cfg attributes. - Modify
Cargo.tomlto set the edition field<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 those caveats I mentioned above β that 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, stay aware, bestie π
π OPTIONS π
π§ Fix options π§
--broken-codeβ fix code even if it already has compiler errors. this is 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 it's chaos, but it's intentional chaos π--editionβ apply changes that will update the code to the next edition. this will not update the edition in theCargo.tomlmanifest β that must be updated manually aftercargo fix --editionhas finished. the manifest is not gonna update itself, bestie, that's a you task π«Ά--edition-idiomsβ apply suggestions that will update code to the preferred style for the current edition. glow-up for your syntax β¨--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 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. icon behavior ngl π
-pspecβ¦,--packagespecβ¦ β 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. protect the pattern, bestie π‘οΈ--workspaceβ fix all members in the workspace. the whole squad π--allβ deprecated alias for--workspace. she's old, she still works, but likeβ¦ noted π--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, same rule as before. the shell is nosy π)
--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 in test mode 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 in benchmark mode 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 everything bagel π₯―
π 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 <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. go big or go home π--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 <https://doc.rust-lang.org/cargo/guide/build-cache.html> documentation for more details.
-r,--releaseβ fix optimized artifacts with the release profile. 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 <https://doc.rust-lang.org/rustc/tests/index.html> for more detail.
see the reference <https://doc.rust-lang.org/cargo/reference/profiles.html> for more details on profiles.
--timings=fmts β output information 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 the build.target-dir config value<https://doc.rust-lang.org/cargo/reference/config.html>. defaults totargetin the root of the workspace.
β¨ Display Options β¨
Okay bestie, these control how Cargo talks to you. π
-v,--verboseβ Wanna see everything? This gives you verbose output. Use it twice for "very verbose" mode, which spills all the tea including dependency warnings and build script output. You can also set this with theterm.verboseconfig value at https://doc.rust-lang.org/cargo/reference/config.html. β¨-q,--quietβ Shhh! π€« This tells Cargo to stop printing its log messages. You can also set this with theterm.quietconfig value at https://doc.rust-lang.org/cargo/reference/config.html.--color whenβ Controls when colored output is used. Valid values are:auto(default): Automatically detects if your terminal supports color. It's giving "I'll match your vibe." πalways: Always displays colors. No cap, full rainbow. πnever: Never displays colors. Monochrome moment. π€
You can also set this with the term.color config value at https://doc.rust-lang.org/cargo/reference/config.html.
--message-format fmtβ The output format for diagnostic messages. You can specify it multiple times and it's a comma-separated list. 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 to stdout. See the reference at 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 from rustc. Cannot be used withhumanorshort. π¦json-diagnostic-rendered-ansi: Ensures therenderedfield of JSON messages contains embedded ANSI color codes for respecting rustc's default color scheme. Cannot be used withhumanorshort. πjson-render-diagnostics: Tells Cargo to not include rustc diagnostics in the 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 π
These are all about your Cargo.toml and lock files, bestie. π«Ά
--manifest-path pathβ Path to theCargo.tomlfile. By default, Cargo searches for theCargo.tomlfile in the current directory or any parent directory. It's like a little scavenger hunt. π--ignore-rust-versionβ Ignores therust-versionspecification in packages. Living on the edge, periodt. π--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 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. So iconic for reproducibility. π
--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 at https://doc.rust-lang.org/cargo/reference/config.html. βοΈ--frozenβ Equivalent to specifying both--lockedand--offline. It's the combo meal. π
π Common Options π
These are the options that show up everywhere, iykyk. π«
+toolchainβ If Cargo has been installed with rustup, and the first argument to cargo begins with+, it will be interpreted as a rustup toolchain name (such as+stableor+nightly). See the rustup documentation at https://rust-lang.github.io/rustup/overrides.html for more information about how toolchain overrides work. π¦--config KEY=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 at https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides for more information. βοΈ-C PATHβ 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 at https://doc.rust-lang.org/book/appendix-07-nightly-rust.html and requires the-Z unstable-optionsflag to enable (see #10098 at https://github.com/rust-lang/cargo/issues/10098). π-h,--helpβ Prints help information. Your bestie when you're lost. π-Z flagβ Unstable (nightly-only) flags to Cargo. Runcargo -Z helpfor details. For the risk-takers. π
π§ Miscellaneous Options π§
The extra sprinkle on top. β¨
-j N,--jobs Nβ Number of parallel jobs to run. May also be specified with thebuild.jobsconfig value at 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 be 0. πββοΈ--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 it moving! πͺ
πΏ ENVIRONMENT πΏ
See the reference at 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 vibe, bestie! π This was CARGO-FIX(1) from the Cargo manual β source is the Cargo docs, and the date is whatever your local version says, but the energy is eternal. Slay! π
β¨
π¦ where this page comes from
- ubuntu jammy: cargo-1.80 1.80.1+dfsg0ubuntu1~bpo0-0ubuntu0.22.04.1 Β· Apache-2.0 WITH LLVM exception
- ubuntu noble: cargo-1.80 1.80.1+dfsg0ubuntu1-0ubuntu0.24.04.01 Β· MIT OR Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms