Skip to content

Commit ff8a95e

Browse files
committed
fix: Pass --all-targets to cargo fix by default
This'll help fix as much code as possible, including tests! Closes #5739
1 parent d195f28 commit ff8a95e

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/bin/cargo/commands/fix.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use command_prelude::*;
22

3-
use cargo::ops;
3+
use cargo::ops::{self, CompileFilter, FilterRule};
44

55
pub fn cli() -> App {
66
subcommand("fix")
@@ -106,9 +106,26 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
106106
}
107107
};
108108
let mode = CompileMode::Check { test };
109+
110+
// Unlike other commands default `cargo fix` to all targets to fix as much
111+
// code as we can.
112+
let mut opts = args.compile_options(config, mode)?;
113+
match opts.filter {
114+
CompileFilter::Default { .. } => {
115+
opts.filter = CompileFilter::Only {
116+
all_targets: true,
117+
lib: true,
118+
bins: FilterRule::All,
119+
examples: FilterRule::All,
120+
benches: FilterRule::All,
121+
tests: FilterRule::All,
122+
};
123+
}
124+
_ => {}
125+
}
109126
ops::fix(&ws, &mut ops::FixOptions {
110127
edition: args.value_of("edition"),
111-
compile_opts: args.compile_options(config, mode)?,
128+
compile_opts: opts,
112129
allow_dirty: args.is_present("allow-dirty"),
113130
allow_no_vcs: args.is_present("allow-no-vcs"),
114131
broken_code: args.is_present("broken-code"),

tests/testsuite/fix.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,3 +843,18 @@ fn does_not_warn_about_dirty_ignored_files() {
843843
execs().with_status(0),
844844
);
845845
}
846+
847+
#[test]
848+
fn fix_all_targets_by_default() {
849+
let p = project()
850+
.file("src/lib.rs", "pub fn foo() { let mut x = 3; drop(x); }")
851+
.file("tests/foo.rs", "pub fn foo() { let mut x = 3; drop(x); }")
852+
.build();
853+
assert_that(
854+
p.cargo("fix --allow-no-vcs")
855+
.env("__CARGO_FIX_YOLO", "1"),
856+
execs().with_status(0),
857+
);
858+
assert!(!p.read_file("src/lib.rs").contains("let mut x"));
859+
assert!(!p.read_file("tests/foo.rs").contains("let mut x"));
860+
}

0 commit comments

Comments
 (0)