#cargo-toml #unused #cargo #dependencies #machete #udeps

bin+lib cargo-shear

Detect and remove unused dependencies from Cargo.toml

24 releases

0.0.24 Apr 9, 2024
0.0.23 Apr 3, 2024
0.0.16 Mar 29, 2024

#199 in Cargo plugins

Download history 483/week @ 2024-03-15 614/week @ 2024-03-22 1037/week @ 2024-03-29 603/week @ 2024-04-05 492/week @ 2024-04-12 597/week @ 2024-04-19

2,760 downloads per month

MIT license

23KB
425 lines

Cargo Shear ✂️ 🐑

Detect and remove unused dependencies from Cargo.toml in Rust projects.

Installation

cargo binstall cargo-shear
# OR
cargo install cargo-shear

Usage

cargo shear --fix

Ignore false positives

cargo shear does not work with transitive dependencies from macros; they need to be ignored manually by adding them to the package's Cargo.toml:

[package.metadata.cargo-shear]
ignored = ["crate"]

Otherwise please report the issue as a bug.

CI

- name: Install cargo-binstall
  uses: cargo-bins/cargo-binstall@main

- name: Install cargo-shear
  run: cargo binstall --no-confirm cargo-shear

- run: cargo shear

Exit Code (for CI)

The exit code gives an indication whether unused dependencies have been found:

  • 0 if found no unused dependencies,
  • 1 if it found at least one unused dependency,
  • 2 if there was an error during processing (in which case there's no indication whether any unused dependency was found or not).

Technique

  1. use the cargo_metadata crate to list all dependencies specified in [workspace.dependencies] and [dependencies]
  2. iterate through all package targets (lib, bin, example, test and bench) to locate all Rust files
  3. use syn to parse these Rust files and extract imports
  4. find the difference between the imports and the package dependencies

Prior Arts

Trophy Cases

Dependencies

~8–19MB
~273K SLoC