Build provenance: This package is built and distributed by Astral as part of astral-dev-toolchain.
It packages cargo-shear from tag v1.14.1 at commit 66b62cb43ca6a153db29c8229a2396656141dec9.
Misplaced dependencies (dev/build dependencies in wrong sections)
Unlinked source files (Rust files not reachable from any module tree)
[!NOTE]
This tool is considered feature-complete. We continue to welcome contributions that focus on bug fixes, dependency upgrades, and UI/UX improvements.
Comparison with Cargo lints
Cargo's unused_dependencies uses rustc's unused-extern results. unused_workspace_dependencies checks manifest inheritance. cargo-shear instead collects crate references from source files and compares them with manifest entries.
Check
Cargo lints
cargo-shear
Unused normal/build dependencies
cargo check checks these tables; cargo check --bin foo skips normal dependencies, even for a single-bin package. Only selected packages are checked.
Checks all workspace members by default, including normal, build, dev, and target-specific dependency tables.
Unused dev-dependencies
Currently unchecked, including with --all-targets.
Checks references collected from source files, including unit tests, integration tests, examples, benchmarks, and Rust doc-comment code blocks.
Features and platforms
Checks dependencies participating in the selected build configuration.
Does not evaluate cfg: references in disabled feature/platform branches count as usage. A single scan can cover these branches without building each configuration.
Macro-generated references
Uses compiler results after expansion.
Can miss generated references by default. --expand adds expanded references using nightly rustc and all features; build scripts are not expanded.
Workspace dependencies
Reports entries that no member inherits. An inherited but unused dependency satisfies this lint.
Checks whether the underlying package has a source reference, feature reference, or ignore exemption in any member. Inheritance alone does not keep it.
Moving dependencies to dev-dependencies
Can suggest a move when development-only usage is observed.
--fix moves non-optional normal dependencies referenced only by integration tests, examples, or benchmarks. References inside #[cfg(test)] in src/ count as normal usage, so these are not moved.
Other cleanup
These two lints concern dependencies.
Also reports unlinked and empty source files; --fix removes unused non-optional dependencies and unused workspace entries. Optional and feature-only dependencies are reported separately for manual review.
For example, an unused dependency declared only in [dev-dependencies] is a reason to run cargo-shear even with Cargo's lints enabled. A dependency used only in a Windows cfg branch is retained by cargo-shear when scanning on Linux, without a Windows build.
Source references are a heuristic: cargo-shear also counts references in scanned files that are not linked into a module tree, so stale code can keep a dependency from being reported. Default analysis does not compile the project, but it runs cargo metadata --all-features, which still requires dependency resolution. See Limitations.
Installation
# Install from pre-built binaries.
cargo binstall cargo-shear
# Build from source.
cargo install cargo-shear --locked
# Install from brew.
brew install cargo-shear
Usage
Check for issues without making changes:
cargo shear
Automatically fix unused dependencies:
cargo shear --fix
Treat warnings as errors (exit with failure code):
cargo shear --deny-warnings
This is useful for CI/CD pipelines to enforce strict checking of warnings such as empty files, unlinked files, and unused optional dependencies.
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.
Detect mismatches between [lib] target settings and source content:
cargo shear --check-test-targets
When set, cargo-shear warns when test = false is paired with source that contains tests (or doctest = false with source that contains doc tests), and — within a workspace — when test / doctest are left at their default of true for lib targets that contain none. Disabled by default; pair with --fix to automatically reconcile the flags.
Limitations
[!IMPORTANT]
cargo shear cannot detect "hidden" imports from macro expansions without the --expand flag (nightly only).
This is because cargo shear uses 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.
Configuration
Ignore false positives
False positives can be ignored by adding them to the package's Cargo.toml:
Package ignore ([package.metadata.cargo-shear]) applies only to that package.
Workspace ignore ([workspace.metadata.cargo-shear]) applies to every member
and to the workspace dependency itself. ignored-paths globs are matched relative
to the workspace root.
An ignore that suppresses nothing is reported as redundant so it can be removed. A
workspace ignore is considered redundant only when no member needs it — a dependency
that is used in one crate but unused in another stays covered by the workspace ignore.
Keep pre-declared path dependencies
Some workflows declare a new crate in [workspace.dependencies] before any member
depends on it, so later commits can inherit it. To keep such entries:
A [workspace.dependencies] entry with path = "..." is then treated as used as
long as a crate exists at the declared path. A stale entry — one whose crate has
been deleted or moved — is still reported as unused.
cargo-hakari workspace-hack crates
cargo-hakari generates a workspace-hack crate that declares many dependencies it never imports (to unify Cargo features) and is depended on by every workspace member without being imported. cargo shear detects such a crate automatically — via the ### BEGIN HAKARI SECTION marker in its Cargo.toml — and skips both the crate itself and the dependency edges pointing at it, so no ignored configuration is needed.
Otherwise please report the issue as a bug.
CI
[!NOTE]
cargo shear uses static analysis and operates on source code without compiling.
This means it only needs to run once on a single platform (e.g., Linux) to detect issues across all target platforms, including those with platform-specific dependencies and conditional compilation.
The only exception is when using the --expand flag, which invokes cargo build and may produce platform-specific results.
findings: Detailed information about each issue including:
code: The diagnostic code (e.g., shear/unused_dependency)
severity: Error or warning level
message: Human-readable description
file: Path to the file with the issue
location: Byte offset and length within the file
help: Suggested fix
fixable: Boolean indicating if issue can be auto-fixed with --fix
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
Strict Mode with --deny-warnings
By default, warnings (such as empty files, unlinked files, and unused optional dependencies) exit with code 0. Use the --deny-warnings flag to treat warnings as errors for stricter CI enforcement:
Exit Code
Without --deny-warnings
With --deny-warnings
0
No errors (warnings allowed)
No errors or warnings
1
Errors found
Errors or warnings found
2
Error during processing
Error during processing
GitHub Actions Example:
- name: cargo-shear
shell: bash
run: |
if ! cargo shear --fix; then
cargo check
fi