CARGO-FIX(1)

πŸ“¦ version 1.92.0, in ubuntu resolute

CARGO-FIX(1) β€” General Commands Manual, bestie edition πŸ’–

ok so this is the manual for cargo-fix and honestly? it's the "let the compiler do ur homework" subcommand. buckle up πŸŽ€

🌸 NAME 🌸

cargo-fix β€” Automatically fix lint warnings reported by rustc

✨ SYNOPSIS ✨

cargo fix [options]

πŸ’– DESCRIPTION πŸ’–

okay so basically: this Cargo subcommand scoops up rustc's suggestions from diagnostics like warnings and applies them straight to ur source code. that's it, that's the whole vibe. it exists to automate the stuff rustc already knows how to tell u to fix, so u don't have to sit there going "yes ma'am" to every single warning by hand πŸ’…

when u run cargo fix, under the hood it's actually running cargo-check(1). every warning that applies to ur crate gets fixed automatically (if it can be fixed), and whatever warnings are left over get printed for u when the check finishes. so like, if u wanna apply all the fixes to the current package, u just run:

cargo fix

and that behaves the same as cargo check --all-targets. cute right?? 🫢

here's the catch tho β€” cargo fix can only fix code that gets compiled normally by cargo check. if some code is conditionally enabled behind optional features, u gotta turn those features on so that code actually gets analyzed:

cargo fix --features foo

same energy for other cfg expressions, like platform-specific code β€” those need --target passed so the code for that target gets fixed:

cargo fix --target x86_64-pc-windows-gnu

if u hit any problems with cargo fix, or u just have questions or feature requests, pls pls pls don't be shy β€” file an issue at https://github.com/rust-lang/cargo πŸ’œ


πŸ¦‹ Edition migration πŸ¦‹

plot twist: the cargo fix subcommand can ALSO migrate a package from one edition to the next. the link for the full lore is https://doc.rust-lang.org/edition-guide/editions/transitioning-an-existing-project-to-a-new-edition.html.

the general procedure goes like this:

  1. Run cargo fix --edition. consider also using the --all-features flag if ur project has multiple features. u may also wanna run cargo fix --edition multiple times with different --target flags if ur project has platform-specific code gated by cfg attributes.
  2. Modify Cargo.toml to set the edition field (https://doc.rust-lang.org/cargo/reference/manifest.html#the-edition-field) to the new edition.
  3. Run ur project tests to verify everything still works. if new warnings show up, u might wanna consider running cargo fix again (without the --edition flag) to apply any suggestions the compiler hands u.

and hopefully that's literally it!! just keep in mind those caveats from up above: cargo fix cannot update code for inactive features or cfg expressions. also, in some rare cases the compiler can't automatically migrate all the code to the new edition, and that may require manual changes after building with the new edition. no cap 😭

πŸ‘‘ OPTIONS πŸ‘‘

πŸ’… Fix options πŸ’…

  • --broken-code β€” fix code even if it already has compiler errors. this is useful if cargo fix fails to apply the changes. it'll apply the changes and leave the broken code sitting in the working directory for u to inspect and manually fix. (yes, on purpose. it's giving "trust the process" πŸ™ˆ)
  • --edition β€” apply changes that will update the code to the next edition. heads up: this will not update the edition in the Cargo.toml manifest, which has to be updated manually after cargo fix --edition finishes.
  • --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 πŸ“¦

so by default, when u don't give any package selection options, the packages that get selected depend on the selected manifest file (which is based on the current working directory if --manifest-path isn't given). if the manifest is the root of a workspace, then the workspace's default members get 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. see cargo-pkgid(1) for the SPEC format. this flag may be specified multiple times and supports common Unix glob patterns like *, ? and []. however!! to avoid ur shell accidentally expanding glob patterns before Cargo gets to handle them, u must use single quotes or double quotes around each pattern. don't skip this step bestie πŸ™
  • --workspace β€” fix all members in the workspace.
  • --all β€” deprecated alias for --workspace. (old but still here, like that one playlist u never delete πŸ’€)
  • --exclude *SPEC*… β€” exclude the specified packages. must be used in conjunction with the --workspace flag. this flag may be specified multiple times and supports common Unix glob patterns like *, ? and []. and same rule as before: to avoid ur shell accidentally expanding glob patterns before Cargo handles them, u 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 get skipped if they have required-features that are missing.

passing target selection flags will fix only the specified targets.

also note that --bin, --example, --test and --bench flags also support common Unix glob patterns like *, ? and []. and yes, once again: to avoid ur shell accidentally expanding glob patterns before Cargo handles them, u must use single quotes or double quotes around each glob pattern. it's a whole thing, 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 the test = true manifest 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 the test flag in the manifest settings for the target. double-build, double-slay, double the wait 😭
  • --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 the bench = true manifest 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 the bench flag 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" option fr fr πŸ₯―

🌈 Feature Selection 🌈

the feature flags let u control which features are enabled. when no feature options are given, the default feature is activated for every selected package.

more deets live in the features documentation at https://doc.rust-lang.org/cargo/reference/features.html#command-line-feature-options.

  • -F features, --features features β€” space or comma separated list of features to activate. features of workspace members may be enabled with package-name/feature-name syntax. 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 πŸ› οΈ

  • --target triple β€” fix for the specified target architecture. this 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 ur cross-compiling some crates and don't wanna specify ur host's machine as a target (for instance, an xtask in a shared project that may be worked on by many hosts).
  • a path to a custom target specification. see Custom Target Lookup Path at https://doc.rust-lang.org/rustc/targets/custom.html#custom-target-lookup-path for more information.

this may also be specified with the build.target config value at 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 the release profile. see also the --profile option for choosing a specific profile by name.
  • --profile name β€” 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 on how long each compilation takes, and track concurrency information over time. accepts an optional comma-separated list of output formats; --timings without 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, so you need -Zunstable-options for this one, bestie. It writes a human-readable file called cargo-timing.html into the target/cargo-timings directory, with a whole report of the compilation in it. It also drops a report into that same directory with a timestamp in the filename, so you can go back and snoop on your older runs like the archive girlie you are. Heads up though: HTML output is for human consumption only β€” it does not give you machine-readable timing data. πŸ’…
  • json β€” unstable too, also requires -Zunstable-options. This one emits machine-readable JSON info about timing information. Robots love it. πŸ€–βœ¨

🫢 Output Options 🫢

  • --target-dir directory β€” the directory where all your generated artifacts and intermediate files go. You can also set this with the CARGO_TARGET_DIR environment variable, or with the build.target-dir config value (https://doc.rust-lang.org/cargo/reference/config.html). If you don't say anything, it defaults to target in the root of the workspace. 🎯

🌈 Display Options 🌈

  • -v, --verbose β€” verbose output, aka the tea-spilling mode. You can pass it twice for "very verbose" output, which throws in extra stuff like dependency warnings and build script output. You can also set this with the term.verbose config value (https://doc.rust-lang.org/cargo/reference/config.html). πŸ‘€
  • -q, --quiet β€” do not print cargo log messages. Silence, iconic, peaceful. May also be set with the term.quiet config value (https://doc.rust-lang.org/cargo/reference/config.html). 🀫
  • --color when β€” controls when colored output happens. Valid values, periodt:
    • auto (this is the default) β€” automatically detects whether your terminal can do color support.
    • always β€” always display colors. no thoughts, just vibrancy. 🌈
    • never β€” never display colors. greyscale era.

May also be set with the term.color config value (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 made of comma-separated values. Valid values:
    • human (the default) β€” display in a human-readable text format. Conflicts with short and json.
    • short β€” emit shorter, human-readable text messages. Conflicts with human and json.
    • 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 with human and short.
    • json-diagnostic-short β€” makes sure the rendered field of JSON messages contains the "short" rendering from rustc. Cannot be used with human or short.
    • json-diagnostic-rendered-ansi β€” makes sure the rendered field of JSON messages contains embedded ANSI color codes, so it respects rustc's default color scheme. Cannot be used with human or short.
    • json-render-diagnostics β€” tells Cargo to not include rustc diagnostics in the JSON messages it prints, and instead have Cargo itself render the JSON diagnostics coming from rustc. Cargo's own JSON diagnostics and the other ones coming from rustc are still emitted. Cannot be used with human or short.

πŸŽ€ Manifest Options πŸŽ€

  • --manifest-path path β€” path to the Cargo.toml file. By default, Cargo goes looking for Cargo.toml in the current directory, or any parent directory. like it's searching for its friends in a crowd. πŸ•΅οΈβ€β™€οΈ
  • --ignore-rust-version β€” ignore the rust-version specification in packages. who even is she πŸ’€
  • --locked β€” asserts that the exact same dependencies and versions are used as when the existing Cargo.lock file was originally generated. Cargo will exit with an error when either of these scenarios pops up:
    • The lock file is missing.
    • Cargo attempted to change the lock file due to a different dependency resolution.

You'd use this in environments where you want deterministic builds, like CI pipelines. no surprises, no plot twists. πŸ”’

  • --offline β€” stops Cargo from accessing the network for any reason. Without this flag, Cargo will stop with an error if it needs the network and the network isn't available. With this flag, Cargo will try to proceed without the network if it possibly can.
⚠️ Beware, bestie β€” this may result in a 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 set with the net.offline config value (https://doc.rust-lang.org/cargo/reference/config.html). ✈️

  • --frozen β€” equivalent to specifying both --locked and --offline at the same time. two for one, slay. 🧊
  • --lockfile-path PATH β€” changes the path of the lockfile from the default (<workspace_root>/Cargo.lock) to PATH. PATH must end with Cargo.lock, e.g. --lockfile-path /tmp/temporary-lockfile/Cargo.lock. Note that providing --lockfile-path will ignore the 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. You can use this flag to run most commands in read-only directories, writing the 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-options flag to enable β€” see #14421 (https://github.com/rust-lang/cargo/issues/14421). πŸŒ™

πŸ’œ Common Options πŸ’œ

  • + toolchain β€” if Cargo was installed with rustup, and the first argument to cargo begins with +, it gets interpreted as a rustup toolchain name (like +stable or +nightly). See the rustup documentation at https://rust-lang.github.io/rustup/overrides.html for more info about how toolchain overrides work. ✨
  • --config KEY=VALUE or PATH β€” overrides a Cargo configuration value. The argument should be in TOML syntax of KEY=VALUE, or given as a path to an extra configuration file. You can specify this flag multiple times. See the command-line overrides section at https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides for more info. πŸ”§
  • -C PATH β€” changes the current working directory before executing any specified operations. This affects stuff like where cargo looks by default for the project manifest (Cargo.toml), and also the directories it searches to discover .cargo/config.toml, for example. This option must appear before the command name, like this:
cargo -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. your bestie when you're lost. 🫢
  • -Z flag β€” unstable (nightly-only) flags for Cargo. Run cargo -Z help for details. πŸ§ͺ

πŸ’… Miscellaneous Options πŸ’…

  • -j N, --jobs N β€” the number of parallel jobs to run. May also be set with the build.jobs config value (https://doc.rust-lang.org/cargo/reference/config.html). It defaults to the number of logical CPUs. If it's negative, it sets the maximum number of parallel jobs to the number of logical CPUs plus the value you provided. If a string default is provided, it sets the value back to defaults. And it 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 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. resilience, main character energy. πŸ‘‘


🌸 ENVIRONMENT 🌸

See the reference at https://doc.rust-lang.org/cargo/reference/environment-variables.html for all the deets on environment variables that Cargo reads. 🌱


⭐ EXIT STATUS ⭐

  • 0 β€” Cargo succeeded. we ate. πŸŽ‰
  • 101 β€” Cargo failed to complete. it's giving tragedy. 😭

πŸ¦‹ EXAMPLES πŸ¦‹

  1. Apply compiler suggestions to the local package:
cargo fix
  1. Update a package to prepare it for the next edition:
cargo fix --edition
  1. Apply suggested idioms for the current edition:
cargo fix --edition-idioms

πŸ‘‘ SEE ALSO πŸ‘‘

cargo(1), cargo-check(1) β€” go read those too, bestie, they're iconic. 🫢


and that's the whole page, fr fr πŸ’– this was CARGO-FIX(1) β€” that's the source/version tag at the bottom, your little footer crown, dated and everything. now go fix your code, you absolute legend βœ¨πŸŽ€

πŸ“¦ where this page comes from