Rust Auto Fixer
Overview
This skill provides an automated loop for fixing Rust compilation errors. It takes a Rust code snippet, runs it through a real compiler (cargo check), analyzes the output, and iteratively applies fixes.
It is particularly useful for:
- •Validating code generated by the Agent.
- •Fixing tricky ownership and borrow checker errors (E0382, E0502).
- •Ensuring dependencies are correctly handled.
Workflow
- •Review Input: Analyze the provided code and context.
- •Check Code: Call
check_rust_code.- •This tool automatically attempts to detect dependencies based on
usestatements.
- •This tool automatically attempts to detect dependencies based on
- •Analyze Results:
23→ - SUCCESS: If the tool returns "SUCCESS" followed by code, the code is valid.
24→ - IMPORTANT: The tool returns the
rustfmtformatted code after "SUCCESS". 25→ - You MUST use this formatted code in your final<code_block>. 26→ - ERRORS: If errors are returned, analyze them using the strategies in Rust Fix Strategies. 27→4. Fix & Verify (Loop Constraint): 26→ - Apply the fix (e.g., adding.clone(), changing lifetimes, fixing types). 27→ - CRITICAL: You have a maximum budget of 3 attempts. 28→ - If the code still fails after 3 fixes, STOP and output the last error with a note: "Max attempts reached." 29→ - (Optional) Re-runcheck_rust_codeto verify. 30→5. Output: Return the final result.
Tools
check_rust_code
Located at scripts/cargo_runner.py.
- •Auto-Dependency Detection: Scans for
use crate::modulepatterns and adds them toCargo.tomlwith version*. - •JSON Output: Returns parsed compiler errors for easy analysis.
- •Formatting: Automatically runs
rustfmton success and returns the formatted code. - •Caching: Uses a persistent build directory to speed up compilation.
Usage:
- •Save the Rust code snippet to a temporary file (e.g.,
temp.rs). - •Run the python script with the file path as an argument:
bash
python scripts/cargo_runner.py temp.rs
- •Read the output. It will be "SUCCESS" or a list of error messages.
Input Schema
xml
<input_schema> <code_snippet> // The Rust code. // Dependencies are auto-detected from 'use' statements. // For macros like #[macro_use] extern crate ..., detection might fail. // Note: For best results, please include use statements for external crates, or mention them in the context. </code_snippet> <context>Optional context</context> </input_schema>
Output Schema
xml
<fixed_result> <original_errors>Summary of errors found</original_errors> <explanation>Explanation of the fix applied</explanation> <code_block>The fixed Rust code</code_block> </fixed_result>