Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initial working version of cargo fix --clippy #7069

Merged
merged 22 commits into from
Jul 19, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/bin/cargo/commands/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ pub fn cli() -> App {
.long("allow-staged")
.help("Fix code even if the working directory has staged changes"),
)
.arg(
Arg::with_name("clippy")
.long("clippy")
.help("Get fix suggestions from clippy instead of check")
yaahc marked this conversation as resolved.
Show resolved Hide resolved
.hidden(true),
yaahc marked this conversation as resolved.
Show resolved Hide resolved
)
.arg(
Arg::with_name("clippy-arg")
yaahc marked this conversation as resolved.
Show resolved Hide resolved
.long("clippy-arg")
.help("Args to pass through to clippy, implies --clippy")
.hidden(true)
.multiple(true)
.number_of_values(1),
)
.after_help(
"\
This Cargo subcommand will automatically take rustc's suggestions from
Expand Down Expand Up @@ -125,6 +139,16 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
// code as we can.
let mut opts = args.compile_options(config, mode, Some(&ws))?;

let clippy_args = args.values_of_lossy("clippy-args");
let use_clippy = args.is_present("clippy") || clippy_args.is_some();

if use_clippy && !config.cli_unstable().unstable_options {
return Err(failure::format_err!(
"`cargo fix --clippy` is unstable, pass `-Z unstable-options` to enable it"
)
.into());
}

if let CompileFilter::Default { .. } = opts.filter {
opts.filter = CompileFilter::Only {
all_targets: true,
Expand All @@ -146,6 +170,8 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
allow_no_vcs: args.is_present("allow-no-vcs"),
allow_staged: args.is_present("allow-staged"),
broken_code: args.is_present("broken-code"),
use_clippy,
clippy_args,
},
)?;
Ok(())
Expand Down
35 changes: 34 additions & 1 deletion src/cargo/ops/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ const BROKEN_CODE_ENV: &str = "__CARGO_FIX_BROKEN_CODE";
const PREPARE_FOR_ENV: &str = "__CARGO_FIX_PREPARE_FOR";
const EDITION_ENV: &str = "__CARGO_FIX_EDITION";
const IDIOMS_ENV: &str = "__CARGO_FIX_IDIOMS";
const CLIPPY_FIX_ENV: &str = "__CARGO_FIX_CLIPPY_PLZ";
const CLIPPY_FIX_ARGS: &str = "__CARGO_FIX_CLIPPY_ARGS";
const CLIPPY_PASSHTHROUGH_SEP: &str = "__CLIPPYSEP__";

pub struct FixOptions<'a> {
pub edition: bool,
Expand All @@ -73,6 +76,8 @@ pub struct FixOptions<'a> {
pub allow_no_vcs: bool,
pub allow_staged: bool,
pub broken_code: bool,
pub use_clippy: bool,
pub clippy_args: Option<Vec<String>>,
yaahc marked this conversation as resolved.
Show resolved Hide resolved
}

pub fn fix(ws: &Workspace<'_>, opts: &mut FixOptions<'_>) -> CargoResult<()> {
Expand All @@ -99,6 +104,14 @@ pub fn fix(ws: &Workspace<'_>, opts: &mut FixOptions<'_>) -> CargoResult<()> {
wrapper.env(IDIOMS_ENV, "1");
}

if opts.use_clippy {
wrapper.env(CLIPPY_FIX_ENV, "1");
}

if let Some(clippy_args) = &opts.clippy_args {
wrapper.env(CLIPPY_FIX_ARGS, clippy_args.join(CLIPPY_PASSHTHROUGH_SEP));
yaahc marked this conversation as resolved.
Show resolved Hide resolved
}

*opts
.compile_opts
.build_config
Expand Down Expand Up @@ -385,6 +398,7 @@ fn rustfix_and_fix(

let mut cmd = Command::new(rustc);
cmd.arg("--error-format=json");

yaahc marked this conversation as resolved.
Show resolved Hide resolved
args.apply(&mut cmd);
let output = cmd
.output()
Expand Down Expand Up @@ -565,6 +579,7 @@ struct FixArgs {
other: Vec<OsString>,
primary_package: bool,
rustc: Option<PathBuf>,
clippy_args: Vec<String>,
}

enum PrepareFor {
Expand All @@ -582,7 +597,19 @@ impl Default for PrepareFor {
impl FixArgs {
fn get() -> FixArgs {
let mut ret = FixArgs::default();
ret.rustc = env::args_os().nth(1).map(PathBuf::from);
if env::var(CLIPPY_FIX_ENV).is_ok() {
ret.rustc = Some(PathBuf::from("clippy-driver"));
yaahc marked this conversation as resolved.
Show resolved Hide resolved
} else {
ret.rustc = env::args_os().nth(1).map(PathBuf::from);
}

if let Ok(clippy_args) = env::var(CLIPPY_FIX_ARGS) {
ret.clippy_args = clippy_args
.split(CLIPPY_PASSHTHROUGH_SEP)
.map(ToString::to_string)
.collect();
}

for arg in env::args_os().skip(2) {
let path = PathBuf::from(arg);
if path.extension().and_then(|s| s.to_str()) == Some("rs") && path.exists() {
Expand All @@ -608,6 +635,7 @@ impl FixArgs {
} else if env::var(EDITION_ENV).is_ok() {
ret.prepare_for_edition = PrepareFor::Next;
}

ret.idioms = env::var(IDIOMS_ENV).is_ok();
ret.primary_package = env::var("CARGO_PRIMARY_PACKAGE").is_ok();
ret
Expand All @@ -617,6 +645,11 @@ impl FixArgs {
if let Some(path) = &self.file {
cmd.arg(path);
}

if !self.clippy_args.is_empty() {
cmd.args(&self.clippy_args);
}

cmd.args(&self.other).arg("--cap-lints=warn");
if let Some(edition) = &self.enabled_edition {
cmd.arg("--edition").arg(edition);
Expand Down