Skip to content

fix: default to all targets when using --edition and --edition-idioms in cargo fix #15192

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
7 changes: 5 additions & 2 deletions src/bin/cargo/commands/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
103 changes: 100 additions & 3 deletions tests/testsuite/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Any> = 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<dyn Any>"));
}

#[cargo_test]
fn local_paths() {
let p = project()
Expand Down Expand Up @@ -708,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; }")
Expand All @@ -717,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]
Expand Down Expand Up @@ -1330,7 +1427,7 @@ fn fix_to_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_status(0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with_status(0) is the default. I think you can just delete the line.

.with_stderr_data(str![[r#"
...
[WARNING] failed to automatically apply fixes suggested by rustc to crate `bar`
Expand Down