Skip to content

Commit a71b8fe

Browse files
committed
feat(fix): Migrate underscore toml fields
1 parent bcf032e commit a71b8fe

File tree

2 files changed

+96
-32
lines changed

2 files changed

+96
-32
lines changed

src/cargo/ops/fix.rs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,53 @@ fn migrate_manifests(ws: &Workspace<'_>, pkgs: &[&Package]) -> CargoResult<()> {
254254
let mut fixes = 0;
255255

256256
let root = document.as_table_mut();
257+
258+
if let Some(workspace) = root
259+
.get_mut("workspace")
260+
.and_then(|t| t.as_table_like_mut())
261+
{
262+
// strictly speaking, the edition doesn't apply to this table but it should be safe
263+
// enough
264+
fixes += rename_dep_fields_2024(workspace, "dependencies");
265+
}
266+
257267
fixes += add_feature_for_unused_deps(pkg, root);
258268
if rename_table(root, "project", "package") {
259269
fixes += 1;
260270
}
271+
if let Some(target) = root.get_mut("lib").and_then(|t| t.as_table_like_mut()) {
272+
fixes += rename_target_fields_2024(target);
273+
}
274+
fixes += rename_array_of_target_fields_2024(root, "bin");
275+
fixes += rename_array_of_target_fields_2024(root, "example");
276+
fixes += rename_array_of_target_fields_2024(root, "test");
277+
fixes += rename_array_of_target_fields_2024(root, "bench");
278+
fixes += rename_dep_fields_2024(root, "dependencies");
279+
if rename_table(root, "dev_dependencies", "dev-dependencies") {
280+
fixes += 1;
281+
}
282+
fixes += rename_dep_fields_2024(root, "dev-dependencies");
283+
if rename_table(root, "build_dependencies", "build-dependencies") {
284+
fixes += 1;
285+
}
286+
fixes += rename_dep_fields_2024(root, "build-dependencies");
287+
for target in root
288+
.get_mut("target")
289+
.and_then(|t| t.as_table_like_mut())
290+
.iter_mut()
291+
.flat_map(|t| t.iter_mut())
292+
.filter_map(|(_k, t)| t.as_table_like_mut())
293+
{
294+
fixes += rename_dep_fields_2024(target, "dependencies");
295+
if rename_table(target, "dev_dependencies", "dev-dependencies") {
296+
fixes += 1;
297+
}
298+
fixes += rename_dep_fields_2024(target, "dev-dependencies");
299+
if rename_table(target, "build_dependencies", "build-dependencies") {
300+
fixes += 1;
301+
}
302+
fixes += rename_dep_fields_2024(target, "build-dependencies");
303+
}
261304

262305
if 0 < fixes {
263306
let verb = if fixes == 1 { "fix" } else { "fixes" };
@@ -274,6 +317,46 @@ fn migrate_manifests(ws: &Workspace<'_>, pkgs: &[&Package]) -> CargoResult<()> {
274317
Ok(())
275318
}
276319

320+
fn rename_dep_fields_2024(parent: &mut dyn toml_edit::TableLike, dep_kind: &str) -> usize {
321+
let mut fixes = 0;
322+
for target in parent
323+
.get_mut(dep_kind)
324+
.and_then(|t| t.as_table_like_mut())
325+
.iter_mut()
326+
.flat_map(|t| t.iter_mut())
327+
.filter_map(|(_k, t)| t.as_table_like_mut())
328+
{
329+
if rename_table(target, "default_features", "default-features") {
330+
fixes += 1;
331+
}
332+
}
333+
fixes
334+
}
335+
336+
fn rename_array_of_target_fields_2024(root: &mut dyn toml_edit::TableLike, kind: &str) -> usize {
337+
let mut fixes = 0;
338+
for target in root
339+
.get_mut(kind)
340+
.and_then(|t| t.as_array_of_tables_mut())
341+
.iter_mut()
342+
.flat_map(|t| t.iter_mut())
343+
{
344+
fixes += rename_target_fields_2024(target);
345+
}
346+
fixes
347+
}
348+
349+
fn rename_target_fields_2024(target: &mut dyn toml_edit::TableLike) -> usize {
350+
let mut fixes = 0;
351+
if rename_table(target, "crate_type", "crate-type") {
352+
fixes += 1;
353+
}
354+
if rename_table(target, "proc_macro", "proc-macro") {
355+
fixes += 1;
356+
}
357+
fixes
358+
}
359+
277360
fn rename_table(parent: &mut dyn toml_edit::TableLike, old: &str, new: &str) -> bool {
278361
let Some(old_key) = parent.key(old).cloned() else {
279362
return false;

tests/testsuite/fix.rs

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2129,26 +2129,7 @@ a = {path = "a", default_features = false}
21292129
.with_stderr(
21302130
"\
21312131
[MIGRATING] Cargo.toml from 2021 edition to 2024
2132-
[WARNING] [CWD]/Cargo.toml: `dev_dependencies` is deprecated in favor of `dev-dependencies` and will not work in the 2024 edition
2133-
(in the `foo` package)
2134-
[WARNING] [CWD]/Cargo.toml: `default_features` is deprecated in favor of `default-features` and will not work in the 2024 edition
2135-
(in the `a` dependency)
2136-
[WARNING] [CWD]/Cargo.toml: `build_dependencies` is deprecated in favor of `build-dependencies` and will not work in the 2024 edition
2137-
(in the `foo` package)
2138-
[WARNING] [CWD]/Cargo.toml: `default_features` is deprecated in favor of `default-features` and will not work in the 2024 edition
2139-
(in the `a` dependency)
2140-
[WARNING] [CWD]/Cargo.toml: `dev_dependencies` is deprecated in favor of `dev-dependencies` and will not work in the 2024 edition
2141-
(in the `cfg(any())` platform target)
2142-
[WARNING] [CWD]/Cargo.toml: `default_features` is deprecated in favor of `default-features` and will not work in the 2024 edition
2143-
(in the `a` dependency)
2144-
[WARNING] [CWD]/Cargo.toml: `build_dependencies` is deprecated in favor of `build-dependencies` and will not work in the 2024 edition
2145-
(in the `cfg(any())` platform target)
2146-
[WARNING] [CWD]/Cargo.toml: `default_features` is deprecated in favor of `default-features` and will not work in the 2024 edition
2147-
(in the `a` dependency)
2148-
[WARNING] [CWD]/Cargo.toml: `crate_type` is deprecated in favor of `crate-type` and will not work in the 2024 edition
2149-
(in the `foo` library target)
2150-
[WARNING] [CWD]/Cargo.toml: `crate_type` is deprecated in favor of `crate-type` and will not work in the 2024 edition
2151-
(in the `ex` example target)
2132+
[FIXED] Cargo.toml (11 fixes)
21522133
Locking 2 packages to latest compatible versions
21532134
Checking a v0.0.1 ([CWD]/a)
21542135
[CHECKING] foo v0.0.0 ([CWD])
@@ -2165,7 +2146,7 @@ cargo-features = ["edition2024"]
21652146
21662147
[workspace.dependencies]
21672148
# Before default_features
2168-
a = {path = "a", default_features = false} # After default_features value
2149+
a = {path = "a", default-features = false} # After default_features value
21692150
# After default_features line
21702151
21712152
[package]
@@ -2175,40 +2156,40 @@ edition = "2021"
21752156
[lib]
21762157
name = "foo"
21772158
# Before crate_type
2178-
crate_type = ["staticlib", "dylib"] # After crate_type value
2159+
crate-type = ["staticlib", "dylib"] # After crate_type value
21792160
# After crate_type line
21802161
21812162
[[example]]
21822163
name = "ex"
21832164
path = "examples/ex.rs"
21842165
# Before crate_type
2185-
crate_type = ["proc-macro"] # After crate_type value
2166+
crate-type = ["proc-macro"] # After crate_type value
21862167
# After crate_type line
21872168
21882169
# Before dev_dependencies
2189-
[ dev_dependencies ] # After dev_dependencies header
2170+
[ dev-dependencies ] # After dev_dependencies header
21902171
# After dev_dependencies line
2191-
a = {path = "a", default_features = false}
2172+
a = {path = "a", default-features = false}
21922173
# After dev_dependencies table
21932174
21942175
# Before build_dependencies
2195-
[ build_dependencies ] # After build_dependencies header
2176+
[ build-dependencies ] # After build_dependencies header
21962177
# After build_dependencies line
2197-
a = {path = "a", default_features = false}
2178+
a = {path = "a", default-features = false}
21982179
# After build_dependencies table
21992180
22002181
# Before dev_dependencies
2201-
[ target.'cfg(any())'.dev_dependencies ] # After dev_dependencies header
2182+
[ target.'cfg(any())'.dev-dependencies ] # After dev_dependencies header
22022183
# After dev_dependencies line
2203-
a = {path = "a", default_features = false}
2184+
a = {path = "a", default-features = false}
22042185
# After dev_dependencies table
22052186
22062187
# Before build_dependencies
2207-
[ target.'cfg(any())'.build_dependencies ] # After build_dependencies header
2188+
[ target.'cfg(any())'.build-dependencies ] # After build_dependencies header
22082189
# After build_dependencies line
2209-
a = {path = "a", default_features = false}
2190+
a = {path = "a", default-features = false}
22102191
# After build_dependencies table
2211-
"#
2192+
"#,
22122193
);
22132194
}
22142195

0 commit comments

Comments
 (0)