Skip to content

Commit 8b2b415

Browse files
authored
Improve error reporting for misplaced deps, and mention fix flag if not used (#318)
Opted to change the misplaced error message to a more actionable message. "misplaced dependencies" could be confusing, while "move to dev-dependencies" is more obvious what the solution is. Also mentioned that --fix exists, if it wasn't already used.
1 parent bb2db98 commit 8b2b415

File tree

2 files changed

+52
-10
lines changed

2 files changed

+52
-10
lines changed

src/lib.rs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,10 @@ pub struct CargoShear<W> {
148148
/// Counter for total unused dependencies found
149149
unused_dependencies: usize,
150150

151-
/// Counter for dependencies that were fixed (removed)
151+
/// Counter for total misplaced dependencies found
152+
misplaced_dependencies: usize,
153+
154+
/// Counter for dependencies that were fixed
152155
fixed_dependencies: usize,
153156
}
154157

@@ -171,7 +174,13 @@ impl<W: Write> CargoShear<W> {
171174
/// ```
172175
#[must_use]
173176
pub const fn new(writer: W, options: CargoShearOptions) -> Self {
174-
Self { writer, options, unused_dependencies: 0, fixed_dependencies: 0 }
177+
Self {
178+
writer,
179+
options,
180+
unused_dependencies: 0,
181+
misplaced_dependencies: 0,
182+
fixed_dependencies: 0,
183+
}
175184
}
176185

177186
/// Run the dependency analysis and optionally fix unused dependencies.
@@ -206,9 +215,10 @@ impl<W: Write> CargoShear<W> {
206215
);
207216
}
208217

209-
let has_deps = (self.unused_dependencies - self.fixed_dependencies) > 0;
218+
let total_issues = self.unused_dependencies + self.misplaced_dependencies;
219+
let has_issues = (total_issues - self.fixed_dependencies) > 0;
210220

211-
if has_deps {
221+
if has_issues {
212222
let _ = writeln!(
213223
self.writer,
214224
"\n\
@@ -220,11 +230,16 @@ impl<W: Write> CargoShear<W> {
220230
[workspace.metadata.cargo-shear]\n\
221231
ignored = [\"crate-name\"]\n"
222232
);
233+
234+
if !self.options.fix {
235+
let _ =
236+
writeln!(self.writer, "To automatically fix issues, run with --fix");
237+
}
223238
} else {
224239
let _ = writeln!(self.writer, "No issues detected!");
225240
}
226241

227-
ExitCode::from(u8::from(if self.options.fix { has_fixed } else { has_deps }))
242+
ExitCode::from(u8::from(if self.options.fix { has_fixed } else { has_issues }))
228243
}
229244
Err(err) => {
230245
let _ = writeln!(self.writer, "{err:?}");
@@ -338,15 +353,16 @@ impl<W: Write> CargoShear<W> {
338353
}
339354

340355
if misplaced_count > 0 {
341-
writeln!(self.writer, " misplaced dev dependencies:")?;
356+
writeln!(self.writer, " move to dev-dependencies:")?;
342357
for misplaced_dep in &result.misplaced_dependencies {
343358
writeln!(self.writer, " {misplaced_dep}")?;
344359
}
345360
}
346361

347362
writeln!(self.writer)?;
348363

349-
self.unused_dependencies += unused_count + misplaced_count;
364+
self.unused_dependencies += unused_count;
365+
self.misplaced_dependencies += misplaced_count;
350366

351367
Ok(())
352368
}

tests/integration_tests.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ fn misplaced_detection() -> Result<(), Box<dyn Error>> {
204204
Analyzing .
205205
206206
misplaced -- Cargo.toml:
207-
misplaced dev dependencies:
207+
move to dev-dependencies:
208208
anyhow
209209
210210
@@ -218,6 +218,8 @@ fn misplaced_detection() -> Result<(), Box<dyn Error>> {
218218
219219
[workspace.metadata.cargo-shear]
220220
ignored = ["crate-name"]
221+
222+
To automatically fix issues, run with --fix
221223
"#);
222224

223225
Ok(())
@@ -274,7 +276,7 @@ fn misplaced_renamed_detection() -> Result<(), Box<dyn Error>> {
274276
Analyzing .
275277
276278
misplaced_renamed -- Cargo.toml:
277-
misplaced dev dependencies:
279+
move to dev-dependencies:
278280
anyhow_v1
279281
280282
@@ -288,6 +290,8 @@ fn misplaced_renamed_detection() -> Result<(), Box<dyn Error>> {
288290
289291
[workspace.metadata.cargo-shear]
290292
ignored = ["crate-name"]
293+
294+
To automatically fix issues, run with --fix
291295
"#);
292296

293297
Ok(())
@@ -320,7 +324,7 @@ fn misplaced_table_detection() -> Result<(), Box<dyn Error>> {
320324
Analyzing .
321325
322326
misplaced_table -- Cargo.toml:
323-
misplaced dev dependencies:
327+
move to dev-dependencies:
324328
anyhow
325329
326330
@@ -334,6 +338,8 @@ fn misplaced_table_detection() -> Result<(), Box<dyn Error>> {
334338
335339
[workspace.metadata.cargo-shear]
336340
ignored = ["crate-name"]
341+
342+
To automatically fix issues, run with --fix
337343
"#);
338344

339345
Ok(())
@@ -407,6 +413,8 @@ fn unused_detection() -> Result<(), Box<dyn Error>> {
407413
408414
[workspace.metadata.cargo-shear]
409415
ignored = ["crate-name"]
416+
417+
To automatically fix issues, run with --fix
410418
"#);
411419

412420
Ok(())
@@ -448,6 +456,8 @@ fn unused_build_detection() -> Result<(), Box<dyn Error>> {
448456
449457
[workspace.metadata.cargo-shear]
450458
ignored = ["crate-name"]
459+
460+
To automatically fix issues, run with --fix
451461
"#);
452462

453463
Ok(())
@@ -489,6 +499,8 @@ fn unused_dev_detection() -> Result<(), Box<dyn Error>> {
489499
490500
[workspace.metadata.cargo-shear]
491501
ignored = ["crate-name"]
502+
503+
To automatically fix issues, run with --fix
492504
"#);
493505

494506
Ok(())
@@ -584,6 +596,8 @@ fn unused_naming_hyphen_detection() -> Result<(), Box<dyn Error>> {
584596
585597
[workspace.metadata.cargo-shear]
586598
ignored = ["crate-name"]
599+
600+
To automatically fix issues, run with --fix
587601
"#);
588602

589603
Ok(())
@@ -625,6 +639,8 @@ fn unused_naming_underscore_detection() -> Result<(), Box<dyn Error>> {
625639
626640
[workspace.metadata.cargo-shear]
627641
ignored = ["crate-name"]
642+
643+
To automatically fix issues, run with --fix
628644
"#);
629645

630646
Ok(())
@@ -724,6 +740,8 @@ fn unused_platform_detection() -> Result<(), Box<dyn Error>> {
724740
725741
[workspace.metadata.cargo-shear]
726742
ignored = ["crate-name"]
743+
744+
To automatically fix issues, run with --fix
727745
"#);
728746

729747
Ok(())
@@ -766,6 +784,8 @@ fn unused_renamed_detection() -> Result<(), Box<dyn Error>> {
766784
767785
[workspace.metadata.cargo-shear]
768786
ignored = ["crate-name"]
787+
788+
To automatically fix issues, run with --fix
769789
"#);
770790

771791
Ok(())
@@ -807,6 +827,8 @@ fn unused_table_detection() -> Result<(), Box<dyn Error>> {
807827
808828
[workspace.metadata.cargo-shear]
809829
ignored = ["crate-name"]
830+
831+
To automatically fix issues, run with --fix
810832
"#);
811833

812834
Ok(())
@@ -848,6 +870,8 @@ fn unused_workspace_detection() -> Result<(), Box<dyn Error>> {
848870
849871
[workspace.metadata.cargo-shear]
850872
ignored = ["crate-name"]
873+
874+
To automatically fix issues, run with --fix
851875
"#);
852876

853877
Ok(())
@@ -890,6 +914,8 @@ fn unused_workspace_renamed_detection() -> Result<(), Box<dyn Error>> {
890914
891915
[workspace.metadata.cargo-shear]
892916
ignored = ["crate-name"]
917+
918+
To automatically fix issues, run with --fix
893919
"#);
894920

895921
Ok(())

0 commit comments

Comments
 (0)