Crates.io | cargo-shear |
lib.rs | cargo-shear |
version | 1.5.2 |
created_at | 2024-03-15 09:33:30.855157+00 |
updated_at | 2025-09-22 13:33:22.607423+00 |
description | Detect and remove unused dependencies from Cargo.toml |
homepage | |
repository | https://github.com/Boshen/cargo-shear |
max_upload_size | |
id | 1174532 |
size | 141,295 |
Detect and remove unused dependencies from Cargo.toml
in Rust projects.
# Install from pre-built binaries.
cargo binstall cargo-shear
# Build from source.
cargo install cargo-shear
# Install from brew.
brew install cargo-shear
cargo shear --fix
[!IMPORTANT]
cargo shear
cannot detect "hidden" imports from macro expansions without the--expand
flag (nightly only). This is becausecargo shear
usessyn
to parse files and does not expand macros by default.
To expand macros:
cargo shear --expand --fix
The --expand
flag uses cargo expand
, which requires nightly and is significantly slower.
False positives can be ignored by adding them to the package's Cargo.toml
:
[package.metadata.cargo-shear]
ignored = ["crate-name"]
or in the workspace Cargo.toml
:
[workspace.metadata.cargo-shear]
ignored = ["crate-name"]
Otherwise please report the issue as a bug.
- name: Install cargo-binstall
uses: cargo-bins/cargo-binstall@main
- name: Install cargo-shear
run: cargo binstall --no-confirm cargo-shear
- run: cargo shear
The exit code gives an indication whether unused dependencies have been found:
With --fix
:
cargo check
after cargo-shear
changed Cargo.toml
.GitHub Actions Job Example:
- name: cargo-shear
shell: bash
run: |
if ! cargo shear --fix; then
cargo check
fi
cargo_metadata
crate to list all dependencies specified in [workspace.dependencies]
and [dependencies]
lib
, bin
, example
, test
and bench
) to locate all Rust filessyn
to parse these Rust files and extract imports--expand
option with cargo expand
to first expand macros and then parse the expanded code (though this is significantly slower).target/
directorycargo