PluginBench
Skill
Pass
Audit score 90

m15-anti-pattern

zhanghandong/rust-skills

How to install m15-anti-pattern

npx skills add https://github.com/zhanghandong/rust-skills --skill m15-anti-pattern
Claude Code
Cursor
Windsurf
Cline
Full instructions (SKILL.md)

Source of truth, from zhanghandong/rust-skills.


name: m15-anti-pattern description: "Use when reviewing code for anti-patterns. Keywords: anti-pattern, common mistake, pitfall, code smell, bad practice, code review, is this an anti-pattern, better way to do this, common mistake to avoid, why is this bad, idiomatic way, beginner mistake, fighting borrow checker, clone everywhere, unwrap in production, should I refactor, 反模式, 常见错误, 代码异味, 最佳实践, 地道写法" user-invocable: false

Anti-Patterns

Layer 2: Design Choices

Core Question

Is this pattern hiding a design problem?

When reviewing code:

  • Is this solving the symptom or the cause?
  • Is there a more idiomatic approach?
  • Does this fight or flow with Rust?

Anti-Pattern → Better Pattern

Anti-PatternWhy BadBetter
.clone() everywhereHides ownership issuesProper references or ownership
.unwrap() in productionRuntime panics?, expect, or handling
Rc when single ownerUnnecessary overheadSimple ownership
unsafe for convenienceUB riskFind safe pattern
OOP via DerefMisleading APIComposition, traits
Giant match armsUnmaintainableExtract to methods
String everywhereAllocation waste&str, Cow<str>
Ignoring #[must_use]Lost errorsHandle or let _ =

Thinking Prompt

When seeing suspicious code:

  1. Is this symptom or cause?

    • Clone to avoid borrow? → Ownership design issue
    • Unwrap "because it won't fail"? → Unhandled case
  2. What would idiomatic code look like?

    • References instead of clones
    • Iterators instead of index loops
    • Pattern matching instead of flags
  3. Does this fight Rust?

    • Fighting borrow checker → restructure
    • Excessive unsafe → find safe pattern

Trace Up ↑

To design understanding:

"Why does my code have so many clones?"
    ↑ Ask: Is the ownership model correct?
    ↑ Check: m09-domain (data flow design)
    ↑ Check: m01-ownership (reference patterns)
Anti-PatternTrace ToQuestion
Clone everywherem01-ownershipWho should own this data?
Unwrap everywherem06-error-handlingWhat's the error strategy?
Rc everywherem09-domainIs ownership clear?
Fighting lifetimesm09-domainShould data structure change?

Trace Down ↓

To implementation (Layer 1):

"Replace clone with proper ownership"
    ↓ m01-ownership: Reference patterns
    ↓ m02-resource: Smart pointer if needed

"Replace unwrap with proper handling"
    ↓ m06-error-handling: ? operator
    ↓ m06-error-handling: expect with message

Top 5 Beginner Mistakes

RankMistakeFix
1Clone to escape borrow checkerUse references
2Unwrap in productionPropagate with ?
3String for everythingUse &str
4Index loopsUse iterators
5Fighting lifetimesRestructure to own data

Code Smell → Refactoring

SmellIndicatesRefactoring
Many .clone()Ownership unclearClarify data flow
Many .unwrap()Error handling missingAdd proper handling
Many pub fieldsEncapsulation brokenPrivate + accessors
Deep nestingComplex logicExtract methods
Long functionsMultiple responsibilitiesSplit
Giant enumsMissing abstractionTrait + types

Common Error Patterns

ErrorAnti-Pattern CauseFix
E0382 use after moveCloning vs ownershipProper references
Panic in productionUnwrap everywhere?, matching
Slow performanceString for all text&str, Cow
Borrow checker fightsWrong structureRestructure
Memory bloatRc/Arc everywhereSimple ownership

Deprecated → Better

DeprecatedBetter
Index-based loops.iter(), .enumerate()
collect::<Vec<_>>() then iterateChain iterators
Manual unsafe cellCell, RefCell
mem::transmute for castsas or TryFrom
Custom linked listVec, VecDeque
lazy_static!std::sync::OnceLock

Quick Review Checklist

  • No .clone() without justification
  • No .unwrap() in library code
  • No pub fields with invariants
  • No index loops when iterator works
  • No String where &str suffices
  • No ignored #[must_use] warnings
  • No unsafe without SAFETY comment
  • No giant functions (>50 lines)

Related Skills

WhenSee
Ownership patternsm01-ownership
Error handlingm06-error-handling
Mental modelsm14-mental-model
Performancem10-performance

Related skills

More from zhanghandong/rust-skills and the wider catalog.

CO

coding-guidelines

zhanghandong/rust-skills

Use when asking about Rust code style or best practices. Keywords: naming, formatting, comment, clippy, rustfmt, lint, code style, best practice, P.NAM, G.FMT, code review, naming convention, variable naming, function naming, type naming, 命名规范, 代码风格, 格式化, 最佳实践, 代码审查, 怎么命名

1.2k installsAudited
M1

m10-performance

zhanghandong/rust-skills

CRITICAL: Use for performance optimization. Triggers: performance, optimization, benchmark, profiling, flamegraph, criterion, slow, fast, allocation, cache, SIMD, make it faster, 性能优化, 基准测试

881 installs
M0

m07-concurrency

zhanghandong/rust-skills

CRITICAL: Use for concurrency/async. Triggers: E0277 Send Sync, cannot be sent between threads, thread, spawn, channel, mpsc, Mutex, RwLock, Atomic, async, await, Future, tokio, deadlock, race condition, 并发, 线程, 异步, 死锁

831 installs
M0

m06-error-handling

zhanghandong/rust-skills

CRITICAL: Use for error handling. Triggers: Result, Option, Error, ?, unwrap, expect, panic, anyhow, thiserror, when to panic vs return Result, custom error, error propagation, 错误处理, Result 用法, 什么时候用 panic

810 installs
RU

rust-refactor-helper

zhanghandong/rust-skills

Safe Rust refactoring with LSP analysis. Triggers on: /refactor, rename symbol, move function, extract, 重构, 重命名, 提取函数, 安全重构

806 installs
M0

m01-ownership

zhanghandong/rust-skills

CRITICAL: Use for ownership/borrow/lifetime issues. Triggers: E0382, E0597, E0506, E0507, E0515, E0716, E0106, value moved, borrowed value does not live long enough, cannot move out of, use of moved value, ownership, borrow, lifetime, 'a, 'static, move, clone, Copy, 所有权, 借用, 生命周期

792 installs