| name | code:rust-tooling |
| description | Rust linting (clippy), formatting (rustfmt), type checking (cargo check), coverage (tarpaulin), and CI validation.
<example>
Context: User wants to set up linting
user: "configure clippy pedantic for this project"
</example>
<example>
Context: User needs to validate their project
user: "run all checks on this rust project"
</example>
|
Rust Tooling
Comprehensive guide to Rust linting, formatting, type checking, and coverage.
Type Checking: cargo check
Rust's compiler IS the type checker. Fast check without building:
cargo check
cargo check --all-targets
cargo check --all-features
Linting: Clippy
Installation
Comes with rustup:
rustup component add clippy
Configuration
[lints.clippy]
pedantic = "warn"
nursery = "warn"
missing_errors_doc = "allow"
missing_panics_doc = "allow"
module_name_repetitions = "allow"
Or in code:
#![warn(clippy::pedantic)]
#![allow(clippy::module_name_repetitions)]
Commands
cargo clippy
cargo clippy --all-targets --all-features
cargo clippy --fix
cargo clippy -- -D warnings
Lint Categories
| Category | Flag | Description |
|---|
| Default | (none) | Common issues |
| Pedantic | clippy::pedantic | Stricter, opinionated |
| Nursery | clippy::nursery | Experimental lints |
| Restriction | clippy::restriction | Very strict |
Formatting: rustfmt
Installation
rustup component add rustfmt
Configuration
edition = "2021"
max_width = 100
tab_spaces = 4
use_small_heuristics = "Default"
imports_granularity = "Module"
group_imports = "StdExternalCrate"
reorder_imports = true
Commands
cargo fmt
cargo fmt -- --check
rustfmt src/lib.rs
Test Coverage: tarpaulin
Installation
cargo install cargo-tarpaulin
Commands
cargo tarpaulin --out Html
cargo tarpaulin --out Xml
Alternative: llvm-cov
cargo install cargo-llvm-cov
cargo llvm-cov --html
Project Validation
Run all checks:
cargo fmt -- --check && cargo clippy -- -D warnings && cargo test
cargo fmt -- --check && \
cargo clippy --all-targets --all-features -- -D warnings && \
cargo test --all-features
Recommended Cargo.toml Lints
[lints.rust]
unsafe_code = "forbid"
[lints.clippy]
pedantic = "warn"
nursery = "warn"
missing_errors_doc = "allow"
missing_panics_doc = "allow"
CI Configuration
name: CI
on: [push, pull_request]
env:
CARGO_TERM_COLOR: always
jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt, clippy
- uses: Swatinem/rust-cache@v2
- name: Format check
run: cargo fmt -- --check
- name: Clippy
run: cargo clippy --all-targets --all-features -- -D warnings
- name: Test
run: cargo test --all-features
- name:
With Coverage
- name: Install tarpaulin
run: cargo install cargo-tarpaulin
- name: Coverage
run: cargo tarpaulin --out Xml
- uses: codecov/codecov-action@v4
Pre-commit Hook
#!/bin/sh
cargo fmt -- --check || exit 1
cargo clippy -- -D warnings || exit 1
cargo test || exit 1
Quick Reference
| Operation | Command |
|---|
| Type check | cargo check |
| Lint check | cargo clippy |
| Lint fix | cargo clippy --fix |
| Format check | cargo fmt -- --check |
| Format fix | cargo fmt |
| Test | cargo test |
| Coverage | cargo tarpaulin --out Html |
| All checks | cargo fmt -- --check && cargo clippy -- -D warnings && cargo test |
| All fixes | cargo fmt && cargo clippy --fix --allow-dirty |
Quick Validation
One-liner to validate a Rust project is CI-ready:
cargo fmt -- --check && cargo clippy --all-targets --all-features -- -D warnings && cargo test --all-features
Documentation
cargo doc
cargo doc --open
cargo doc --document-private-items