Skip to content

Commit 8b5c351

Browse files
authored
fix: default to all targets when using --edition and --edition-idioms in cargo fix (#15192)
### What does this PR try to resolve? Close #13527 As we discussed, `cargo fix` should use the default target selection with `cargo check`. In this PR, I modified `cargo fix` to no longer use all targets by default. For `cargo fix --edition` and `cargo fix --edition-idioms`, it will retain the old behavior and select all targets. ### How should we test and review this PR? Unit tests ### Additional information
2 parents 8bf154c + a6eb2bd commit 8b5c351

File tree

2 files changed

+104
-5
lines changed

2 files changed

+104
-5
lines changed

src/bin/cargo/commands/fix.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,11 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
8181

8282
let mut opts = args.compile_options(gctx, mode, Some(&ws), ProfileChecking::LegacyTestOnly)?;
8383

84-
if !opts.filter.is_specific() {
85-
// cargo fix with no target selection implies `--all-targets`.
84+
let edition = args.flag("edition") || args.flag("edition-idioms");
85+
if !opts.filter.is_specific() && edition {
86+
// When `cargo fix` is run without specifying targets but with `--edition` or `--edition-idioms`,
87+
// it should default to fixing all targets.
88+
// See: https://github.com/rust-lang/cargo/issues/13527
8689
opts.filter = ops::CompileFilter::new_all_targets();
8790
}
8891

tests/testsuite/fix.rs

+99-3
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,103 @@ fn prepare_for_2018() {
197197
.contains("let x = crate::foo::FOO;"));
198198
}
199199

200+
#[cargo_test]
201+
fn fix_tests_with_edition() {
202+
let p = project()
203+
.file(
204+
"Cargo.toml",
205+
r#"
206+
[package]
207+
name = "foo"
208+
version = "0.1.0"
209+
edition = "2018"
210+
"#,
211+
)
212+
.file(
213+
"src/lib.rs",
214+
r#"
215+
#![allow(ellipsis_inclusive_range_patterns)]
216+
pub fn foo() {}
217+
218+
#[cfg(test)]
219+
mod tests {
220+
#[test]
221+
fn it_works() {
222+
f();
223+
}
224+
fn f() -> bool {
225+
let x = 123;
226+
match x {
227+
0...100 => true,
228+
_ => false,
229+
}
230+
}
231+
}
232+
"#,
233+
)
234+
.build();
235+
236+
p.cargo("fix --edition --allow-no-vcs")
237+
.with_stderr_data(str![[r#"
238+
[MIGRATING] Cargo.toml from 2018 edition to 2021
239+
[CHECKING] foo v0.1.0 ([ROOT]/foo)
240+
[MIGRATING] src/lib.rs from 2018 edition to 2021
241+
[FIXED] src/lib.rs (1 fix)
242+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
243+
244+
"#]])
245+
.with_stdout_data("")
246+
.run();
247+
// Check that the test is fixed.
248+
assert!(p.read_file("src/lib.rs").contains(r#"0..=100 => true,"#));
249+
}
250+
251+
#[cargo_test]
252+
fn fix_tests_with_edition_idioms() {
253+
let p = project()
254+
.file(
255+
"Cargo.toml",
256+
r#"
257+
[package]
258+
name = 'foo'
259+
version = '0.1.0'
260+
edition = '2018'
261+
"#,
262+
)
263+
.file(
264+
"src/lib.rs",
265+
r#"
266+
pub fn foo() {}
267+
268+
#[cfg(test)]
269+
mod tests {
270+
#[test]
271+
fn it_works() {
272+
f();
273+
}
274+
275+
use std::any::Any;
276+
pub fn f() {
277+
let _x: Box<Any> = Box::new(3);
278+
}
279+
}
280+
"#,
281+
)
282+
.build();
283+
284+
p.cargo("fix --edition-idioms --allow-no-vcs")
285+
.with_stderr_data(str![[r#"
286+
[CHECKING] foo v0.1.0 ([ROOT]/foo)
287+
[FIXED] src/lib.rs (1 fix)
288+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
289+
290+
"#]])
291+
.with_stdout_data("")
292+
.run();
293+
// Check that the test is fixed.
294+
assert!(p.read_file("src/lib.rs").contains("Box<dyn Any>"));
295+
}
296+
200297
#[cargo_test]
201298
fn local_paths() {
202299
let p = project()
@@ -708,7 +805,7 @@ fn does_not_warn_about_dirty_ignored_files() {
708805
}
709806

710807
#[cargo_test]
711-
fn fix_all_targets_by_default() {
808+
fn do_not_fix_tests_by_default() {
712809
let p = project()
713810
.file("src/lib.rs", "pub fn foo() { let mut x = 3; let _ = x; }")
714811
.file("tests/foo.rs", "pub fn foo() { let mut x = 3; let _ = x; }")
@@ -717,7 +814,7 @@ fn fix_all_targets_by_default() {
717814
.env("__CARGO_FIX_YOLO", "1")
718815
.run();
719816
assert!(!p.read_file("src/lib.rs").contains("let mut x"));
720-
assert!(!p.read_file("tests/foo.rs").contains("let mut x"));
817+
assert!(p.read_file("tests/foo.rs").contains("let mut x"));
721818
}
722819

723820
#[cargo_test]
@@ -1330,7 +1427,6 @@ fn fix_to_broken_code() {
13301427
p.cargo("fix --allow-no-vcs --broken-code")
13311428
.cwd("bar")
13321429
.env("RUSTC", p.root().join("foo/target/debug/foo"))
1333-
.with_status(101)
13341430
.with_stderr_data(str![[r#"
13351431
...
13361432
[WARNING] failed to automatically apply fixes suggested by rustc to crate `bar`

0 commit comments

Comments
 (0)