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 --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.
217
218 This may also be specified with the build.target config value
219 <https://doc.rust-lang.org/cargo/reference/config.html>.
220
221 Note that specifying this flag makes Cargo run in a different mode
222 where the target artifacts are placed in a separate directory. See
223 the build cache
224 <https://doc.rust-lang.org/cargo/guide/build-cache.html>
225 documentation for more details.
226
227 --release
228 Fix optimized artifacts with the release profile. See the PROFILES
229 section for details on how this affects profile selection.
230
231 --profile name
232 Changes fix behavior. Currently only test is supported, which will
233 fix with the #[cfg(test)] attribute enabled. This is useful to have
234 it fix unit tests which are usually excluded via the cfg attribute.
235 This does not change the actual profile used.
236
237 --ignore-rust-version
238 Fix the target even if the selected Rust compiler is older than the
239 required Rust version as configured in the project's rust-version
240 field.
241
242 Output Options
243 --target-dir directory
244 Directory for all generated artifacts and intermediate files. May
245 also be specified with the CARGO_TARGET_DIR environment variable,
246 or the build.target-dir config value
247 <https://doc.rust-lang.org/cargo/reference/config.html>. Defaults
248 to target in the root of the workspace.
249
250 Display Options
251 -v, --verbose
252 Use verbose output. May be specified twice for "very verbose"
253 output which includes extra output such as dependency warnings and
254 build script output. May also be specified with the term.verbose
255 config value
256 <https://doc.rust-lang.org/cargo/reference/config.html>.
257
258 -q, --quiet
259 No output printed to stdout.
260
261 --color when
262 Control when colored output is used. Valid values:
263
264 • auto (default): Automatically detect if color support is
265 available on the terminal.
266
267 • always: Always display colors.
268
269 • never: Never display colors.
270
271 May also be specified with the term.color config value
272 <https://doc.rust-lang.org/cargo/reference/config.html>.
273
274 --message-format fmt
275 The output format for diagnostic messages. Can be specified
276 multiple times and consists of comma-separated values. Valid
277 values:
278
279 • human (default): Display in a human-readable text format.
280 Conflicts with short and json.
281
282 • short: Emit shorter, human-readable text messages. Conflicts
283 with human and json.
284
285 • json: Emit JSON messages to stdout. See the reference
286 <https://doc.rust-lang.org/cargo/reference/external-tools.html#json-messages>
287 for more details. Conflicts with human and short.
288
289 • json-diagnostic-short: Ensure the rendered field of JSON
290 messages contains the "short" rendering from rustc. Cannot be
291 used with human or short.
292
293 • json-diagnostic-rendered-ansi: Ensure the rendered field of JSON
294 messages contains embedded ANSI color codes for respecting
295 rustc's default color scheme. Cannot be used with human or
296 short.
297
298 • json-render-diagnostics: Instruct Cargo to not include rustc
299 diagnostics in in JSON messages printed, but instead Cargo
300 itself should render the JSON diagnostics coming from rustc.
301 Cargo's own JSON diagnostics and others coming from rustc are
302 still emitted. Cannot be used with human or short.
303
304 Manifest Options
305 --manifest-path path
306 Path to the Cargo.toml file. By default, Cargo searches for the
307 Cargo.toml file in the current directory or any parent directory.
308
309 --frozen, --locked
310 Either of these flags requires that the Cargo.lock file is
311 up-to-date. If the lock file is missing, or it needs to be updated,
312 Cargo will exit with an error. The --frozen flag also prevents
313 Cargo from attempting to access the network to determine if it is
314 out-of-date.
315
316 These may be used in environments where you want to assert that the
317 Cargo.lock file is up-to-date (such as a CI build) or want to avoid
318 network access.
319
320 --offline
321 Prevents Cargo from accessing the network for any reason. Without
322 this flag, Cargo will stop with an error if it needs to access the
323 network and the network is not available. With this flag, Cargo
324 will attempt to proceed without the network if possible.
325
326 Beware that this may result in different dependency resolution than
327 online mode. Cargo will restrict itself to crates that are
328 downloaded locally, even if there might be a newer version as
329 indicated in the local copy of the index. See the cargo-fetch(1)
330 command to download dependencies before going offline.
331
332 May also be specified with the net.offline config value
333 <https://doc.rust-lang.org/cargo/reference/config.html>.
334
335 Common Options
336 +toolchain
337 If Cargo has been installed with rustup, and the first argument to
338 cargo begins with +, it will be interpreted as a rustup toolchain
339 name (such as +stable or +nightly). See the rustup documentation
340 <https://rust-lang.github.io/rustup/overrides.html> for more
341 information about how toolchain overrides work.
342
343 -h, --help
344 Prints help information.
345
346 -Z flag
347 Unstable (nightly-only) flags to Cargo. Run cargo -Z help for
348 details.
349
350 Miscellaneous Options
351 -j N, --jobs N
352 Number of parallel jobs to run. May also be specified with the
353 build.jobs config value
354 <https://doc.rust-lang.org/cargo/reference/config.html>. Defaults
355 to the number of CPUs.
356
358 Profiles may be used to configure compiler options such as optimization
359 levels and debug settings. See the reference
360 <https://doc.rust-lang.org/cargo/reference/profiles.html> for more
361 details.
362
363 Profile selection depends on the target and crate being built. By
364 default the dev or test profiles are used. If the --release flag is
365 given, then the release or bench profiles are used.
366
367
368 ┌────────────────────┬─────────────────┬───────────────────┐
369 │Target │ Default Profile │ --release Profile │
370 ├────────────────────┼─────────────────┼───────────────────┤
371 │lib, bin, example │ dev │ release │
372 ├────────────────────┼─────────────────┼───────────────────┤
373 │test, bench, or any │ test │ bench │
374 │target in "test" or │ │ │
375 │"bench" mode │ │ │
376 └────────────────────┴─────────────────┴───────────────────┘
377
378 Dependencies use the dev/release profiles.
379
381 See the reference
382 <https://doc.rust-lang.org/cargo/reference/environment-variables.html>
383 for details on environment variables that Cargo reads.
384
386 • 0: Cargo succeeded.
387
388 • 101: Cargo failed to complete.
389
391 1. Apply compiler suggestions to the local package:
392
393 cargo fix
394
395 2. Update a package to prepare it for the next edition:
396
397 cargo fix --edition
398
399 3. Apply suggested idioms for the current edition:
400
401 cargo fix --edition-idioms
402
404 cargo(1), cargo-check(1)
405
406
407
408 CARGO-FIX(1)