Auto-fix Rust code with cargo fmt and clippy.
| Step | Command | Description |
|---|---|---|
| 1 | cargo fmt | Format code |
| 2 | cargo clippy --fix | Apply clippy autofixes |
// Beforeuse std::io::Read;// After// (unused import removed automatically)// BeforestructConfig{name:String}// After#[derive(Debug)]structConfig{name:String}// Beforefnfoo(){let x=1;}// Afterfnfoo(){let x = 1;}redundant_field_names→ removes duplicate field namesredundant_static_lifetime→ removes unnecessary'staticunnecessary_cast→ removes unnecessary castsunused_variables→ prefixes with_
# .github/workflows/rust-auto-fix.ymlname: Rust Auto-Fix Boton:
issue_comment:
types: [created]jobs:
rust-auto-fix-request:
if: github.event.issue.pull_request && contains(github.event.comment.body, '/rust-fix')runs-on: ubuntu-latestpermissions:
contents: readpull-requests: writesteps:
- name: Check fix typeid: checkrun: | comment="${{ github.event.comment.body }}" fixes="" if [[ "$comment" == *"/rust-fix all"* ]]; then fixes="all" elif [[ "$comment" == *"/rust-fix clippy"* ]]; then fixes="clippy" elif [[ "$comment" == *"/rust-fix fmt"* ]]; then fixes="fmt" fi echo "fixes=$fixes" >> $GITHUB_OUTPUT - name: Confirm requestif: steps.check.outputs.fixes != ''uses: actions/github-script@v7with:
script: | await github.rest.issues.createComment({ issue_number: context.issue.number, owner: context.repo.owner, repo: context.repo.repo, body: `Applying: ${{ steps.check.outputs.fixes }}` });rust-auto-fix-execute:
if: github.event.issue.pull_request && contains(github.event.comment.body, '/confirm')runs-on: ubuntu-latestpermissions:
contents: writepull-requests: writesteps:
- uses: actions/checkout@v6with:
token: ${{ secrets.GITHUB_TOKEN }}fetch-depth: 0
- name: Checkout PRrun: gh pr checkout ${{ github.event.issue.number }}env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Apply fixuses: libnudget/rust-fix@v1with:
fixes: all# Comment on PR
/rust-fix fmt # Format only
/rust-fix clippy # Clippy fixes only
/rust-fix all # Both (default)
/confirm # Execute after request| Input | Description | Default | Options |
|---|---|---|---|
| fixes | Type of fixes to apply | all | fmt, clippy, all |
1. User comments: /rust-fix clippy
→ Bot replies: Applying: clippy
2. User comments: /confirm
→ Workflow runs:
- cargo fmt
- cargo clippy --fix
- git commit & push
- Clippy fixes are limited to lints with
#[clippy::fix]attribute - Not all clippy warnings have autofixes (e.g.,
needless_range_loop) - Workflow requires
workflow_dispatch: inputs:for manual triggers
MIT