cargo-machete
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinesecargo-machete - Unused Dependency Detection
cargo-machete - 未使用依赖项检测
Detect and remove unused dependencies in Rust projects using cargo-machete.
使用cargo-machete检测并移除Rust项目中未使用的依赖项。
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 |
| 适用场景 | 替代工具场景 |
|---|---|
| 审计未使用的依赖项 | 检查过时依赖项——使用 |
| 清理Cargo.toml | 审计安全漏洞——使用 |
| 通过移除冗余优化构建时间 | 检查许可证合规性——使用 |
| 在CI中验证依赖项 | 需要 nightly 版本的精准分析——使用 |
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
- Cargo.toml: !
test -f Cargo.toml && echo "EXISTS" || echo "MISSING" - 工作区: !
grep -q '\[workspace\]' Cargo.toml 2>/dev/null && echo "YES" || echo "NO" - cargo-machete已安装: !
cargo machete --version 2>/dev/null || echo "NOT INSTALLED" - Machete配置文件: !
test -f .cargo-machete.toml && echo "EXISTS" || echo "MISSING" - 工作区成员: !
grep -A 20 '^\[workspace\]' Cargo.toml 2>/dev/null | grep -o '"[^"]*"' 2>/dev/null
Execution
执行步骤
Execute this unused dependency analysis:
执行以下未使用依赖项分析流程:
Step 1: Verify installation
步骤1:验证安装
Check if cargo-machete is installed (see Context above). If not installed, install it:
bash
cargo install cargo-machete检查cargo-machete是否已安装(参考上方环境检查)。若未安装,执行以下命令安装:
bash
cargo install cargo-macheteStep 2: Run dependency analysis
步骤2:运行依赖项分析
Run cargo-machete with metadata for detailed output:
bash
undefined携带元数据参数运行cargo-machete以获取详细输出:
bash
undefinedSingle crate
单个 crate
cargo machete --with-metadata
cargo machete --with-metadata
Workspace (if Context shows workspace)
工作区(若环境检查显示为工作区)
cargo machete --workspace --with-metadata
undefinedcargo machete --workspace --with-metadata
undefinedStep 3: Evaluate results
步骤3:评估分析结果
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 |
查看输出并对每个检测结果进行分类处理:
| 检测结果 | 处理操作 |
|---|---|
| 确实未使用的依赖项 | 使用 |
| 过程宏依赖项(如serde derive) | 添加 |
| 仅在build.rs中使用的依赖项 | 移至 |
| 被重导出的依赖项 | 添加带有说明的 |
Step 4: Apply fixes
步骤4:应用修复
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"]对于确认未使用的依赖项,自动移除它们:
bash
cargo machete --fix对于误报情况,在中添加忽略注释:
Cargo.tomltoml
serde = "1.0" # machete:ignore - used via derive macro或创建文件进行项目级全局忽略:
.cargo-machete.tomltoml
[ignore]
dependencies = ["serde", "log"]Step 5: Verify build
步骤5:验证构建
After removing dependencies, confirm everything still compiles:
bash
cargo check --all-targets
cargo test --no-run移除依赖项后,确认项目仍可正常编译:
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 |
| 场景 | 解决方案 |
|---|---|
| 过程宏依赖项(如serde derive) | 添加 |
| 仅在build.rs中使用的依赖项 | 移至 |
| 被重导出的依赖项 | 添加带有说明的 |
| 仅在示例/基准测试中使用的依赖项 | 确认其位于 |
Comparison with cargo-udeps
与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-features| 特性 | cargo-machete | cargo-udeps |
|---|---|---|
| 准确性 | 良好 | 优秀 |
| 速度 | 极快 | 较慢 |
| Rust版本要求 | 稳定版 | 需要nightly版 |
| 误报率 | 存在部分 | 极少 |
快速CI检查使用cargo-machete,全面审计使用cargo-udeps:
bash
cargo +nightly udeps --workspace --all-featuresAgentic Optimizations
智能优化命令
| Context | Command |
|---|---|
| Quick check | |
| Detailed check | |
| Workspace check | |
| Auto-fix | |
| Verify after fix | |
| Accurate check (nightly) | |
| 场景 | 命令 |
|---|---|
| 快速检查 | |
| 详细检查 | |
| 工作区检查 | |
| 自动修复 | |
| 修复后验证 | |
| 精准检查(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 |
| 参数 | 描述 |
|---|---|
| 显示包含版本和位置的详细输出 |
| 自动从Cargo.toml中移除未使用的依赖项 |
| 检查所有工作区成员 |
| 检查指定的工作区成员 |
| 排除指定的工作区成员 |
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.
| 问题 | 解决方案 |
|---|---|
| 过程宏被标记为未使用 | 在Cargo.toml中添加 |
| build.rs依赖项被标记 | 移至 |
| 被重导出的依赖项被标记 | 添加带有说明的 |
| 需要更高准确性 | 使用 |
关于CI集成模式、配置文件示例、pre-commit设置和Makefile集成,请参考REFERENCE.md。