Cargo Shear ✂️ 🐑
Detect and remove unused dependencies from Cargo.toml
in Rust projects.
Installation
# OR
Usage
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
:
[]
= ["crate"]
or in workspace:
[]
= ["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
- use the
cargo_metadata
crate to list all dependencies specified in[workspace.dependencies]
and[dependencies]
- iterate through all package targets (
lib
,bin
,example
,test
andbench
) to locate all Rust files - use
syn
to parse these Rust files and extract imports - find the difference between the imports and the package dependencies