How to Run rustfmt and clippy in GitHub Actions
Rust catches a lot at compile time, but rustfmt and clippy catch the style and correctness smells the compiler allows.
Run cargo fmt --check and cargo clippy -- -D warnings so any formatting drift or lint warning fails the job.
Steps
- Install the
rustfmtandclippycomponents with the toolchain. - Run
cargo fmt --all -- --checkto verify formatting. - Run
cargo clippy --all-targets -- -D warningsso warnings become errors.
Workflow
.github/workflows/rust-lint.yml
name: Rust Lint
on: [pull_request]
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt, clippy
- run: cargo fmt --all -- --check
- run: cargo clippy --all-targets --all-features -- -D warningsNotes
- Cache the cargo registry and target dir to keep clippy runs fast.
- On Latchkey managed runners Rust lint jobs run cheaper and self-heal if a runner drops.
Related guides
How to Run gofmt and goimports Checks in GitHub ActionsVerify Go formatting in GitHub Actions with gofmt and goimports so unformatted or unsorted-import Go code fai…
How to Cache the Rust target Directory in GitHub ActionsCache Cargo registry, git deps, and the target directory in GitHub Actions, keyed on Cargo.lock, so Rust CI r…