From 7beddfb52b7d458a5ce55e6150bf43ca05398920 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 25 Apr 2024 13:22:06 -0500 Subject: [PATCH] feat(fix): Migrate underscore toml fields --- src/cargo/ops/fix.rs | 83 ++++++++++++++++++++++++++++++++++++++++++ tests/testsuite/fix.rs | 45 +++++++---------------- 2 files changed, 96 insertions(+), 32 deletions(-) diff --git a/src/cargo/ops/fix.rs b/src/cargo/ops/fix.rs index 743bd4866461..686ae3b318e7 100644 --- a/src/cargo/ops/fix.rs +++ b/src/cargo/ops/fix.rs @@ -254,10 +254,53 @@ fn migrate_manifests(ws: &Workspace<'_>, pkgs: &[&Package]) -> CargoResult<()> { let mut fixes = 0; let root = document.as_table_mut(); + + if let Some(workspace) = root + .get_mut("workspace") + .and_then(|t| t.as_table_like_mut()) + { + // strictly speaking, the edition doesn't apply to this table but it should be safe + // enough + fixes += rename_dep_fields_2024(workspace, "dependencies"); + } + fixes += add_feature_for_unused_deps(pkg, root); if rename_table(root, "project", "package") { fixes += 1; } + if let Some(target) = root.get_mut("lib").and_then(|t| t.as_table_like_mut()) { + fixes += rename_target_fields_2024(target); + } + fixes += rename_array_of_target_fields_2024(root, "bin"); + fixes += rename_array_of_target_fields_2024(root, "example"); + fixes += rename_array_of_target_fields_2024(root, "test"); + fixes += rename_array_of_target_fields_2024(root, "bench"); + fixes += rename_dep_fields_2024(root, "dependencies"); + if rename_table(root, "dev_dependencies", "dev-dependencies") { + fixes += 1; + } + fixes += rename_dep_fields_2024(root, "dev-dependencies"); + if rename_table(root, "build_dependencies", "build-dependencies") { + fixes += 1; + } + fixes += rename_dep_fields_2024(root, "build-dependencies"); + for target in root + .get_mut("target") + .and_then(|t| t.as_table_like_mut()) + .iter_mut() + .flat_map(|t| t.iter_mut()) + .filter_map(|(_k, t)| t.as_table_like_mut()) + { + fixes += rename_dep_fields_2024(target, "dependencies"); + if rename_table(target, "dev_dependencies", "dev-dependencies") { + fixes += 1; + } + fixes += rename_dep_fields_2024(target, "dev-dependencies"); + if rename_table(target, "build_dependencies", "build-dependencies") { + fixes += 1; + } + fixes += rename_dep_fields_2024(target, "build-dependencies"); + } if 0 < fixes { let verb = if fixes == 1 { "fix" } else { "fixes" }; @@ -274,6 +317,46 @@ fn migrate_manifests(ws: &Workspace<'_>, pkgs: &[&Package]) -> CargoResult<()> { Ok(()) } +fn rename_dep_fields_2024(parent: &mut dyn toml_edit::TableLike, dep_kind: &str) -> usize { + let mut fixes = 0; + for target in parent + .get_mut(dep_kind) + .and_then(|t| t.as_table_like_mut()) + .iter_mut() + .flat_map(|t| t.iter_mut()) + .filter_map(|(_k, t)| t.as_table_like_mut()) + { + if rename_table(target, "default_features", "default-features") { + fixes += 1; + } + } + fixes +} + +fn rename_array_of_target_fields_2024(root: &mut dyn toml_edit::TableLike, kind: &str) -> usize { + let mut fixes = 0; + for target in root + .get_mut(kind) + .and_then(|t| t.as_array_of_tables_mut()) + .iter_mut() + .flat_map(|t| t.iter_mut()) + { + fixes += rename_target_fields_2024(target); + } + fixes +} + +fn rename_target_fields_2024(target: &mut dyn toml_edit::TableLike) -> usize { + let mut fixes = 0; + if rename_table(target, "crate_type", "crate-type") { + fixes += 1; + } + if rename_table(target, "proc_macro", "proc-macro") { + fixes += 1; + } + fixes +} + fn rename_table(parent: &mut dyn toml_edit::TableLike, old: &str, new: &str) -> bool { let Some(old_key) = parent.key(old).cloned() else { return false; diff --git a/tests/testsuite/fix.rs b/tests/testsuite/fix.rs index 179d7d9c2753..2eb3c79ae54a 100644 --- a/tests/testsuite/fix.rs +++ b/tests/testsuite/fix.rs @@ -2129,26 +2129,7 @@ a = {path = "a", default_features = false} .with_stderr( "\ [MIGRATING] Cargo.toml from 2021 edition to 2024 -[WARNING] [CWD]/Cargo.toml: `dev_dependencies` is deprecated in favor of `dev-dependencies` and will not work in the 2024 edition -(in the `foo` package) -[WARNING] [CWD]/Cargo.toml: `default_features` is deprecated in favor of `default-features` and will not work in the 2024 edition -(in the `a` dependency) -[WARNING] [CWD]/Cargo.toml: `build_dependencies` is deprecated in favor of `build-dependencies` and will not work in the 2024 edition -(in the `foo` package) -[WARNING] [CWD]/Cargo.toml: `default_features` is deprecated in favor of `default-features` and will not work in the 2024 edition -(in the `a` dependency) -[WARNING] [CWD]/Cargo.toml: `dev_dependencies` is deprecated in favor of `dev-dependencies` and will not work in the 2024 edition -(in the `cfg(any())` platform target) -[WARNING] [CWD]/Cargo.toml: `default_features` is deprecated in favor of `default-features` and will not work in the 2024 edition -(in the `a` dependency) -[WARNING] [CWD]/Cargo.toml: `build_dependencies` is deprecated in favor of `build-dependencies` and will not work in the 2024 edition -(in the `cfg(any())` platform target) -[WARNING] [CWD]/Cargo.toml: `default_features` is deprecated in favor of `default-features` and will not work in the 2024 edition -(in the `a` dependency) -[WARNING] [CWD]/Cargo.toml: `crate_type` is deprecated in favor of `crate-type` and will not work in the 2024 edition -(in the `foo` library target) -[WARNING] [CWD]/Cargo.toml: `crate_type` is deprecated in favor of `crate-type` and will not work in the 2024 edition -(in the `ex` example target) +[FIXED] Cargo.toml (11 fixes) Locking 2 packages to latest compatible versions Checking a v0.0.1 ([CWD]/a) [CHECKING] foo v0.0.0 ([CWD]) @@ -2165,7 +2146,7 @@ cargo-features = ["edition2024"] [workspace.dependencies] # Before default_features -a = {path = "a", default_features = false} # After default_features value +a = {path = "a", default-features = false} # After default_features value # After default_features line [package] @@ -2175,40 +2156,40 @@ edition = "2021" [lib] name = "foo" # Before crate_type -crate_type = ["staticlib", "dylib"] # After crate_type value +crate-type = ["staticlib", "dylib"] # After crate_type value # After crate_type line [[example]] name = "ex" path = "examples/ex.rs" # Before crate_type -crate_type = ["proc-macro"] # After crate_type value +crate-type = ["proc-macro"] # After crate_type value # After crate_type line # Before dev_dependencies -[ dev_dependencies ] # After dev_dependencies header +[ dev-dependencies ] # After dev_dependencies header # After dev_dependencies line -a = {path = "a", default_features = false} +a = {path = "a", default-features = false} # After dev_dependencies table # Before build_dependencies -[ build_dependencies ] # After build_dependencies header +[ build-dependencies ] # After build_dependencies header # After build_dependencies line -a = {path = "a", default_features = false} +a = {path = "a", default-features = false} # After build_dependencies table # Before dev_dependencies -[ target.'cfg(any())'.dev_dependencies ] # After dev_dependencies header +[ target.'cfg(any())'.dev-dependencies ] # After dev_dependencies header # After dev_dependencies line -a = {path = "a", default_features = false} +a = {path = "a", default-features = false} # After dev_dependencies table # Before build_dependencies -[ target.'cfg(any())'.build_dependencies ] # After build_dependencies header +[ target.'cfg(any())'.build-dependencies ] # After build_dependencies header # After build_dependencies line -a = {path = "a", default_features = false} +a = {path = "a", default-features = false} # After build_dependencies table -"# +"#, ); }