cargo-shear 1.6.5

Detect and fix unused/misplaced dependencies from Cargo.toml
Documentation
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
//! # cargo-shear
//!
//! A tool for detecting and removing unused dependencies from Rust projects.
//!
//! ## Overview
//!
//! `cargo-shear` analyzes your Rust codebase to identify dependencies that are declared
//! in `Cargo.toml` but never actually used in the code. It can automatically remove
//! these unused dependencies with the `--fix` flag.
//!
//! ## Architecture
//!
//! The codebase is organized into several focused modules:
//!
//! - `cargo_toml_editor` - Handles modifications to Cargo.toml files
//! - `dependency_analyzer` - Analyzes code to find used dependencies
//! - `package_processor` - Processes packages and detects unused dependencies
//! - `import_collector` - Parses Rust source to collect import statements
//! - `error` - Custom error types with detailed context
//!
//! ## Usage
//!
//! ```no_run
//! use cargo_shear::{CargoShear, CargoShearOptions};
//!
//! let options = CargoShearOptions::new_for_test(
//!     std::path::PathBuf::from("."),
//!     false, // fix
//! );
//! let exit_code = CargoShear::new(std::io::stdout(), options).run();
//! ```

mod cargo_toml_editor;
mod dependency_analyzer;
mod import_collector;
mod package_processor;
#[cfg(test)]
mod tests;

use std::{
    env, fs,
    io::Write,
    path::{Path, PathBuf},
    process::ExitCode,
    str::FromStr,
};

use anyhow::Result;
use bpaf::Bpaf;
use cargo_metadata::{CargoOpt, Metadata, MetadataCommand, Package};
use cargo_toml::Manifest;
use rustc_hash::FxHashSet;
use toml_edit::DocumentMut;

use crate::{
    cargo_toml_editor::CargoTomlEditor,
    package_processor::{PackageProcessResult, PackageProcessor, WorkspaceProcessResult},
};

const VERSION: &str = match option_env!("SHEAR_VERSION") {
    Some(v) => v,
    None => "dev",
};

/// Command-line options for cargo-shear.
///
/// This struct is parsed from command-line arguments using `bpaf`.
/// The "batteries" feature strips the binary name using `bpaf::cargo_helper`.
///
/// See <https://docs.rs/bpaf/latest/bpaf/batteries/fn.cargo_helper.html>
#[derive(Debug, Clone, Bpaf)]
#[bpaf(options("shear"), version(VERSION))]
pub struct CargoShearOptions {
    /// Remove unused dependencies.
    ///
    /// When set, cargo-shear will automatically remove detected unused
    /// dependencies from Cargo.toml files.
    #[bpaf(long)]
    fix: bool,

    /// Uses `cargo expand` to expand macros, which requires nightly and is significantly slower.
    ///
    /// This option provides more accurate detection by expanding proc macros
    /// and attribute macros, but requires a nightly Rust toolchain.
    #[bpaf(long)]
    expand: bool,

    /// Assert that `Cargo.lock` will remain unchanged.
    locked: bool,

    /// Run without accessing the network
    offline: bool,

    /// Equivalent to specifying both --locked and --offline
    frozen: bool,

    /// Package(s) to check.
    ///
    /// If not specified, all packages in the workspace are checked.
    /// Can be specified multiple times to check specific packages.
    #[bpaf(long, short, argument("SPEC"))]
    package: Vec<String>,

    /// Exclude packages from the check.
    ///
    /// Can be specified multiple times to exclude multiple packages.
    exclude: Vec<String>,

    /// Path to the project directory.
    ///
    /// Defaults to the current directory if not specified.
    #[bpaf(positional("PATH"), fallback_with(default_path))]
    path: PathBuf,
}

impl CargoShearOptions {
    /// Create a new `CargoShearOptions` for testing purposes
    #[must_use]
    pub const fn new_for_test(path: PathBuf, fix: bool) -> Self {
        Self {
            fix,
            expand: false,
            locked: false,
            offline: false,
            frozen: false,
            package: vec![],
            exclude: vec![],
            path,
        }
    }
}

pub(crate) fn default_path() -> Result<PathBuf> {
    Ok(env::current_dir()?)
}

/// The main struct that orchestrates the dependency analysis and removal process.
///
/// `CargoShear` coordinates the analysis of a Rust project to find unused dependencies
/// and optionally removes them from Cargo.toml files.
pub struct CargoShear<W> {
    /// Writer for output
    writer: W,

    /// Configuration options for the analysis
    options: CargoShearOptions,

    /// Counter for total unused dependencies found
    unused_dependencies: usize,

    /// Counter for total misplaced dependencies found
    misplaced_dependencies: usize,

    /// Counter for dependencies that were fixed
    fixed_dependencies: usize,
}

impl<W: Write> CargoShear<W> {
    /// Create a new `CargoShear` instance with the given options.
    ///
    /// # Arguments
    ///
    /// * `writer` - Output writer
    /// * `options` - Configuration options for the analysis
    ///
    /// # Example
    ///
    /// ```
    /// use cargo_shear::{CargoShear, CargoShearOptions};
    /// use std::path::PathBuf;
    ///
    /// let options = CargoShearOptions::new_for_test(PathBuf::from("."), false);
    /// let shear = CargoShear::new(std::io::stdout(), options);
    /// ```
    #[must_use]
    pub const fn new(writer: W, options: CargoShearOptions) -> Self {
        Self {
            writer,
            options,
            unused_dependencies: 0,
            misplaced_dependencies: 0,
            fixed_dependencies: 0,
        }
    }

    /// Run the dependency analysis and optionally fix unused dependencies.
    ///
    /// This method performs the complete analysis workflow:
    /// 1. Analyzes all packages in the workspace
    /// 2. Detects unused dependencies
    /// 3. Optionally removes them if `--fix` is enabled
    /// 4. Reports results to the writer
    ///
    /// # Returns
    ///
    /// Returns an `ExitCode` indicating success or failure:
    /// - `0` if no issues were found or all issues were fixed
    /// - `1` if unused dependencies were found (without `--fix`)
    /// - `2` if an error occurred
    #[must_use]
    pub fn run(mut self) -> ExitCode {
        let _ = writeln!(self.writer, "Analyzing {}", self.options.path.to_string_lossy());
        let _ = writeln!(self.writer);

        match self.shear() {
            Ok(()) => {
                let has_fixed = self.fixed_dependencies > 0;

                if has_fixed {
                    let _ = writeln!(
                        self.writer,
                        "Fixed {} {}.\n",
                        self.fixed_dependencies,
                        if self.fixed_dependencies == 1 { "dependency" } else { "dependencies" }
                    );
                }

                let total_issues = self.unused_dependencies + self.misplaced_dependencies;
                let has_issues = (total_issues - self.fixed_dependencies) > 0;

                if has_issues {
                    let _ = writeln!(
                        self.writer,
                        "\n\
                        cargo-shear may have detected unused dependencies incorrectly due to its limitations.\n\
                        They can be ignored by adding the crate name to the package's Cargo.toml:\n\n\
                        [package.metadata.cargo-shear]\n\
                        ignored = [\"crate-name\"]\n\n\
                        or in the workspace Cargo.toml:\n\n\
                        [workspace.metadata.cargo-shear]\n\
                        ignored = [\"crate-name\"]\n"
                    );

                    if !self.options.fix {
                        let _ =
                            writeln!(self.writer, "To automatically fix issues, run with --fix");
                    }
                } else {
                    let _ = writeln!(self.writer, "No issues detected!");
                }

                ExitCode::from(u8::from(if self.options.fix { has_fixed } else { has_issues }))
            }
            Err(err) => {
                let _ = writeln!(self.writer, "{err:?}");
                let _ = writeln!(
                    self.writer,
                    "note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace"
                );
                ExitCode::from(2)
            }
        }
    }

    fn shear(&mut self) -> Result<()> {
        let mut extra_opts = Vec::new();
        if self.options.locked {
            extra_opts.push("--locked".to_owned());
        }
        if self.options.offline {
            extra_opts.push("--offline".to_owned());
        }
        if self.options.frozen {
            extra_opts.push("--frozen".to_owned());
        }

        let metadata = MetadataCommand::new()
            .features(CargoOpt::AllFeatures)
            .current_dir(&self.options.path)
            .other_options(extra_opts)
            .exec()
            .map_err(|e| anyhow::anyhow!("Metadata error: {e}"))?;

        let processor = PackageProcessor::new(self.options.expand);

        // Track all packages used across the workspace
        let mut workspace_used_pkgs = FxHashSet::default();

        // Process packages
        for package in metadata.workspace_packages() {
            // Skip if package is in the exclude list
            if self.options.exclude.iter().any(|name| name == package.name.as_str()) {
                continue;
            }

            // Skip if specific packages are specified and this package is not in the list
            if !self.options.package.is_empty()
                && !self.options.package.iter().any(|name| name == package.name.as_str())
            {
                continue;
            }

            let manifest_path = package.manifest_path.as_std_path();
            let manifest = Manifest::from_path(manifest_path)?;
            let result = processor.process_package(&metadata, package, &manifest)?;

            self.report_package_issues(package, &result, &metadata)?;
            self.fix_package_issues(manifest_path, &result)?;

            workspace_used_pkgs.extend(result.used_packages);
        }

        // Process workspace
        let manifest_path = metadata.workspace_root.as_std_path().join("Cargo.toml");
        let workspace_manifest = Manifest::from_path(&manifest_path)?;

        let workspace_result = PackageProcessor::process_workspace(
            &workspace_manifest,
            &metadata,
            &workspace_used_pkgs,
        );

        self.report_workspace_issues(&manifest_path, &workspace_result)?;
        self.fix_workspace_issues(&manifest_path, &workspace_result)?;

        Ok(())
    }

    fn report_package_issues(
        &mut self,
        package: &Package,
        result: &PackageProcessResult,
        metadata: &Metadata,
    ) -> Result<()> {
        // Warn about redundant ignores
        for ignored_dep in &result.redundant_ignores {
            writeln!(
                self.writer,
                "warning: '{ignored_dep}' is redundant in [package.metadata.cargo-shear] for package '{}'.\n",
                package.name
            )?;
        }

        let unused_count = result.unused_dependencies.len();
        let misplaced_count = result.misplaced_dependencies.len();

        if unused_count == 0 && misplaced_count == 0 {
            return Ok(());
        }

        let relative_path = PackageProcessor::get_relative_path(
            package.manifest_path.as_std_path(),
            metadata.workspace_root.as_std_path(),
        );

        writeln!(self.writer, "{} -- {}:", package.name, relative_path.display())?;

        if unused_count > 0 {
            writeln!(self.writer, "  unused dependencies:")?;
            for misplaced_dep in &result.unused_dependencies {
                writeln!(self.writer, "    {misplaced_dep}")?;
            }
        }

        if misplaced_count > 0 {
            writeln!(self.writer, "  move to dev-dependencies:")?;
            for misplaced_dep in &result.misplaced_dependencies {
                writeln!(self.writer, "    {misplaced_dep}")?;
            }
        }

        writeln!(self.writer)?;

        self.unused_dependencies += unused_count;
        self.misplaced_dependencies += misplaced_count;

        Ok(())
    }

    fn fix_package_issues(
        &mut self,
        manifest_path: &Path,
        result: &PackageProcessResult,
    ) -> Result<()> {
        if !self.options.fix {
            return Ok(());
        }

        if result.misplaced_dependencies.is_empty() && result.unused_dependencies.is_empty() {
            return Ok(());
        }

        let content = fs::read_to_string(manifest_path)?;
        let mut manifest = DocumentMut::from_str(&content)?;

        let fixed_unused =
            CargoTomlEditor::remove_dependencies(&mut manifest, &result.unused_dependencies);
        let fixed_misplaced = CargoTomlEditor::move_to_dev_dependencies(
            &mut manifest,
            &result.misplaced_dependencies,
        );

        fs::write(manifest_path, manifest.to_string())?;
        self.fixed_dependencies += fixed_unused + fixed_misplaced;

        Ok(())
    }

    fn report_workspace_issues(
        &mut self,
        manifest_path: &Path,
        result: &WorkspaceProcessResult,
    ) -> Result<()> {
        // Warn about redundant workspace ignores
        for ignored_dep in &result.redundant_ignores {
            writeln!(
                self.writer,
                "warning: '{ignored_dep}' is redundant in [workspace.metadata.cargo-shear].\n"
            )?;
        }

        if result.unused_dependencies.is_empty() {
            return Ok(());
        }

        let path = manifest_path
            .strip_prefix(env::current_dir().unwrap_or_default())
            .unwrap_or(manifest_path)
            .to_string_lossy();

        writeln!(self.writer, "root -- {path}:")?;
        writeln!(self.writer, "  unused dependencies:")?;
        for unused_dep in &result.unused_dependencies {
            writeln!(self.writer, "    {unused_dep}")?;
        }
        writeln!(self.writer)?;

        self.unused_dependencies += result.unused_dependencies.len();

        Ok(())
    }

    fn fix_workspace_issues(
        &mut self,
        manifest_path: &Path,
        result: &WorkspaceProcessResult,
    ) -> Result<()> {
        if !self.options.fix {
            return Ok(());
        }

        if result.unused_dependencies.is_empty() {
            return Ok(());
        }

        let content = fs::read_to_string(manifest_path)?;
        let mut manifest = DocumentMut::from_str(&content)?;

        let fixed =
            CargoTomlEditor::remove_dependencies(&mut manifest, &result.unused_dependencies);

        fs::write(manifest_path, manifest.to_string())?;
        self.fixed_dependencies += fixed;

        Ok(())
    }
}