1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//! Format-preserving rewrites for `Cargo.toml`. Built on `toml_edit`, so
//! comments, ordering, and styling survive the edit.
//!
//! Covers:
//! - Package-level dependencies (`[dependencies]`, `[dev-dependencies]`, ...).
//! - Workspace dependencies (`[workspace.dependencies]`).
//! - Target-specific dependencies (`[target.'cfg(...)'.{table}]`).
//! - `[features]` entries that name removed dependencies.
//! - Lib-target flags `test` / `doctest`.
use rustc_hash::FxHashSet;
use toml_edit::{DocumentMut, Item, Table, value};
use crate::{
manifest::DepLocation,
package_processor::{MisplacedDependency, UnusedDependency, UnusedWorkspaceDependency},
};
const DEP_TABLE_KEYS: &[&str] = &["dependencies", "dev-dependencies", "build-dependencies"];
/// Stateless namespace for the `Cargo.toml` rewrite operations.
pub struct CargoTomlEditor;
impl CargoTomlEditor {
/// Remove each entry in `unused_deps` from its declared location, scrub any
/// dangling references from `[features]`, and prune now-empty parent tables.
///
/// Returns the number of dependencies actually removed (entries that no
/// longer exist in the manifest are silently skipped).
pub fn remove_dependencies(
manifest: &mut DocumentMut,
unused_deps: &[UnusedDependency],
) -> usize {
let mut removed = FxHashSet::default();
for dep in unused_deps {
let success = match &dep.location {
DepLocation::Root(table) => manifest
.get_mut(&table.to_string())
.and_then(|item| item.as_table_mut())
.and_then(|deps| deps.remove(dep.name.get_ref()))
.is_some(),
DepLocation::Target { cfg, table } => manifest
.get_mut("target")
.and_then(|item| item.as_table_mut())
.and_then(|targets| targets.get_mut(cfg))
.and_then(|item| item.as_table_mut())
.and_then(|target| target.get_mut(&table.to_string()))
.and_then(|item| item.as_table_mut())
.and_then(|deps| deps.remove(dep.name.get_ref()))
.is_some(),
};
if success {
removed.insert(dep.name.get_ref().as_str());
}
}
let count = removed.len();
Self::fix_features(manifest, &removed);
Self::cleanup_empty_tables(manifest);
count
}
/// Remove each entry in `unused_deps` from `[workspace.dependencies]`,
/// scrub any dangling references from `[features]`, and prune the
/// containing tables if they end up empty.
///
/// Returns the number of dependencies actually removed.
pub fn remove_workspace_deps(
manifest: &mut DocumentMut,
unused_deps: &[UnusedWorkspaceDependency],
) -> usize {
let mut removed = FxHashSet::default();
for dep in unused_deps {
let success = manifest
.get_mut("workspace")
.and_then(|item| item.as_table_mut())
.and_then(|workspace| workspace.get_mut("dependencies"))
.and_then(|item| item.as_table_mut())
.and_then(|deps| deps.remove(dep.name.get_ref()))
.is_some();
if success {
removed.insert(dep.name.get_ref().as_str());
}
}
let count = removed.len();
Self::fix_features(manifest, &removed);
Self::cleanup_empty_tables(manifest);
count
}
/// Move each entry from its current (non-dev) table to the matching
/// `dev-dependencies` table, preserving its `cfg` target if any.
///
/// Returns the number of dependencies actually moved.
pub fn move_to_dev_dependencies(
manifest: &mut DocumentMut,
misplaced_deps: &[MisplacedDependency],
) -> usize {
let mut count = 0;
for dep in misplaced_deps {
let success = match &dep.location {
DepLocation::Root(_) => Self::move_root_to_dev(manifest, dep),
DepLocation::Target { .. } => Self::move_target_to_dev(manifest, dep),
};
if success {
count += 1;
}
}
Self::cleanup_empty_tables(manifest);
count
}
fn move_root_to_dev(manifest: &mut DocumentMut, dep: &MisplacedDependency) -> bool {
// Lift the entry out of `[dependencies]` so we can re-insert it elsewhere.
let Some(value) = manifest
.get_mut("dependencies")
.and_then(|item| item.as_table_mut())
.and_then(|deps| deps.remove(dep.name.get_ref()))
else {
return false;
};
// Create `[dev-dependencies]` on the fly if the manifest doesn't have one yet.
if !manifest.contains_key("dev-dependencies") {
manifest["dev-dependencies"] = Item::Table(Table::new());
}
// Re-insert into `[dev-dependencies]`. The earlier `contains_key` check
// means this should always succeed, but stay defensive in case of a
// non-table value at that key.
if let Some(dev_deps) =
manifest.get_mut("dev-dependencies").and_then(|item| item.as_table_mut())
{
dev_deps.insert(dep.name.get_ref(), value);
return true;
}
false
}
fn move_target_to_dev(manifest: &mut DocumentMut, dep: &MisplacedDependency) -> bool {
let DepLocation::Target { cfg, .. } = &dep.location else {
return false;
};
let Some(target) = manifest
.get_mut("target")
.and_then(|item| item.as_table_mut())
.and_then(|targets| targets.get_mut(cfg))
.and_then(|item| item.as_table_mut())
else {
return false;
};
// Lift the entry out of `[target.'cfg(...)'.dependencies]`.
let Some(value) = target
.get_mut("dependencies")
.and_then(|item| item.as_table_mut())
.and_then(|deps| deps.remove(dep.name.get_ref()))
else {
return false;
};
// Create the dev-dependencies table for this `cfg` if it isn't there yet.
if !target.contains_key("dev-dependencies") {
target["dev-dependencies"] = Item::Table(Table::new());
}
// Re-insert into `[target.'cfg(...)'.dev-dependencies]`.
if let Some(dev_deps) =
target.get_mut("dev-dependencies").and_then(|item| item.as_table_mut())
{
dev_deps.insert(dep.name.get_ref(), value);
return true;
}
false
}
/// Remove a single flag (e.g. `test` or `doctest`) from the `[lib]` table.
/// Returns `true` if the key was present and removed.
pub fn remove_lib_flag(manifest: &mut DocumentMut, flag: &str) -> bool {
let removed = manifest
.get_mut("lib")
.and_then(|item| item.as_table_mut())
.and_then(|table| table.remove(flag))
.is_some();
if removed {
Self::cleanup_empty_tables(manifest);
}
removed
}
/// Force a flag to `false` under `[lib]`, creating the table if it's absent.
/// Used to suppress Cargo's defaults of `test = true` / `doctest = true`.
pub fn set_lib_flag_false(manifest: &mut DocumentMut, flag: &str) {
if !manifest.contains_key("lib") {
manifest["lib"] = Item::Table(Table::new());
}
if let Some(table) = manifest.get_mut("lib").and_then(|item| item.as_table_mut()) {
table[flag] = value(false);
}
}
fn cleanup_empty_tables(manifest: &mut DocumentMut) {
// Top-level `[dependencies]` / `[dev-dependencies]` / `[build-dependencies]`.
for key in DEP_TABLE_KEYS {
if manifest.get(key).and_then(|i| i.as_table()).is_some_and(Table::is_empty) {
manifest.remove(key);
}
}
// Target-specific tables, walked bottom-up: snapshot the keys first so
// we can mutate `targets` without invalidating an in-flight iterator.
if let Some(targets) = manifest.get_mut("target").and_then(|i| i.as_table_mut()) {
let target_keys: Vec<String> = targets.iter().map(|(k, _)| k.to_owned()).collect();
for cfg_key in &target_keys {
if let Some(target) = targets.get_mut(cfg_key).and_then(|i| i.as_table_mut()) {
for dep_key in DEP_TABLE_KEYS {
if target
.get(dep_key)
.and_then(|i| i.as_table())
.is_some_and(Table::is_empty)
{
target.remove(dep_key);
}
}
}
if targets.get(cfg_key).and_then(|i| i.as_table()).is_some_and(Table::is_empty) {
targets.remove(cfg_key);
}
}
}
if manifest.get("target").and_then(|i| i.as_table()).is_some_and(Table::is_empty) {
manifest.remove("target");
}
// `[workspace.dependencies]` — only drop the `dependencies` sub-key, not
// the whole `[workspace]` table (it carries members, resolver, etc.).
if let Some(workspace) = manifest.get_mut("workspace").and_then(|i| i.as_table_mut())
&& workspace.get("dependencies").and_then(|i| i.as_table()).is_some_and(Table::is_empty)
{
workspace.remove("dependencies");
}
// `[lib]` — drop the table when it has no remaining keys.
if manifest.get("lib").and_then(|i| i.as_table()).is_some_and(Table::is_empty) {
manifest.remove("lib");
}
}
fn fix_features(manifest: &mut DocumentMut, unused_deps: &FxHashSet<&str>) {
let Some(features) = manifest.get_mut("features").and_then(|item| item.as_table_mut())
else {
return;
};
for (_, deps) in features.iter_mut() {
let Some(list) = deps.as_array_mut() else {
continue;
};
list.retain(|value| {
let Some(value) = value.as_str() else {
return true;
};
let dep = value.strip_prefix("dep:").unwrap_or(value);
!unused_deps.contains(dep)
});
}
}
}