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

bin+lib cargo-shear

Detect and remove unused dependencies from Cargo.toml

33 releases (7 stable)

1.1.4 Nov 25, 2024
1.1.3 Sep 23, 2024
1.1.2 Aug 18, 2024
1.1.1 Jul 25, 2024
0.0.16 Mar 29, 2024

#63 in Cargo plugins

Download history 3047/week @ 2024-08-19 3064/week @ 2024-08-26 3001/week @ 2024-09-02 2506/week @ 2024-09-09 2796/week @ 2024-09-16 3219/week @ 2024-09-23 2373/week @ 2024-09-30 2790/week @ 2024-10-07 3218/week @ 2024-10-14 3467/week @ 2024-10-21 3231/week @ 2024-10-28 3210/week @ 2024-11-04 3063/week @ 2024-11-11 3571/week @ 2024-11-18 3493/week @ 2024-11-25 3240/week @ 2024-12-02

13,619 downloads per month

MIT license

27KB
478 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

[!IMPORTANT] cargo shear cannot detect dependency usages from macros because it uses syn to parse the files

False positives can be ignored by adding them to the package's Cargo.toml:

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

or in workspace's Cargo.toml:

[workspace.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

Sponsored By

My sponsors

Dependencies

~9–18MB
~241K SLoC