cargo-machete
Original:🇺🇸 English
Translated
Detect unused dependencies in Rust projects for cleaner Cargo.toml files and faster builds. Use when auditing dependencies, optimizing build times, cleaning up Cargo.toml, or detecting bloat. Trigger terms: unused dependencies, cargo-machete, dependency audit, dependency cleanup, bloat detection, cargo-udeps.
6installs
Added on
NPX Install
npx skill4agent add laurigates/claude-plugins cargo-macheteTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →cargo-machete - Unused Dependency Detection
Detect and remove unused dependencies in Rust projects using cargo-machete.
When to Use This Skill
| Use this skill when... | Use X instead when... |
|---|---|
| Auditing for unused dependencies | Checking for outdated deps -- use |
| Cleaning up Cargo.toml | Auditing security vulnerabilities -- use |
| Optimizing build times by removing bloat | Checking license compliance -- use |
| Verifying deps in CI | Need nightly-accurate analysis -- use |
Context
- Cargo.toml: !
test -f Cargo.toml && echo "EXISTS" || echo "MISSING" - Workspace: !
grep -q '\[workspace\]' Cargo.toml 2>/dev/null && echo "YES" || echo "NO" - cargo-machete installed: !
cargo machete --version 2>/dev/null || echo "NOT INSTALLED" - Machete config: !
test -f .cargo-machete.toml && echo "EXISTS" || echo "MISSING" - Workspace members: !
grep -A 20 '^\[workspace\]' Cargo.toml 2>/dev/null | grep -o '"[^"]*"' 2>/dev/null
Execution
Execute this unused dependency analysis:
Step 1: Verify installation
Check if cargo-machete is installed (see Context above). If not installed, install it:
bash
cargo install cargo-macheteStep 2: Run dependency analysis
Run cargo-machete with metadata for detailed output:
bash
# Single crate
cargo machete --with-metadata
# Workspace (if Context shows workspace)
cargo machete --workspace --with-metadataStep 3: Evaluate results
Review the output and classify each finding:
| Finding | Action |
|---|---|
| Genuinely unused dependency | Remove with |
| Proc-macro dependency (e.g., serde derive) | Add |
| Build.rs-only dependency | Move to |
| Re-exported dependency | Add |
Step 4: Apply fixes
For confirmed unused dependencies, auto-remove them:
bash
cargo machete --fixFor false positives, add ignore annotations in :
Cargo.tomltoml
serde = "1.0" # machete:ignore - used via derive macroOr create for project-wide ignores:
.cargo-machete.tomltoml
[ignore]
dependencies = ["serde", "log"]Step 5: Verify build
After removing dependencies, confirm everything still compiles:
bash
cargo check --all-targets
cargo test --no-runFalse Positive Handling
| Scenario | Solution |
|---|---|
| Proc-macro deps (e.g., serde derive) | |
| Build.rs-only deps | Move to |
| Re-exported deps | |
| Example/bench-only deps | Verify in |
Comparison with cargo-udeps
| Feature | cargo-machete | cargo-udeps |
|---|---|---|
| Accuracy | Good | Excellent |
| Speed | Very fast | Slower |
| Rust version | Stable | Requires nightly |
| False positives | Some | Fewer |
Use cargo-machete for fast CI checks. Use cargo-udeps for thorough audits:
bash
cargo +nightly udeps --workspace --all-featuresAgentic Optimizations
| Context | Command |
|---|---|
| Quick check | |
| Detailed check | |
| Workspace check | |
| Auto-fix | |
| Verify after fix | |
| Accurate check (nightly) | |
Quick Reference
| Flag | Description |
|---|---|
| Show detailed output with versions and locations |
| Auto-remove unused dependencies from Cargo.toml |
| Check all workspace members |
| Check specific workspace member |
| Exclude specific workspace member |
Troubleshooting
| Problem | Solution |
|---|---|
| Proc macro flagged as unused | Add |
| Build.rs dep flagged | Move to |
| Re-exported dep flagged | Add |
| Need more accuracy | Use |
For CI integration patterns, configuration file examples, pre-commit setup, and Makefile integration, see REFERENCE.md.