Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Identifies and corrects common Rust anti-patterns like excessive clone, unwrap in production, and OOP-style Deref misuse.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
SKILL.md
1---2name: m15-anti-pattern3description: "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, 反模式, 常见错误, 代码异味, 最佳实践, 地道写法"4user-invocable: false5---67# Anti-Patterns89> **Layer 2: Design Choices**1011## Core Question1213**Is this pattern hiding a design problem?**1415When reviewing code:16- Is this solving the symptom or the cause?17- Is there a more idiomatic approach?18- Does this fight or flow with Rust?1920---2122## Anti-Pattern → Better Pattern2324| Anti-Pattern | Why Bad | Better |25|--------------|---------|--------|26| `.clone()` everywhere | Hides ownership issues | Proper references or ownership |27| `.unwrap()` in production | Runtime panics | `?`, `expect`, or handling |28| `Rc` when single owner | Unnecessary overhead | Simple ownership |29| `unsafe` for convenience | UB risk | Find safe pattern |30| OOP via `Deref` | Misleading API | Composition, traits |31| Giant match arms | Unmaintainable | Extract to methods |32| `String` everywhere | Allocation waste | `&str`, `Cow<str>` |33| Ignoring `#[must_use]` | Lost errors | Handle or `let _ =` |3435---3637## Thinking Prompt3839When seeing suspicious code:40411. **Is this symptom or cause?**42- Clone to avoid borrow? → Ownership design issue43- Unwrap "because it won't fail"? → Unhandled case44452. **What would idiomatic code look like?**46- References instead of clones47- Iterators instead of index loops48- Pattern matching instead of flags49503. **Does this fight Rust?**51- Fighting borrow checker → restructure52- Excessive unsafe → find safe pattern5354---5556## Trace Up ↑5758To design understanding:5960```61"Why does my code have so many clones?"62↑ Ask: Is the ownership model correct?63↑ Check: m09-domain (data flow design)64↑ Check: m01-ownership (reference patterns)65```6667| Anti-Pattern | Trace To | Question |68|--------------|----------|----------|69| Clone everywhere | m01-ownership | Who should own this data? |70| Unwrap everywhere | m06-error-handling | What's the error strategy? |71| Rc everywhere | m09-domain | Is ownership clear? |72| Fighting lifetimes | m09-domain | Should data structure change? |7374---7576## Trace Down ↓7778To implementation (Layer 1):7980```81"Replace clone with proper ownership"82↓ m01-ownership: Reference patterns83↓ m02-resource: Smart pointer if needed8485"Replace unwrap with proper handling"86↓ m06-error-handling: ? operator87↓ m06-error-handling: expect with message88```8990---9192## Top 5 Beginner Mistakes9394| Rank | Mistake | Fix |95|------|---------|-----|96| 1 | Clone to escape borrow checker | Use references |97| 2 | Unwrap in production | Propagate with `?` |98| 3 | String for everything | Use `&str` |99| 4 | Index loops | Use iterators |100| 5 | Fighting lifetimes | Restructure to own data |101102## Code Smell → Refactoring103104| Smell | Indicates | Refactoring |105|-------|-----------|-------------|106| Many `.clone()` | Ownership unclear | Clarify data flow |107| Many `.unwrap()` | Error handling missing | Add proper handling |108| Many `pub` fields | Encapsulation broken | Private + accessors |109| Deep nesting | Complex logic | Extract methods |110| Long functions | Multiple responsibilities | Split |111| Giant enums | Missing abstraction | Trait + types |112113---114115## Common Error Patterns116117| Error | Anti-Pattern Cause | Fix |118|-------|-------------------|-----|119| E0382 use after move | Cloning vs ownership | Proper references |120| Panic in production | Unwrap everywhere | ?, matching |121| Slow performance | String for all text | &str, Cow |122| Borrow checker fights | Wrong structure | Restructure |123| Memory bloat | Rc/Arc everywhere | Simple ownership |124125---126127## Deprecated → Better128129| Deprecated | Better |130|------------|--------|131| Index-based loops | `.iter()`, `.enumerate()` |132| `collect::<Vec<_>>()` then iterate | Chain iterators |133| Manual unsafe cell | `Cell`, `RefCell` |134| `mem::transmute` for casts | `as` or `TryFrom` |135| Custom linked list | `Vec`, `VecDeque` |136| `lazy_static!` | `std::sync::OnceLock` |137138---139140## Quick Review Checklist141142- [ ] No `.clone()` without justification143- [ ] No `.unwrap()` in library code144- [ ] No `pub` fields with invariants145- [ ] No index loops when iterator works146- [ ] No `String` where `&str` suffices147- [ ] No ignored `#[must_use]` warnings148- [ ] No `unsafe` without SAFETY comment149- [ ] No giant functions (>50 lines)150151---152153## Related Skills154155| When | See |156|------|-----|157| Ownership patterns | m01-ownership |158| Error handling | m06-error-handling |159| Mental models | m14-mental-model |160| Performance | m10-performance |161