-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #5723 - alexcrichton:cargo-fix, r=ehuss
Import `cargo fix` directly in to Cargo This commit imports the `cargo fix` subcommand in rust-lang-nursery/rustfix directly into Cargo as a subcommand. This should allow us to ease our distribution story of `cargo fix` as we prepare for the upcoming 2018 edition release. It's been attempted here to make the code as idiomatic as possible for Cargo's own codebase. Additionally all tests from cargo-fix were imported into Cargo's test suite as well. After this lands and is published in nightly the `cargo-fix` command in rust-lang-nursery/rustfix will likely be removed. cc rust-lang/rust#52272
- Loading branch information
Showing
20 changed files
with
1,984 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
use command_prelude::*; | ||
|
||
use cargo::ops; | ||
|
||
pub fn cli() -> App { | ||
subcommand("fix") | ||
.about("Automatically fix lint warnings reported by rustc") | ||
.arg_package_spec( | ||
"Package(s) to fix", | ||
"Fix all packages in the workspace", | ||
"Exclude packages from the fixes", | ||
) | ||
.arg_jobs() | ||
.arg_targets_all( | ||
"Fix only this package's library", | ||
"Fix only the specified binary", | ||
"Fix all binaries", | ||
"Fix only the specified example", | ||
"Fix all examples", | ||
"Fix only the specified test target", | ||
"Fix all tests", | ||
"Fix only the specified bench target", | ||
"Fix all benches", | ||
"Fix all targets (lib and bin targets by default)", | ||
) | ||
.arg_release("Fix artifacts in release mode, with optimizations") | ||
.arg(opt("profile", "Profile to build the selected target for").value_name("PROFILE")) | ||
.arg_features() | ||
.arg_target_triple("Fix for the target triple") | ||
.arg_target_dir() | ||
.arg_manifest_path() | ||
.arg_message_format() | ||
.arg( | ||
Arg::with_name("broken-code") | ||
.long("broken-code") | ||
.help("Fix code even if it already has compiler errors"), | ||
) | ||
.arg( | ||
Arg::with_name("edition") | ||
.long("prepare-for") | ||
.help("Fix warnings in preparation of an edition upgrade") | ||
.takes_value(true) | ||
.possible_values(&["2018"]), | ||
) | ||
.arg( | ||
Arg::with_name("allow-no-vcs") | ||
.long("allow-no-vcs") | ||
.help("Fix code even if a VCS was not detected"), | ||
) | ||
.arg( | ||
Arg::with_name("allow-dirty") | ||
.long("allow-dirty") | ||
.help("Fix code even if the working directory is dirty"), | ||
) | ||
.after_help( | ||
"\ | ||
This Cargo subcommmand will automatically take rustc's suggestions from | ||
diagnostics like warnings and apply them to your source code. This is intended | ||
to help automate tasks that rustc itself already knows how to tell you to fix! | ||
The `cargo fix` subcommand is also being developed for the Rust 2018 edition | ||
to provide code the ability to easily opt-in to the new edition without having | ||
to worry about any breakage. | ||
Executing `cargo fix` will under the hood execute `cargo check`. Any warnings | ||
applicable to your crate will be automatically fixed (if possible) and all | ||
remaining warnings will be displayed when the check process is finished. For | ||
example if you'd like to prepare for the 2018 edition, you can do so by | ||
executing: | ||
cargo fix --prepare-for 2018 | ||
Note that this is not guaranteed to fix all your code as it only fixes code that | ||
`cargo check` would otherwise compile. For example unit tests are left out | ||
from this command, but they can be checked with: | ||
cargo fix --prepare-for 2018 --all-targets | ||
which behaves the same as `cargo check --all-targets`. Similarly if you'd like | ||
to fix code for different platforms you can do: | ||
cargo fix --prepare-for 2018 --target x86_64-pc-windows-gnu | ||
or if your crate has optional features: | ||
cargo fix --prepare-for 2018 --no-default-features --features foo | ||
If you encounter any problems with `cargo fix` or otherwise have any questions | ||
or feature requests please don't hesitate to file an issue at | ||
https://github.com/rust-lang/cargo | ||
", | ||
) | ||
} | ||
|
||
pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult { | ||
let ws = args.workspace(config)?; | ||
let test = match args.value_of("profile") { | ||
Some("test") => true, | ||
None => false, | ||
Some(profile) => { | ||
let err = format_err!( | ||
"unknown profile: `{}`, only `test` is \ | ||
currently supported", | ||
profile | ||
); | ||
return Err(CliError::new(err, 101)); | ||
} | ||
}; | ||
let mode = CompileMode::Check { test }; | ||
ops::fix(&ws, &mut ops::FixOptions { | ||
edition: args.value_of("edition"), | ||
compile_opts: args.compile_options(config, mode)?, | ||
allow_dirty: args.is_present("allow-dirty"), | ||
allow_no_vcs: args.is_present("allow-no-vcs"), | ||
broken_code: args.is_present("broken-code"), | ||
})?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.