1CARGO-FIX(1) General Commands Manual CARGO-FIX(1)
2
3
4
6 cargo-fix — Automatically fix lint warnings reported by rustc
7
9 cargo fix [options]
10
12 This Cargo subcommand will automatically take rustc’s suggestions from
13 diagnostics like warnings and apply them to your source code. This is
14 intended to help automate tasks that rustc itself already knows how to
15 tell you to fix!
16
17 Executing cargo fix will under the hood execute cargo-check(1). Any
18 warnings applicable to your crate will be automatically fixed (if
19 possible) and all remaining warnings will be displayed when the check
20 process is finished. For example if you’d like to apply all fixes to
21 the current package, you can run:
22
23 cargo fix
24
25 which behaves the same as cargo check --all-targets.
26
27 cargo fix is only capable of fixing code that is normally compiled with
28 cargo check. If code is conditionally enabled with optional features,
29 you will need to enable those features for that code to be analyzed:
30
31 cargo fix --features foo
32
33 Similarly, other cfg expressions like platform-specific code will need
34 to pass --target to fix code for the given target.
35
36 cargo fix --target x86_64-pc-windows-gnu
37
38 If you encounter any problems with cargo fix or otherwise have any
39 questions or feature requests please don’t hesitate to file an issue at
40 <https://github.com/rust-lang/cargo>.
41
42 Edition migration
43 The cargo fix subcommand can also be used to migrate a package from one
44 edition
45 <https://doc.rust-lang.org/edition-guide/editions/transitioning-an-existing-project-to-a-new-edition.html>
46 to the next. The general procedure is:
47
48 1. Run cargo fix --edition. Consider also using the --all-features
49 flag if your project has multiple features. You may also want to
50 run cargo fix --edition multiple times with different --target
51 flags if your project has platform-specific code gated by cfg
52 attributes.
53
54 2. Modify Cargo.toml to set the edition field
55 <https://doc.rust-lang.org/cargo/reference/manifest.html#the-edition-field>
56 to the new edition.
57
58 3. Run your project tests to verify that everything still works. If
59 new warnings are issued, you may want to consider running cargo fix
60 again (without the --edition flag) to apply any suggestions given
61 by the compiler.
62
63 And hopefully that’s it! Just keep in mind of the caveats mentioned
64 above that cargo fix cannot update code for inactive features or cfg
65 expressions. Also, in some rare cases the compiler is unable to
66 automatically migrate all code to the new edition, and this may require
67 manual changes after building with the new edition.
68
70 Fix options
71 --broken-code
72 Fix code even if it already has compiler errors. This is useful if
73 cargo fix fails to apply the changes. It will apply the changes and
74 leave the broken code in the working directory for you to inspect
75 and manually fix.
76
77 --edition
78 Apply changes that will update the code to the next edition. This
79 will not update the edition in the Cargo.toml manifest, which must
80 be updated manually after cargo fix --edition has finished.
81
82 --edition-idioms
83 Apply suggestions that will update code to the preferred style for
84 the current edition.
85
86 --allow-no-vcs
87 Fix code even if a VCS was not detected.
88
89 --allow-dirty
90 Fix code even if the working directory has changes.
91
92 --allow-staged
93 Fix code even if the working directory has staged changes.
94
95 Package Selection
96 By default, when no package selection options are given, the packages
97 selected depend on the selected manifest file (based on the current
98 working directory if --manifest-path is not given). If the manifest is
99 the root of a workspace then the workspaces default members are
100 selected, otherwise only the package defined by the manifest will be
101 selected.
102
103 The default members of a workspace can be set explicitly with the
104 workspace.default-members key in the root manifest. If this is not set,
105 a virtual workspace will include all workspace members (equivalent to
106 passing --workspace), and a non-virtual workspace will include only the
107 root crate itself.
108
109 -p spec…, --package spec…
110 Fix only the specified packages. See cargo-pkgid(1) for the SPEC
111 format. This flag may be specified multiple times and supports
112 common Unix glob patterns like *, ? and []. However, to avoid your
113 shell accidentally expanding glob patterns before Cargo handles
114 them, you must use single quotes or double quotes around each
115 pattern.
116
117 --workspace
118 Fix all members in the workspace.
119
120 --all
121 Deprecated alias for --workspace.
122
123 --exclude SPEC…
124 Exclude the specified packages. Must be used in conjunction with
125 the --workspace flag. This flag may be specified multiple times and
126 supports common Unix glob patterns like *, ? and []. However, to
127 avoid your shell accidentally expanding glob patterns before Cargo
128 handles them, you must use single quotes or double quotes around
129 each pattern.
130
131 Target Selection
132 When no target selection options are given, cargo fix will fix all
133 targets (--all-targets implied). Binaries are skipped if they have
134 required-features that are missing.
135
136 Passing target selection flags will fix only the specified targets.
137
138 Note that --bin, --example, --test and --bench flags also support
139 common Unix glob patterns like *, ? and []. However, to avoid your
140 shell accidentally expanding glob patterns before Cargo handles them,
141 you must use single quotes or double quotes around each glob pattern.
142
143 --lib
144 Fix the package’s library.
145
146 --bin name…
147 Fix the specified binary. This flag may be specified multiple times
148 and supports common Unix glob patterns.
149
150 --bins
151 Fix all binary targets.
152
153 --example name…
154 Fix the specified example. This flag may be specified multiple
155 times and supports common Unix glob patterns.
156
157 --examples
158 Fix all example targets.
159
160 --test name…
161 Fix the specified integration test. This flag may be specified
162 multiple times and supports common Unix glob patterns.
163
164 --tests
165 Fix all targets in test mode that have the test = true manifest
166 flag set. By default this includes the library and binaries built
167 as unittests, and integration tests. Be aware that this will also
168 build any required dependencies, so the lib target may be built
169 twice (once as a unittest, and once as a dependency for binaries,
170 integration tests, etc.). Targets may be enabled or disabled by
171 setting the test flag in the manifest settings for the target.
172
173 --bench name…
174 Fix the specified benchmark. This flag may be specified multiple
175 times and supports common Unix glob patterns.
176
177 --benches
178 Fix all targets in benchmark mode that have the bench = true
179 manifest flag set. By default this includes the library and
180 binaries built as benchmarks, and bench targets. Be aware that this
181 will also build any required dependencies, so the lib target may be
182 built twice (once as a benchmark, and once as a dependency for
183 binaries, benchmarks, etc.). Targets may be enabled or disabled by
184 setting the bench flag in the manifest settings for the target.
185
186 --all-targets
187 Fix all targets. This is equivalent to specifying --lib --bins
188 --tests --benches --examples.
189
190 Feature Selection
191 The feature flags allow you to control which features are enabled. When
192 no feature options are given, the default feature is activated for
193 every selected package.
194
195 See the features documentation
196 <https://doc.rust-lang.org/cargo/reference/features.html#command-line-feature-options>
197 for more details.
198
199 -F features, --features features
200 Space or comma separated list of features to activate. Features of
201 workspace members may be enabled with package-name/feature-name
202 syntax. This flag may be specified multiple times, which enables
203 all specified features.
204
205 --all-features
206 Activate all available features of all selected packages.
207
208 --no-default-features
209 Do not activate the default feature of the selected packages.
210
211 Compilation Options
212 --target triple
213 Fix for the given architecture. The default is the host
214 architecture. The general format of the triple is
215 <arch><sub>-<vendor>-<sys>-<abi>. Run rustc --print target-list for
216 a list of supported targets. This flag may be specified multiple
217 times.
218
219 This may also be specified with the build.target config value
220 <https://doc.rust-lang.org/cargo/reference/config.html>.
221
222 Note that specifying this flag makes Cargo run in a different mode
223 where the target artifacts are placed in a separate directory. See
224 the build cache
225 <https://doc.rust-lang.org/cargo/guide/build-cache.html>
226 documentation for more details.
227
228 -r, --release
229 Fix optimized artifacts with the release profile. See also the
230 --profile option for choosing a specific profile by name.
231
232 --profile name
233 Fix with the given profile.
234
235 As a special case, specifying the test profile will also enable
236 checking in test mode which will enable checking tests and enable
237 the test cfg option. See rustc tests
238 <https://doc.rust-lang.org/rustc/tests/index.html> for more detail.
239
240 See the the reference
241 <https://doc.rust-lang.org/cargo/reference/profiles.html> for more
242 details on profiles.
243
244 --ignore-rust-version
245 Fix the target even if the selected Rust compiler is older than the
246 required Rust version as configured in the project’s rust-version
247 field.
248
249 --timings=fmts
250 Output information how long each compilation takes, and track
251 concurrency information over time. Accepts an optional
252 comma-separated list of output formats; --timings without an
253 argument will default to --timings=html. Specifying an output
254 format (rather than the default) is unstable and requires
255 -Zunstable-options. Valid output formats:
256
257 • html (unstable, requires -Zunstable-options): Write a
258 human-readable file cargo-timing.html to the
259 target/cargo-timings directory with a report of the
260 compilation. Also write a report to the same directory with a
261 timestamp in the filename if you want to look at older runs.
262 HTML output is suitable for human consumption only, and does
263 not provide machine-readable timing data.
264
265 • json (unstable, requires -Zunstable-options): Emit
266 machine-readable JSON information about timing information.
267
268 Output Options
269 --target-dir directory
270 Directory for all generated artifacts and intermediate files. May
271 also be specified with the CARGO_TARGET_DIR environment variable,
272 or the build.target-dir config value
273 <https://doc.rust-lang.org/cargo/reference/config.html>. Defaults
274 to target in the root of the workspace.
275
276 Display Options
277 -v, --verbose
278 Use verbose output. May be specified twice for “very verbose”
279 output which includes extra output such as dependency warnings and
280 build script output. May also be specified with the term.verbose
281 config value
282 <https://doc.rust-lang.org/cargo/reference/config.html>.
283
284 -q, --quiet
285 Do not print cargo log messages. May also be specified with the
286 term.quiet config value
287 <https://doc.rust-lang.org/cargo/reference/config.html>.
288
289 --color when
290 Control when colored output is used. Valid values:
291
292 • auto (default): Automatically detect if color support is
293 available on the terminal.
294
295 • always: Always display colors.
296
297 • never: Never display colors.
298
299 May also be specified with the term.color config value
300 <https://doc.rust-lang.org/cargo/reference/config.html>.
301
302 --message-format fmt
303 The output format for diagnostic messages. Can be specified
304 multiple times and consists of comma-separated values. Valid
305 values:
306
307 • human (default): Display in a human-readable text format.
308 Conflicts with short and json.
309
310 • short: Emit shorter, human-readable text messages. Conflicts
311 with human and json.
312
313 • json: Emit JSON messages to stdout. See the reference
314 <https://doc.rust-lang.org/cargo/reference/external-tools.html#json-messages>
315 for more details. Conflicts with human and short.
316
317 • json-diagnostic-short: Ensure the rendered field of JSON
318 messages contains the “short” rendering from rustc. Cannot be
319 used with human or short.
320
321 • json-diagnostic-rendered-ansi: Ensure the rendered field of JSON
322 messages contains embedded ANSI color codes for respecting
323 rustc’s default color scheme. Cannot be used with human or
324 short.
325
326 • json-render-diagnostics: Instruct Cargo to not include rustc
327 diagnostics in JSON messages printed, but instead Cargo itself
328 should render the JSON diagnostics coming from rustc. Cargo’s
329 own JSON diagnostics and others coming from rustc are still
330 emitted. Cannot be used with human or short.
331
332 Manifest Options
333 --manifest-path path
334 Path to the Cargo.toml file. By default, Cargo searches for the
335 Cargo.toml file in the current directory or any parent directory.
336
337 --frozen, --locked
338 Either of these flags requires that the Cargo.lock file is
339 up-to-date. If the lock file is missing, or it needs to be updated,
340 Cargo will exit with an error. The --frozen flag also prevents
341 Cargo from attempting to access the network to determine if it is
342 out-of-date.
343
344 These may be used in environments where you want to assert that the
345 Cargo.lock file is up-to-date (such as a CI build) or want to avoid
346 network access.
347
348 --offline
349 Prevents Cargo from accessing the network for any reason. Without
350 this flag, Cargo will stop with an error if it needs to access the
351 network and the network is not available. With this flag, Cargo
352 will attempt to proceed without the network if possible.
353
354 Beware that this may result in different dependency resolution than
355 online mode. Cargo will restrict itself to crates that are
356 downloaded locally, even if there might be a newer version as
357 indicated in the local copy of the index. See the cargo-fetch(1)
358 command to download dependencies before going offline.
359
360 May also be specified with the net.offline config value
361 <https://doc.rust-lang.org/cargo/reference/config.html>.
362
363 Common Options
364 +toolchain
365 If Cargo has been installed with rustup, and the first argument to
366 cargo begins with +, it will be interpreted as a rustup toolchain
367 name (such as +stable or +nightly). See the rustup documentation
368 <https://rust-lang.github.io/rustup/overrides.html> for more
369 information about how toolchain overrides work.
370
371 --config KEY=VALUE or PATH
372 Overrides a Cargo configuration value. The argument should be in
373 TOML syntax of KEY=VALUE, or provided as a path to an extra
374 configuration file. This flag may be specified multiple times. See
375 the command-line overrides section
376 <https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
377 for more information.
378
379 -C PATH
380 Changes the current working directory before executing any
381 specified operations. This affects things like where cargo looks by
382 default for the project manifest (Cargo.toml), as well as the
383 directories searched for discovering .cargo/config.toml, for
384 example. This option must appear before the command name, for
385 example cargo -C path/to/my-project build.
386
387 This option is only available on the nightly channel
388 <https://doc.rust-lang.org/book/appendix-07-nightly-rust.html> and
389 requires the -Z unstable-options flag to enable (see #10098
390 <https://github.com/rust-lang/cargo/issues/10098>).
391
392 -h, --help
393 Prints help information.
394
395 -Z flag
396 Unstable (nightly-only) flags to Cargo. Run cargo -Z help for
397 details.
398
399 Miscellaneous Options
400 -j N, --jobs N
401 Number of parallel jobs to run. May also be specified with the
402 build.jobs config value
403 <https://doc.rust-lang.org/cargo/reference/config.html>. Defaults
404 to the number of logical CPUs. If negative, it sets the maximum
405 number of parallel jobs to the number of logical CPUs plus provided
406 value. If a string default is provided, it sets the value back to
407 defaults. Should not be 0.
408
409 --keep-going
410 Build as many crates in the dependency graph as possible, rather
411 than aborting the build on the first one that fails to build.
412
413 For example if the current package depends on dependencies fails
414 and works, one of which fails to build, cargo fix -j1 may or may
415 not build the one that succeeds (depending on which one of the two
416 builds Cargo picked to run first), whereas cargo fix -j1
417 --keep-going would definitely run both builds, even if the one run
418 first fails.
419
421 See the reference
422 <https://doc.rust-lang.org/cargo/reference/environment-variables.html>
423 for details on environment variables that Cargo reads.
424
426 • 0: Cargo succeeded.
427
428 • 101: Cargo failed to complete.
429
431 1. Apply compiler suggestions to the local package:
432
433 cargo fix
434
435 2. Update a package to prepare it for the next edition:
436
437 cargo fix --edition
438
439 3. Apply suggested idioms for the current edition:
440
441 cargo fix --edition-idioms
442
444 cargo(1), cargo-check(1)
445
446
447
448 CARGO-FIX(1)