OpenParable
Skip to content
← Back to skills

Check Similarity Rs

Testing & Quality
check-similarity-rs

Check Similarity Rs is an Agent Skill for testing & quality published in the mizchi/similarity repository on GitHub: a SKILL.md instruction file an AI agent can load to do this task.

Detect duplicate Rust code using AST-based similarity analysis. Use when working with .rs files and looking for code duplication or refactoring opportunities.

View on GitHub ↗
QUICK START

How to use this skill

Bring this guide into your coding agent with a prompt tailored to the tool you use.

  1. Open your project in Codex.
  2. Copy and paste the prompt below.
  3. Review files and risks before approving any action.
Prompt to paste
Inspect the Agent Skill at https://github.com/mizchi/similarity/blob/HEAD/.claude/skills/check-similarity-rs/SKILL.md and all supporting files. Report provenance, permissions, scripts, dependencies, and security risks. Propose an exact installation into .agents/skills/check-similarity-rs, but wait for my approval before writing files or running scripts.

Copying this prompt does not install or run the skill. Review third-party files before use.

Rust Code Similarity Detection

What to do

Run similarity-rs on the target Rust project to detect duplicate functions, struct/enum definitions, and impl methods, then analyze results and propose refactoring.

If similarity-rs is not installed:

cargo install similarity-rs

Step 1: Run similarity analysis

similarity-rs $ARGUMENTS

If no arguments given:

similarity-rs . --threshold 0.85 --min-lines 5

For struct/enum similarity:

similarity-rs . --threshold 0.85 --experimental-types

Step 2: Analyze results

High-priority

  • 100% similarity: Exact duplicates -> extract shared function or use generics
  • 95-100%: Same algorithm on different types -> generic function with trait bounds
  • Duplicate impl methods: Same logic across types -> trait with default implementation

Medium-priority

  • 85-95%: Similar match arms or error handling -> macro or shared helper
  • Parallel struct definitions: Identical fields -> shared base or generic struct

Acceptable

  • Short new() constructors with field initialization
  • Simple From/Into implementations
  • Derive-equivalent implementations

Step 3: Propose refactoring

For each high-priority pair, show before/after code with Rust idioms.

Key Options

OptionDescription
--threshold <0-1>Similarity threshold (default: 0.85)
--min-lines <n>Skip functions shorter than n lines (default: 3)
--min-tokens <n>Skip functions with fewer AST nodes
--printShow actual code snippets
--experimental-typesEnable struct/enum similarity detection
--filter-function <name>Filter by function name
--fail-on-duplicatesExit code 1 if duplicates found

Common Rust refactoring patterns

  • Type-specific functions -> generic fn<T: Trait> with trait bounds
  • Duplicate impl blocks across types -> trait with default methods
  • Repeated match patterns -> macro_rules! or helper function
  • Parallel X/Y/Z functions -> enum parameter or tuple-based abstraction
  • Similar error handling -> shared Result combinator or ? chain