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

bin+lib cargo-shear

Detect and remove unused dependencies from Cargo.toml

38 releases (12 stable)

new 1.1.9 Feb 11, 2025
1.1.4 Nov 25, 2024
1.1.1 Jul 25, 2024
0.0.26 May 29, 2024
0.0.16 Mar 29, 2024

#55 in Cargo plugins

Download history 3532/week @ 2024-10-22 3143/week @ 2024-10-29 3111/week @ 2024-11-05 3127/week @ 2024-11-12 3808/week @ 2024-11-19 3329/week @ 2024-11-26 3492/week @ 2024-12-03 3835/week @ 2024-12-10 3501/week @ 2024-12-17 2051/week @ 2024-12-24 2415/week @ 2024-12-31 4326/week @ 2025-01-07 3486/week @ 2025-01-14 3194/week @ 2025-01-21 2726/week @ 2025-01-28 3132/week @ 2025-02-04

13,135 downloads per month

MIT license

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

Limitation

[!IMPORTANT] cargo shear cannot detect "hidden" imports from macro expansions. This is because cargo shear uses syn to parse files and does not expand macros.

Ignore false positives

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
~238K SLoC