| Crates.io | cargo-shear |
| lib.rs | cargo-shear |
| version | 1.9.1 |
| created_at | 2024-03-15 09:33:30.855157+00 |
| updated_at | 2025-12-15 13:06:13.427137+00 |
| description | Detect and fix unused/misplaced dependencies from Cargo.toml |
| homepage | |
| repository | https://github.com/Boshen/cargo-shear |
| max_upload_size | |
| id | 1174532 |
| size | 305,067 |
Detect and fix issues in Rust projects:
Cargo.toml# Install from pre-built binaries.
cargo binstall cargo-shear
# Build from source.
cargo install cargo-shear
# Install from brew.
brew install cargo-shear
Check for issues without making changes:
cargo shear
Automatically fix unused dependencies:
cargo shear --fix
Generate machine-readable JSON output:
cargo shear --format=json
This is particularly useful for CI/CD pipelines and custom tooling that need to programmatically process the results.
[!IMPORTANT]
cargo shearcannot detect "hidden" imports from macro expansions without the--expandflag (nightly only). This is becausecargo shearuses rust-analyzer's parser 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.
[!IMPORTANT] Misplaced dependency detection only works for integration tests, benchmarks, and examples. Unit tests dependencies within
#[cfg(test)]cannot be detected as misplaced.
False positives can be ignored by adding them to the package's Cargo.toml:
[package.metadata.cargo-shear]
ignored = ["crate-name"]
Unlinked files can be ignored using glob patterns:
[package.metadata.cargo-shear]
ignored-paths = ["src/proto/*.rs", "examples/old/*"]
Both options work in workspace Cargo.toml as well:
[workspace.metadata.cargo-shear]
ignored = ["crate-name"]
ignored-paths = ["*/proto/*.rs"]
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
For CI systems that require structured output, use the --format=json flag:
- name: Check for unused dependencies
run: cargo shear --format=json > shear-results.json
The JSON output includes:
code: The diagnostic code (e.g., shear/unused_dependency)severity: Error or warning levelmessage: Human-readable descriptionfile: Path to the file with the issuelocation: Byte offset and length within the filehelp: Suggested fixfixable: Boolean indicating if issue can be auto-fixed with --fix| Exit Code | Without --fix |
With --fix |
|---|---|---|
| 0 | No issues found | No issues found, no changes made |
| 1 | Issues found | Issues found and fixed |
| 2 | Error during processing | Error during processing |
GitHub Actions 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 filesra_ap_syntax) 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