From 69f398f647986c9152d8cc8dd06f40b81c79eec0 Mon Sep 17 00:00:00 2001 From: Rustin170506 Date: Thu, 6 Mar 2025 21:57:36 +0800 Subject: [PATCH 1/3] test: add tests for fixing edition and edition idioms Signed-off-by: Rustin170506 --- tests/testsuite/fix.rs | 97 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/tests/testsuite/fix.rs b/tests/testsuite/fix.rs index 7d9c4610c..19692aada 100644 --- a/tests/testsuite/fix.rs +++ b/tests/testsuite/fix.rs @@ -197,6 +197,103 @@ fn prepare_for_2018() { .contains("let x = crate::foo::FOO;")); } +#[cargo_test] +fn fix_tests_with_edition() { + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.1.0" + edition = "2018" + "#, + ) + .file( + "src/lib.rs", + r#" + #![allow(ellipsis_inclusive_range_patterns)] + pub fn foo() {} + + #[cfg(test)] + mod tests { + #[test] + fn it_works() { + f(); + } + fn f() -> bool { + let x = 123; + match x { + 0...100 => true, + _ => false, + } + } + } + "#, + ) + .build(); + + p.cargo("fix --edition --allow-no-vcs") + .with_stderr_data(str![[r#" +[MIGRATING] Cargo.toml from 2018 edition to 2021 +[CHECKING] foo v0.1.0 ([ROOT]/foo) +[MIGRATING] src/lib.rs from 2018 edition to 2021 +[FIXED] src/lib.rs (1 fix) +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s + +"#]]) + .with_stdout_data("") + .run(); + // Check that the test is fixed. + assert!(p.read_file("src/lib.rs").contains(r#"0..=100 => true,"#)); +} + +#[cargo_test] +fn fix_tests_with_edition_idioms() { + let p = project() + .file( + "Cargo.toml", + r#" + [package] + name = 'foo' + version = '0.1.0' + edition = '2018' + "#, + ) + .file( + "src/lib.rs", + r#" + pub fn foo() {} + + #[cfg(test)] + mod tests { + #[test] + fn it_works() { + f(); + } + + use std::any::Any; + pub fn f() { + let _x: Box = Box::new(3); + } + } + "#, + ) + .build(); + + p.cargo("fix --edition-idioms --allow-no-vcs") + .with_stderr_data(str![[r#" +[CHECKING] foo v0.1.0 ([ROOT]/foo) +[FIXED] src/lib.rs (1 fix) +[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s + +"#]]) + .with_stdout_data("") + .run(); + // Check that the test is fixed. + assert!(p.read_file("src/lib.rs").contains("Box")); +} + #[cargo_test] fn local_paths() { let p = project() From 75a8cdd5a94a0d30cdaa83784b21713d510f243c Mon Sep 17 00:00:00 2001 From: Rustin170506 Date: Thu, 6 Mar 2025 22:03:32 +0800 Subject: [PATCH 2/3] fix: default to all targets when using --edition and --edition-idioms in cargo fix Signed-off-by: Rustin170506 --- src/bin/cargo/commands/fix.rs | 7 +++++-- tests/testsuite/fix.rs | 6 +++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/bin/cargo/commands/fix.rs b/src/bin/cargo/commands/fix.rs index 38bd75497..8850c8b01 100644 --- a/src/bin/cargo/commands/fix.rs +++ b/src/bin/cargo/commands/fix.rs @@ -81,8 +81,11 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult { let mut opts = args.compile_options(gctx, mode, Some(&ws), ProfileChecking::LegacyTestOnly)?; - if !opts.filter.is_specific() { - // cargo fix with no target selection implies `--all-targets`. + let edition = args.flag("edition") || args.flag("edition-idioms"); + if !opts.filter.is_specific() && edition { + // When `cargo fix` is run without specifying targets but with `--edition` or `--edition-idioms`, + // it should default to fixing all targets. + // See: https://github.com/rust-lang/cargo/issues/13527 opts.filter = ops::CompileFilter::new_all_targets(); } diff --git a/tests/testsuite/fix.rs b/tests/testsuite/fix.rs index 19692aada..e0565fd73 100644 --- a/tests/testsuite/fix.rs +++ b/tests/testsuite/fix.rs @@ -805,7 +805,7 @@ fn does_not_warn_about_dirty_ignored_files() { } #[cargo_test] -fn fix_all_targets_by_default() { +fn do_not_fix_tests_by_default() { let p = project() .file("src/lib.rs", "pub fn foo() { let mut x = 3; let _ = x; }") .file("tests/foo.rs", "pub fn foo() { let mut x = 3; let _ = x; }") @@ -814,7 +814,7 @@ fn fix_all_targets_by_default() { .env("__CARGO_FIX_YOLO", "1") .run(); assert!(!p.read_file("src/lib.rs").contains("let mut x")); - assert!(!p.read_file("tests/foo.rs").contains("let mut x")); + assert!(p.read_file("tests/foo.rs").contains("let mut x")); } #[cargo_test] @@ -1424,7 +1424,7 @@ fn fix_to_broken_code() { p.cargo("build").cwd("foo").run(); // Attempt to fix code, but our shim will always fail the second compile - p.cargo("fix --allow-no-vcs --broken-code") + p.cargo("fix --all-targets --allow-no-vcs --broken-code") .cwd("bar") .env("RUSTC", p.root().join("foo/target/debug/foo")) .with_status(101) From a6eb2bd33484edd7ef91f3bb2d6d99688e0587d3 Mon Sep 17 00:00:00 2001 From: Rustin170506 Date: Wed, 16 Apr 2025 17:23:07 +0800 Subject: [PATCH 3/3] test: use the correct stats code and do not use `--all-targets` Signed-off-by: Rustin170506 --- tests/testsuite/fix.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/testsuite/fix.rs b/tests/testsuite/fix.rs index e0565fd73..e97ec1ca1 100644 --- a/tests/testsuite/fix.rs +++ b/tests/testsuite/fix.rs @@ -1424,10 +1424,9 @@ fn fix_to_broken_code() { p.cargo("build").cwd("foo").run(); // Attempt to fix code, but our shim will always fail the second compile - p.cargo("fix --all-targets --allow-no-vcs --broken-code") + p.cargo("fix --allow-no-vcs --broken-code") .cwd("bar") .env("RUSTC", p.root().join("foo/target/debug/foo")) - .with_status(101) .with_stderr_data(str![[r#" ... [WARNING] failed to automatically apply fixes suggested by rustc to crate `bar`