rust-refactor-helper について
Rust のリファクタリングスキルで、シンボルの名前変更、関数の抽出やインライン化、モジュール移動の前にコードへの影響を分析します。LSP 操作を使い、dry run で変更を確認できます。
主な機能
- 01シンボルの名前変更
- 02関数の抽出またはインライン化
- 03モジュール間でシンボルを移動
- 04参照と依存関係を分析
- 05dry run で変更を確認
活用シーン
- 01名前変更が参照箇所に与える影響を確認
- 02指定範囲から関数を抽出
- 03シンボル移動前に依存関係を確認
MCP 対応
findReferences や goToDefinition などの LSP 操作で、リファクタリング前に Rust コードを分析します。MCP ツールの公開や MCP サーバーの管理については記載されていません。
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ actionbook/ rust-skills --skill rust-refactor-helperインストール後、Claude Code で/rust-refactor-helperを入力して読み込まれたか確認します。
詳細
Rust リファクタリングヘルパー
影響を包括的に分析し、安全にリファクタリングします。
使用方法
/rust-refactor-helper <action> <target> [--dry-run]
操作:
rename <old> <new>- シンボル名を変更しますextract-fn <selection>- 選択範囲を関数に抽出しますinline <fn>- 関数をインライン化しますmove <symbol> <dest>- シンボルをモジュールに移動します
例:
/rust-refactor-helper rename parse_config load_config/rust-refactor-helper extract-fn src/main.rs:20-35/rust-refactor-helper move UserService src/services/
使用する LSP 操作
リファクタリング前の分析
# Find all references before renaming
LSP(
operation: "findReferences",
filePath: "src/lib.rs",
line: 25,
character: 8
)
# Get symbol info
LSP(
operation: "hover",
filePath: "src/lib.rs",
line: 25,
character: 8
)
# Check call hierarchy for move operations
LSP(
operation: "incomingCalls",
filePath: "src/lib.rs",
line: 25,
character: 8
)
リファクタリングのワークフロー
1. シンボル名の変更
User: "Rename parse_config to load_config"
│
▼
[1] Find symbol definition
LSP(goToDefinition)
│
▼
[2] Find ALL references
LSP(findReferences)
│
▼
[3] Categorize by file
│
▼
[4] Check for conflicts
- Is 'load_config' already used?
- Are there macro-generated uses?
│
▼
[5] Show impact analysis (--dry-run)
│
▼
[6] Apply changes with Edit tool
出力:
## Rename: parse_config → load_config
### Impact Analysis
**Definition:** src/config.rs:25
**References found:** 8
| File | Line | Context | Change |
|------|------|---------|--------|
| src/config.rs | 25 | `pub fn parse_config(` | Definition |
| src/config.rs | 45 | `parse_config(path)?` | Call |
| src/main.rs | 12 | `config::parse_config` | Import |
| src/main.rs | 30 | `let cfg = parse_config(` | Call |
| src/lib.rs | 8 | `pub use config::parse_config` | Re-export |
| tests/config_test.rs | 15 | `parse_config("test.toml")` | Test |
| tests/config_test.rs | 25 | `parse_config("")` | Test |
| docs/api.md | 42 | `parse_config` | Documentation |
### Potential Issues
⚠️ **Documentation reference:** docs/api.md:42 may need manual update
⚠️ **Re-export:** src/lib.rs:8 - public API change
### Proceed?
- [x] --dry-run (preview only)
- [ ] Apply changes
2. 関数の抽出
User: "Extract lines 20-35 in main.rs to a function"
│
▼
[1] Read the selected code block
│
▼
[2] Analyze variables
- Which are inputs? (used but not defined in block)
- Which are outputs? (defined and used after block)
- Which are local? (defined and used only in block)
│
▼
[3] Determine function signature
│
▼
[4] Check for early returns, loops, etc.
│
▼
[5] Generate extracted function
│
▼
[6] Replace original code with call
出力:
## Extract Function: src/main.rs:20-35
### Selected Code
```rust
let file = File::open(&path)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
let config: Config = toml::from_str(&contents)?;
validate_config(&config)?;
```
### Analysis
**Inputs:** path: &Path
**Outputs:** config: Config
**Side Effects:** File I/O, may return error
### Extracted Function
```rust
fn load_and_validate_config(path: &Path) -> Result<Config> {
let file = File::open(path)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
let config: Config = toml::from_str(&contents)?;
validate_config(&config)?;
Ok(config)
}
```
### Replacement
```rust
let config = load_and_validate_config(&path)?;
```
3. シンボルの移動
User: "Move UserService to src/services/"
│
▼
[1] Find symbol and all its dependencies
│
▼
[2] Find all references (callers)
LSP(findReferences)
│
▼
[3] Analyze import changes needed
│
▼
[4] Check for circular dependencies
│
▼
[5] Generate move plan
出力:
## Move: UserService → src/services/user.rs
### Current Location
src/handlers/auth.rs:50-120
### Dependencies (will be moved together)
- struct UserService (50-80)
- impl UserService (82-120)
- const DEFAULT_TIMEOUT (48)
### Import Changes Required
| File | Current | New |
|------|---------|-----|
| src/main.rs | `use handlers::auth::UserService` | `use services::user::UserService` |
| src/handlers/api.rs | `use super::auth::UserService` | `use crate::services::user::UserService` |
| tests/auth_test.rs | `use crate::handlers::auth::UserService` | `use crate::services::user::UserService` |
### New File Structure
```
src/
├── services/
│ ├── mod.rs (NEW - add `pub mod user;`)
│ └── user.rs (NEW - UserService moved here)
├── handlers/
│ └── auth.rs (UserService removed)
```
### Circular Dependency Check
✅ No circular dependencies detected
安全性チェック
| チェック | 目的 |
|---|---|
| 参照の網羅性 | すべての使用箇所が見つかったことを確認します |
| 名前の競合 | 同じ名前の既存シンボルを検出します |
| 可視性の変更 | pub/private のスコープが変わる場合に警告します |
| マクロ生成コード | マクロ内のコードについて警告します |
| ドキュメント | シンボルに言及するドキュメントコメントを検出します |
| テストカバレッジ | 影響を受けるテストを表示します |
ドライランモード
変更をプレビューするため、まず必ず --dry-run を使用してください。
/rust-refactor-helper rename old_name new_name --dry-run
変更を適用せずに、すべての変更内容を表示します。
関連スキル
| 場合 | 参照先 |
|---|---|
| シンボルへ移動する | rust-code-navigator |
| 呼び出しの流れを把握する | rust-call-graph |
| プロジェクト構造を確認する | rust-symbol-analyzer |
| トレイトの実装を確認する | rust-trait-explorer |
コメント
Markdown 対応