Sub topic 11
Enforcing formatting and lints in CI
This chapter assumes you can run cargo fmt and cargo clippy locally and know how lint levels work (allow, warn, deny). It covers making both tools mandatory on every push, so style and lint problems cannot reach the main branch.
Why local checks are not enough
Running the tools by hand depends on each contributor remembering to do it. A CI (continuous integration) job runs the same commands on a clean machine for every push and pull request. If a command exits with a non-zero status, the job fails and the change cannot be merged, provided your repository requires that job to pass.
Two details matter for CI:
- The default commands change files or only warn. CI needs the variants that report a problem through the exit code.
- Results must be reproducible. Pin the toolchain, or accept that a new Rust release can add lints and break a previously green build.
Checking formatting without changing files
cargo fmt rewrites files in place. In CI you want a read-only check instead.
cargo fmt
cargo fmt --all -- --checkThe first command rewrites files, so use it locally. The second prints a diff for every file that is not formatted and exits with a non-zero status if there is any difference. --all covers every package in a workspace. The arguments after -- go to rustfmt itself.
Making Clippy warnings fail the build
By default, Clippy prints warnings and exits with status 0, so CI would pass even with problems. The -D warnings flag turns every warning into an error.
cargo clippy --all-targets --all-features -- -D warnings--all-targetsalso checks tests, examples, and benchmarks, which a plaincargo clippyskips.--all-featuresenables every Cargo feature, so code behind feature flags is linted too.-D warningsdenies all warnings. Arguments after--go to the linter, not to Cargo.
Use this strict form only in CI. Locally, plain warnings are easier to work with while you are still editing.
A complete GitHub Actions job
This workflow runs both checks on every push and pull request.
name: lint
on: [push, 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 warningsThe components line installs rustfmt and clippy. They are separate rustup components, and a minimal toolchain install does not include them. Without this line, the steps fail with a "command not found" style error.
The formatting step runs first because it is the cheapest. A style failure then stops the job before the slower Clippy step, which has to compile the code.
Common errors
- Formatting passes locally but fails in CI. The versions of rustfmt differ, or a
rustfmt.tomlfile is not committed. Commit the config and pin the toolchain version. - New warnings appear without any code change. The
stablechannel moved to a newer release with new lints. Pin a specific version, such as1.82.0, and upgrade deliberately. - Clippy does not report issues in tests. You omitted
--all-targets. cargo clippy --fixis not suited to CI. It edits files on the runner, and those edits are lost. Run it locally, review the diff, and commit the result.
Exercise
Your CI job runs cargo clippy and always passes, even though a developer sees warnings on their machine. Name the change that makes the job fail on warnings, and name the flag that makes Clippy also check test code.
Show answer
Run cargo clippy --all-targets --all-features -- -D warnings. The -D warnings after -- turns warnings into errors so the exit status is non-zero, and --all-targets includes tests, examples, and benchmarks.
Exercise
Why does the workflow list components: rustfmt, clippy for the toolchain step?
Show answer
rustfmt and clippy are separate rustup components. A minimal toolchain install does not include them, so without this line the cargo fmt and cargo clippy steps would fail.