cargo-machete

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

cargo-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 dependenciesChecking for outdated deps -- use
cargo outdated
Cleaning up Cargo.tomlAuditing security vulnerabilities -- use
cargo audit
Optimizing build times by removing bloatChecking license compliance -- use
cargo deny
Verifying deps in CINeed nightly-accurate analysis -- use
cargo +nightly udeps
适用场景替代工具场景
审计未使用的依赖项检查过时依赖项——使用
cargo outdated
清理Cargo.toml审计安全漏洞——使用
cargo audit
通过移除冗余优化构建时间检查许可证合规性——使用
cargo deny
在CI中验证依赖项需要 nightly 版本的精准分析——使用
cargo +nightly udeps

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-machete

Step 2: Run dependency analysis

步骤2:运行依赖项分析

Run cargo-machete with metadata for detailed output:
bash
undefined
携带元数据参数运行cargo-machete以获取详细输出:
bash
undefined

Single crate

单个 crate

cargo machete --with-metadata
cargo machete --with-metadata

Workspace (if Context shows workspace)

工作区(若环境检查显示为工作区)

cargo machete --workspace --with-metadata
undefined
cargo machete --workspace --with-metadata
undefined

Step 3: Evaluate results

步骤3:评估分析结果

Review the output and classify each finding:
FindingAction
Genuinely unused dependencyRemove with
--fix
or manually
Proc-macro dependency (e.g., serde derive)Add
machete:ignore
comment
Build.rs-only dependencyMove to
[build-dependencies]
Re-exported dependencyAdd
machete:ignore
comment with explanation
查看输出并对每个检测结果进行分类处理:
检测结果处理操作
确实未使用的依赖项使用
--fix
参数自动移除或手动删除
过程宏依赖项(如serde derive)添加
machete:ignore
注释
仅在build.rs中使用的依赖项移至
[build-dependencies]
区域
被重导出的依赖项添加带有说明的
machete:ignore
注释

Step 4: Apply fixes

步骤4:应用修复

For confirmed unused dependencies, auto-remove them:
bash
cargo machete --fix
For false positives, add ignore annotations in
Cargo.toml
:
toml
serde = "1.0"  # machete:ignore - used via derive macro
Or create
.cargo-machete.toml
for project-wide ignores:
toml
[ignore]
dependencies = ["serde", "log"]
对于确认未使用的依赖项,自动移除它们:
bash
cargo machete --fix
对于误报情况,在
Cargo.toml
中添加忽略注释:
toml
serde = "1.0"  # machete:ignore - used via derive macro
或创建
.cargo-machete.toml
文件进行项目级全局忽略:
toml
[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-run

False Positive Handling

误报处理

ScenarioSolution
Proc-macro deps (e.g., serde derive)
machete:ignore
comment
Build.rs-only depsMove to
[build-dependencies]
Re-exported deps
machete:ignore
comment with explanation
Example/bench-only depsVerify in
[dev-dependencies]
场景解决方案
过程宏依赖项(如serde derive)添加
machete:ignore
注释
仅在build.rs中使用的依赖项移至
[build-dependencies]
区域
被重导出的依赖项添加带有说明的
machete:ignore
注释
仅在示例/基准测试中使用的依赖项确认其位于
[dev-dependencies]
区域

Comparison with cargo-udeps

与cargo-udeps的对比

Featurecargo-machetecargo-udeps
AccuracyGoodExcellent
SpeedVery fastSlower
Rust versionStableRequires nightly
False positivesSomeFewer
Use cargo-machete for fast CI checks. Use cargo-udeps for thorough audits:
bash
cargo +nightly udeps --workspace --all-features
特性cargo-machetecargo-udeps
准确性良好优秀
速度极快较慢
Rust版本要求稳定版需要nightly版
误报率存在部分极少
快速CI检查使用cargo-machete,全面审计使用cargo-udeps:
bash
cargo +nightly udeps --workspace --all-features

Agentic Optimizations

智能优化命令

ContextCommand
Quick check
cargo machete
Detailed check
cargo machete --with-metadata
Workspace check
cargo machete --workspace --with-metadata
Auto-fix
cargo machete --fix
Verify after fix
cargo check --all-targets
Accurate check (nightly)
cargo +nightly udeps --workspace
场景命令
快速检查
cargo machete
详细检查
cargo machete --with-metadata
工作区检查
cargo machete --workspace --with-metadata
自动修复
cargo machete --fix
修复后验证
cargo check --all-targets
精准检查(nightly版)
cargo +nightly udeps --workspace

Quick Reference

快速参考

FlagDescription
--with-metadata
Show detailed output with versions and locations
--fix
Auto-remove unused dependencies from Cargo.toml
--workspace
Check all workspace members
-p <crate>
Check specific workspace member
--exclude <crate>
Exclude specific workspace member
参数描述
--with-metadata
显示包含版本和位置的详细输出
--fix
自动从Cargo.toml中移除未使用的依赖项
--workspace
检查所有工作区成员
-p <crate>
检查指定的工作区成员
--exclude <crate>
排除指定的工作区成员

Troubleshooting

故障排除

ProblemSolution
Proc macro flagged as unusedAdd
machete:ignore
comment in Cargo.toml
Build.rs dep flaggedMove to
[build-dependencies]
Re-exported dep flaggedAdd
machete:ignore
with explanation
Need more accuracyUse
cargo +nightly udeps
For CI integration patterns, configuration file examples, pre-commit setup, and Makefile integration, see REFERENCE.md.
问题解决方案
过程宏被标记为未使用在Cargo.toml中添加
machete:ignore
注释
build.rs依赖项被标记移至
[build-dependencies]
区域
被重导出的依赖项被标记添加带有说明的
machete:ignore
注释
需要更高准确性使用
cargo +nightly udeps
关于CI集成模式、配置文件示例、pre-commit设置和Makefile集成,请参考REFERENCE.md