diff --git a/src/bin/cargo/commands/fix.rs b/src/bin/cargo/commands/fix.rs index f552b7d024c..ab0341e04a7 100644 --- a/src/bin/cargo/commands/fix.rs +++ b/src/bin/cargo/commands/fix.rs @@ -1,6 +1,6 @@ use command_prelude::*; -use cargo::ops; +use cargo::ops::{self, CompileFilter, FilterRule}; pub fn cli() -> App { subcommand("fix") @@ -106,9 +106,26 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult { } }; let mode = CompileMode::Check { test }; + + // Unlike other commands default `cargo fix` to all targets to fix as much + // code as we can. + let mut opts = args.compile_options(config, mode)?; + match opts.filter { + CompileFilter::Default { .. } => { + opts.filter = CompileFilter::Only { + all_targets: true, + lib: true, + bins: FilterRule::All, + examples: FilterRule::All, + benches: FilterRule::All, + tests: FilterRule::All, + }; + } + _ => {} + } ops::fix(&ws, &mut ops::FixOptions { edition: args.value_of("edition"), - compile_opts: args.compile_options(config, mode)?, + compile_opts: opts, allow_dirty: args.is_present("allow-dirty"), allow_no_vcs: args.is_present("allow-no-vcs"), broken_code: args.is_present("broken-code"), diff --git a/tests/testsuite/fix.rs b/tests/testsuite/fix.rs index a699de1966b..781fc69f042 100644 --- a/tests/testsuite/fix.rs +++ b/tests/testsuite/fix.rs @@ -843,3 +843,18 @@ fn does_not_warn_about_dirty_ignored_files() { execs().with_status(0), ); } + +#[test] +fn fix_all_targets_by_default() { + let p = project() + .file("src/lib.rs", "pub fn foo() { let mut x = 3; drop(x); }") + .file("tests/foo.rs", "pub fn foo() { let mut x = 3; drop(x); }") + .build(); + assert_that( + p.cargo("fix --allow-no-vcs") + .env("__CARGO_FIX_YOLO", "1"), + execs().with_status(0), + ); + assert!(!p.read_file("src/lib.rs").contains("let mut x")); + assert!(!p.read_file("tests/foo.rs").contains("let mut x")); +}