Cargo Shear ✂️ 🐑
Detect and fix issues in Rust projects:
- Unused dependencies in
Cargo.toml - Misplaced dependencies (dev/build dependencies in wrong sections)
- Unlinked source files (Rust files not reachable from any module tree)
Installation
# Install from pre-built binaries.
# Build from source.
# Install from brew.
Usage
Check for issues without making changes:
Automatically fix unused dependencies:
Limitations
[!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:
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.
Configuration
Ignore false positives
False positives can be ignored by adding them to the package's Cargo.toml:
[]
= ["crate-name"]
Ignore unlinked files
Unlinked files can be ignored using glob patterns:
[]
= ["src/proto/*.rs", "examples/old/*"]
Both options work in workspace Cargo.toml as well:
[]
= ["crate-name"]
= ["*/proto/*.rs"]
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)
| 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
Technique
- Use the
cargo_metadatacrate to list all dependencies specified in[workspace.dependencies]and[dependencies] - Iterate through all package targets (
lib,bin,example,testandbench) to locate all Rust files - Use rust-analyzer's parser (
ra_ap_syntax) to parse these Rust files and extract imports- Alternatively, use the
--expandoption withcargo expandto first expand macros and then parse the expanded code (though this is significantly slower)
- Alternatively, use the
- Find the difference between the imports and the package dependencies
Prior Art
- est31/cargo-udeps
- it collects dependency usage by compiling your project and find them from the
target/directory - does not seem to work anymore with the latest versions of
cargo - does not work with cargo workspaces
- it collects dependency usage by compiling your project and find them from the
- bnjbvr/cargo-machete
- it collects dependency usage by running regex patterns on source code
- does not detect all usages of a dependency
- does not remove unused dependencies from the workspace root
- cargo and clippy
- There was intention to add similar features to cargo or clippy, but the progress is currently stagnant
- See https://github.com/rust-lang/rust/issues/57274 and https://github.com/rust-lang/rust-clippy/issues/4341
Trophy Cases
- -7 lines from oxc
- -59 lines from rspack
- -39 lines from rolldown
- -12 lines ast-grep commit1 commit2
- -66 lines biome
- -164 lines astral-sh/uv
- -86 lines reqsign
- -184 lines from turbopack
- -625 lines from openai/codex