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.
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 -r, --release
228 Fix optimized artifacts with the release profile. See also the
229 --profile option for choosing a specific profile by name.
230
231 --profile name
232 Fix with the given profile.
233
234 As a special case, specifying the test profile will also enable
235 checking in test mode which will enable checking tests and enable
236 the test cfg option. See rustc tests
237 <https://doc.rust-lang.org/rustc/tests/index.html> for more detail.
238
239 See the the reference
240 <https://doc.rust-lang.org/cargo/reference/profiles.html> for more
241 details on profiles.
242
243 --ignore-rust-version
244 Fix the target even if the selected Rust compiler is older than the
245 required Rust version as configured in the project's rust-version
246 field.
247
248 --timings=fmts
249 Output information how long each compilation takes, and track
250 concurrency information over time. Accepts an optional
251 comma-separated list of output formats; --timings without an
252 argument will default to --timings=html. Specifying an output
253 format (rather than the default) is unstable and requires
254 -Zunstable-options. Valid output formats:
255
256 • html: Write a human-readable file cargo-timing.html to the
257 target/cargo-timings directory with a report of the
258 compilation. Also write a report to the same directory with a
259 timestamp in the filename if you want to look at older runs.
260 HTML output is suitable for human consumption only, and does
261 not provide machine-readable timing data.
262
263 • json (unstable, requires -Zunstable-options): Emit
264 machine-readable JSON information about timing information.
265
266 Output Options
267 --target-dir directory
268 Directory for all generated artifacts and intermediate files. May
269 also be specified with the CARGO_TARGET_DIR environment variable,
270 or the build.target-dir config value
271 <https://doc.rust-lang.org/cargo/reference/config.html>. Defaults
272 to target in the root of the workspace.
273
274 Display Options
275 -v, --verbose
276 Use verbose output. May be specified twice for "very verbose"
277 output which includes extra output such as dependency warnings and
278 build script output. May also be specified with the term.verbose
279 config value
280 <https://doc.rust-lang.org/cargo/reference/config.html>.
281
282 -q, --quiet
283 Do not print cargo log messages. May also be specified with the
284 term.quiet config value
285 <https://doc.rust-lang.org/cargo/reference/config.html>.
286
287 --color when
288 Control when colored output is used. Valid values:
289
290 • auto (default): Automatically detect if color support is
291 available on the terminal.
292
293 • always: Always display colors.
294
295 • never: Never display colors.
296
297 May also be specified with the term.color config value
298 <https://doc.rust-lang.org/cargo/reference/config.html>.
299
300 --message-format fmt
301 The output format for diagnostic messages. Can be specified
302 multiple times and consists of comma-separated values. Valid
303 values:
304
305 • human (default): Display in a human-readable text format.
306 Conflicts with short and json.
307
308 • short: Emit shorter, human-readable text messages. Conflicts
309 with human and json.
310
311 • json: Emit JSON messages to stdout. See the reference
312 <https://doc.rust-lang.org/cargo/reference/external-tools.html#json-messages>
313 for more details. Conflicts with human and short.
314
315 • json-diagnostic-short: Ensure the rendered field of JSON
316 messages contains the "short" rendering from rustc. Cannot be
317 used with human or short.
318
319 • json-diagnostic-rendered-ansi: Ensure the rendered field of JSON
320 messages contains embedded ANSI color codes for respecting
321 rustc's default color scheme. Cannot be used with human or
322 short.
323
324 • json-render-diagnostics: Instruct Cargo to not include rustc
325 diagnostics in in JSON messages printed, but instead Cargo
326 itself should render the JSON diagnostics coming from rustc.
327 Cargo's own JSON diagnostics and others coming from rustc are
328 still emitted. Cannot be used with human or short.
329
330 Manifest Options
331 --manifest-path path
332 Path to the Cargo.toml file. By default, Cargo searches for the
333 Cargo.toml file in the current directory or any parent directory.
334
335 --frozen, --locked
336 Either of these flags requires that the Cargo.lock file is
337 up-to-date. If the lock file is missing, or it needs to be updated,
338 Cargo will exit with an error. The --frozen flag also prevents
339 Cargo from attempting to access the network to determine if it is
340 out-of-date.
341
342 These may be used in environments where you want to assert that the
343 Cargo.lock file is up-to-date (such as a CI build) or want to avoid
344 network access.
345
346 --offline
347 Prevents Cargo from accessing the network for any reason. Without
348 this flag, Cargo will stop with an error if it needs to access the
349 network and the network is not available. With this flag, Cargo
350 will attempt to proceed without the network if possible.
351
352 Beware that this may result in different dependency resolution than
353 online mode. Cargo will restrict itself to crates that are
354 downloaded locally, even if there might be a newer version as
355 indicated in the local copy of the index. See the cargo-fetch(1)
356 command to download dependencies before going offline.
357
358 May also be specified with the net.offline config value
359 <https://doc.rust-lang.org/cargo/reference/config.html>.
360
361 Common Options
362 +toolchain
363 If Cargo has been installed with rustup, and the first argument to
364 cargo begins with +, it will be interpreted as a rustup toolchain
365 name (such as +stable or +nightly). See the rustup documentation
366 <https://rust-lang.github.io/rustup/overrides.html> for more
367 information about how toolchain overrides work.
368
369 -h, --help
370 Prints help information.
371
372 -Z flag
373 Unstable (nightly-only) flags to Cargo. Run cargo -Z help for
374 details.
375
376 Miscellaneous Options
377 -j N, --jobs N
378 Number of parallel jobs to run. May also be specified with the
379 build.jobs config value
380 <https://doc.rust-lang.org/cargo/reference/config.html>. Defaults
381 to the number of CPUs.
382
383 --keep-going
384 Build as many crates in the dependency graph as possible, rather
385 than aborting the build on the first one that fails to build.
386 Unstable, requires -Zunstable-options.
387
389 See the reference
390 <https://doc.rust-lang.org/cargo/reference/environment-variables.html>
391 for details on environment variables that Cargo reads.
392
394 • 0: Cargo succeeded.
395
396 • 101: Cargo failed to complete.
397
399 1. Apply compiler suggestions to the local package:
400
401 cargo fix
402
403 2. Update a package to prepare it for the next edition:
404
405 cargo fix --edition
406
407 3. Apply suggested idioms for the current edition:
408
409 cargo fix --edition-idioms
410
412 cargo(1), cargo-check(1)
413
414
415
416 CARGO-FIX(1)