关于 similarity-rs
similarity-rs 是在开发和代码审查中使用 Rust 代码重复检测 CLI 的技能,面向 Rust 开发者。它介绍先搜索再编写的流程、CI 配置和参数调优,并提醒 Beta 结果需人工复核。
主要功能
- 01检测相似的 Rust 代码
- 02先搜索再编写的流程指南
- 03Pre-commit 与 GitHub Actions 配置示例
- 04调节阈值和最小行数
- 05复核误报的建议
适用场景
- 01提交前检查代码重复
- 02清理期间寻找可合并的代码
- 03在 CI 中加入重复检测
安装
复制下面的命令,在终端中运行。
npx skills add https:/ / github.com/ chrisgliddon/ bevy-skills --skill similarity-rs安装后在 Claude Code 中输入/similarity-rs确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
references/antipatterns.mdci-integration.mdfalse-positives.mdsearch-before-write.mdsibling-tools.mdtuning.md详细介绍
similarity-rs 0.5 — Rust 重复代码检测
工具状态:Beta — 根据上游 README,尚未经过生产环境测试。请人工评估检测结果后再采取行动。
何时使用此技能
反模式触发条件(编写代码前采取行动):
- 准备编写实用函数——先在代码库中搜索,它可能已经存在。
- 发现自己在想“我就在这里内联实现吧”。
- 计划复制粘贴并稍作修改现有函数。
- 添加可能与现有类型重叠的新枚举或结构体。
- 两个
Plugin配置包含大量相同的样板代码。 - 相似的 Bevy 系统仅在其
Query过滤组件上有所不同。 - 重复出现的
commands.spawn((Mesh3d(...), MeshMaterial3d(...), ...))模式本该归入Bundle。
工具集成触发条件(运行检查):
- 提交前:确认没有引入跨文件重复代码。
- 清理冲刺期间:找出适合合并的代码。
- 设置提交前钩子或 GitHub Actions 门禁。
- 为了减少过多或过少的检测结果,调整
-t/--threshold或--min-lines。
标准工作流
模式是:先搜索 → 尽量少写 → 运行检查。
# 1. Search before writing — check whether the helper already exists
rg "fn <verb>" # e.g., rg "fn normalize" to find existing helpers
cargo doc --open --package <dep> # inventory what your deps already provide
# 2. Write only what's missing.
# 3. Before committing, check for accidental duplication
cargo install similarity-rs # one-time install
similarity-rs ./src # scan the src tree
# 4. If --cross-file output is needed (catches LLM's most common antipattern)
similarity-rs --cross-file ./src
# 5. Tighten or loosen if needed (see references/tuning.md)
similarity-rs -t 0.85 -m 5 --cross-file --skip-test ./src
在编写一次性辅助函数前,先查阅每个依赖项的 cargo doc --open。与凭空编写相比,LLM 更常从标准库、itertools 或已经声明的 crate 依赖中复现函数。
主题
| 主题 | 参考资料 |
|---|---|
| LLM 重复代码模式及前后示例 | references/antipatterns.md |
| 先搜索再编写工作流(rg、cargo doc、扫描 CLAUDE.md) | references/search-before-write.md |
| 提交前钩子和 GitHub Actions CI 配置示例 | references/ci-integration.md |
-t、--min-lines、--cross-file、--no-size-penalty 调整项 | references/tuning.md |
| 相似代码有意为之的情况——无需处理 | references/false-positives.md |
多语言代码库的 similarity-ts、similarity-py、similarity-generic | references/sibling-tools.md |
注意事项
-
默认关闭
--cross-file。 不启用时,只会报告同一文件内的代码对。大多数由 LLM 引入的重复代码都分布在不同文件中。除非你只想检查单个文件内的代码,否则始终传入--cross-file。 -
Beta 工具。 上游 README 标明
similarity-rs尚未经过生产环境测试。请将输出视为参考信号,而非最终结论。重构前请逐一检查所有标记出的代码对。 -
CI 中使用
--skip-test。 测试文件通常会有意包含结构相似的设置样例。在 CI 中使用--skip-test,以排除预期的测试样例对称性;在本地检查测试代码的重复情况。 -
阈值需按项目调整。 默认的
-t 0.85可捕获明显的复制粘贴代码。清理冲刺期间可使用 0.75–0.80(命中更多);生产门禁可使用 0.90–0.95(误报更少)。参见 references/tuning.md。 -
此技能没有 tester 代码片段。
similarity-rs是仅提供命令行界面的工具,没有库 API,因此没有可用于验证的cargo check示例。这与bevy-cargo-features和bevy-migration-0-17-to-0-18的处理方式一致,它们也没有 tester 示例。
另请参阅
bevy-cargo-features— 通过cargo install安装similarity-rs与Cargo.toml中的功能配置相关;还涵盖可能隐藏意外依赖重复的default-features决策。bevy-core-concepts— 重复的Plugin配置和调度样板代码,是此工具最常捕获的 Bevy 特定反模式。bevy-ecs-components— 添加功能时若未进行整合,常会出现近似重复的组件结构体和彼此重叠的枚举。