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