m15-anti-pattern
コードレビューで、よくある間違いや避けるべき悪い書き方(アンチパターン)がないかチェックし、より良い方法や推奨される書き方を提案することで、コードの品質向上を支援するSkill。
📜 元の英語説明(参考)
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, 反模式, 常见错误, 代码异味, 最佳实践, 地道写法
🇯🇵 日本人クリエイター向け解説
コードレビューで、よくある間違いや避けるべき悪い書き方(アンチパターン)がないかチェックし、より良い方法や推奨される書き方を提案することで、コードの品質向上を支援するSkill。
※ jpskill.com 編集部が日本のビジネス現場向けに補足した解説です。Skill本体の挙動とは独立した参考情報です。
下記のコマンドをコピーしてターミナル(Mac/Linux)または PowerShell(Windows)に貼り付けてください。 ダウンロード → 解凍 → 配置まで全自動。
mkdir -p ~/.claude/skills && cd ~/.claude/skills && curl -L -o m15-anti-pattern.zip https://jpskill.com/download/9273.zip && unzip -o m15-anti-pattern.zip && rm m15-anti-pattern.zip
$d = "$env:USERPROFILE\.claude\skills"; ni -Force -ItemType Directory $d | Out-Null; iwr https://jpskill.com/download/9273.zip -OutFile "$d\m15-anti-pattern.zip"; Expand-Archive "$d\m15-anti-pattern.zip" -DestinationPath $d -Force; ri "$d\m15-anti-pattern.zip"
完了後、Claude Code を再起動 → 普通に「動画プロンプト作って」のように話しかけるだけで自動発動します。
💾 手動でダウンロードしたい(コマンドが難しい人向け)
- 1. 下の青いボタンを押して
m15-anti-pattern.zipをダウンロード - 2. ZIPファイルをダブルクリックで解凍 →
m15-anti-patternフォルダができる - 3. そのフォルダを
C:\Users\あなたの名前\.claude\skills\(Win)または~/.claude/skills/(Mac)へ移動 - 4. Claude Code を再起動
⚠️ ダウンロード・利用は自己責任でお願いします。当サイトは内容・動作・安全性について責任を負いません。
🎯 このSkillでできること
下記の説明文を読むと、このSkillがあなたに何をしてくれるかが分かります。Claudeにこの分野の依頼をすると、自動で発動します。
📦 インストール方法 (3ステップ)
- 1. 上の「ダウンロード」ボタンを押して .skill ファイルを取得
- 2. ファイル名の拡張子を .skill から .zip に変えて展開(macは自動展開可)
- 3. 展開してできたフォルダを、ホームフォルダの
.claude/skills/に置く- · macOS / Linux:
~/.claude/skills/ - · Windows:
%USERPROFILE%\.claude\skills\
- · macOS / Linux:
Claude Code を再起動すれば完了。「このSkillを使って…」と話しかけなくても、関連する依頼で自動的に呼び出されます。
詳しい使い方ガイドを見る →- 最終更新
- 2026-05-18
- 取得日時
- 2026-05-18
- 同梱ファイル
- 1
📖 Skill本文(日本語訳)
※ 原文(英語/中国語)を Gemini で日本語化したものです。Claude 自身は原文を読みます。誤訳がある場合は原文をご確認ください。
アンチパターン
レイヤー 2: 設計の選択
中核となる質問
このパターンは設計上の問題を隠蔽していませんか?
コードレビュー時:
- これは症状を解決していますか、それとも原因を解決していますか?
- より慣用的なアプローチはありますか?
- これは Rust と対立していますか、それとも調和していますか?
アンチパターン → より良いパターン
| アンチパターン | なぜ悪いのか | より良いパターン |
|---|---|---|
あらゆる場所での .clone() |
所有権の問題を隠蔽する | 適切な参照または所有権 |
本番環境での .unwrap() |
ランタイムパニック | ?、expect、またはハンドリング |
単一オーナーの場合の Rc |
不要なオーバーヘッド | 単純な所有権 |
利便性のための unsafe |
UB のリスク | 安全なパターンを見つける |
Deref を介した OOP |
誤解を招く API | コンポジション、トレイト |
| 巨大な match アーム | メンテナンスが困難 | メソッドに抽出する |
あらゆる場所での String |
アロケーションの無駄 | &str、Cow<str> |
#[must_use] の無視 |
エラーを見逃す | ハンドルするか let _ = |
思考を促すプロンプト
疑わしいコードを見つけた場合:
-
これは症状ですか、それとも原因ですか?
- 借用を避けるための Clone? → 所有権の設計上の問題
- 「失敗しないから」Unwrap? → 未処理のケース
-
慣用的なコードはどのようなものですか?
- クローンではなく参照
- インデックスループではなくイテレータ
- フラグではなくパターンマッチング
-
これは Rust と対立していますか?
- 借用チェッカーとの戦い → 再構築
- 過剰な unsafe → 安全なパターンを見つける
トレースアップ ↑
設計の理解へ:
"なぜ私のコードにはこんなに多くのクローンがあるのだろう?"
↑ 質問: 所有権モデルは正しいか?
↑ チェック: m09-domain (データフロー設計)
↑ チェック: m01-ownership (参照パターン)
| アンチパターン | トレース先 | 質問 |
|---|---|---|
| あらゆる場所での Clone | m01-ownership | 誰がこのデータを所有すべきか? |
| あらゆる場所での Unwrap | m06-error-handling | エラー戦略は? |
| あらゆる場所での Rc | m09-domain | 所有権は明確か? |
| ライフタイムとの戦い | m09-domain | データ構造を変更すべきか? |
トレースダウン ↓
実装へ (レイヤー 1):
"clone を適切な所有権に置き換える"
↓ m01-ownership: 参照パターン
↓ m02-resource: 必要に応じてスマートポインタ
"unwrap を適切なハンドリングに置き換える"
↓ m06-error-handling: ? 演算子
↓ m06-error-handling: メッセージ付きの expect
初心者が犯しがちな間違いトップ 5
| ランク | 間違い | 修正 |
|---|---|---|
| 1 | 借用チェッカーから逃れるための Clone | 参照を使用する |
| 2 | 本番環境での Unwrap | ? で伝播する |
| 3 | すべてに String | &str を使用する |
| 4 | インデックスループ | イテレータを使用する |
| 5 | ライフタイムとの戦い | データを所有するように再構築する |
コードの臭い → リファクタリング
| 臭い | 示すもの | リファクタリング |
|---|---|---|
多くの .clone() |
所有権が不明確 | データフローを明確にする |
多くの .unwrap() |
エラー処理が欠落 | 適切なハンドリングを追加する |
多くの pub フィールド |
カプセル化が壊れている | Private + アクセッサ |
| 深いネスト | 複雑なロジック | メソッドを抽出する |
| 長い関数 | 複数の責任 | 分割する |
| 巨大な enum | 抽象化が欠落 | トレイト + 型 |
一般的なエラーパターン
| エラー | アンチパターンの原因 | 修正 |
|---|---|---|
| E0382 use after move | クローン対所有権 | 適切な参照 |
| 本番環境でのパニック | あらゆる場所での Unwrap | ?, マッチング |
| 遅いパフォーマンス | すべてのテキストに String | &str, Cow |
| 借用チェッカーとの戦い | 間違った構造 | 再構築 |
| メモリの肥大化 | あらゆる場所での Rc/Arc | 単純な所有権 |
非推奨 → より良いもの
| 非推奨 | より良いもの |
|---|---|
| インデックスベースのループ | .iter()、.enumerate() |
collect::<Vec<_>>() してからイテレート |
イテレータをチェーンする |
| 手動の unsafe cell | Cell、RefCell |
キャストのための mem::transmute |
as または TryFrom |
| カスタムリンクドリスト | Vec、VecDeque |
lazy_static! |
std::sync::OnceLock |
クイックレビューチェックリスト
- [ ] 正当な理由のない
.clone()はない - [ ] ライブラリコードに
.unwrap()はない - [ ] インバリアントを持つ
pubフィールドはない - [ ] イテレータが機能する場合はインデックスループはない
- [ ]
&strで十分な場合にStringはない - [ ] 無視された
#[must_use]警告はない - [ ] SAFETY コメントのない
unsafeはない - [ ] 巨大な関数 (>50 行) はない
関連スキル
| いつ | 参照 |
|---|---|
| 所有権パターン | m01-ownership |
| エラー処理 | m06-error-handling |
| メンタルモデル | m14-mental-model |
| パフォーマンス | m10-performance |
📜 原文 SKILL.md(Claudeが読む英語/中国語)を展開
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-Pattern | Why Bad | Better |
|---|---|---|
.clone() everywhere |
Hides ownership issues | Proper references or ownership |
.unwrap() in production |
Runtime panics | ?, expect, or handling |
Rc when single owner |
Unnecessary overhead | Simple ownership |
unsafe for convenience |
UB risk | Find safe pattern |
OOP via Deref |
Misleading API | Composition, traits |
| Giant match arms | Unmaintainable | Extract to methods |
String everywhere |
Allocation waste | &str, Cow<str> |
Ignoring #[must_use] |
Lost errors | Handle or let _ = |
Thinking Prompt
When seeing suspicious code:
-
Is this symptom or cause?
- Clone to avoid borrow? → Ownership design issue
- Unwrap "because it won't fail"? → Unhandled case
-
What would idiomatic code look like?
- References instead of clones
- Iterators instead of index loops
- Pattern matching instead of flags
-
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-Pattern | Trace To | Question |
|---|---|---|
| Clone everywhere | m01-ownership | Who should own this data? |
| Unwrap everywhere | m06-error-handling | What's the error strategy? |
| Rc everywhere | m09-domain | Is ownership clear? |
| Fighting lifetimes | m09-domain | Should 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
| Rank | Mistake | Fix |
|---|---|---|
| 1 | Clone to escape borrow checker | Use references |
| 2 | Unwrap in production | Propagate with ? |
| 3 | String for everything | Use &str |
| 4 | Index loops | Use iterators |
| 5 | Fighting lifetimes | Restructure to own data |
Code Smell → Refactoring
| Smell | Indicates | Refactoring |
|---|---|---|
Many .clone() |
Ownership unclear | Clarify data flow |
Many .unwrap() |
Error handling missing | Add proper handling |
Many pub fields |
Encapsulation broken | Private + accessors |
| Deep nesting | Complex logic | Extract methods |
| Long functions | Multiple responsibilities | Split |
| Giant enums | Missing abstraction | Trait + types |
Common Error Patterns
| Error | Anti-Pattern Cause | Fix |
|---|---|---|
| E0382 use after move | Cloning vs ownership | Proper references |
| Panic in production | Unwrap everywhere | ?, matching |
| Slow performance | String for all text | &str, Cow |
| Borrow checker fights | Wrong structure | Restructure |
| Memory bloat | Rc/Arc everywhere | Simple ownership |
Deprecated → Better
| Deprecated | Better |
|---|---|
| Index-based loops | .iter(), .enumerate() |
collect::<Vec<_>>() then iterate |
Chain iterators |
| Manual unsafe cell | Cell, RefCell |
mem::transmute for casts |
as or TryFrom |
| Custom linked list | Vec, VecDeque |
lazy_static! |
std::sync::OnceLock |
Quick Review Checklist
- [ ] No
.clone()without justification - [ ] No
.unwrap()in library code - [ ] No
pubfields with invariants - [ ] No index loops when iterator works
- [ ] No
Stringwhere&strsuffices - [ ] No ignored
#[must_use]warnings - [ ] No
unsafewithout SAFETY comment - [ ] No giant functions (>50 lines)
Related Skills
| When | See |
|---|---|
| Ownership patterns | m01-ownership |
| Error handling | m06-error-handling |
| Mental models | m14-mental-model |
| Performance | m10-performance |