| name | rust-code-quality |
| description | Apply rustfmt and clippy linting standards for consistent, safe Rust code. Use before commits and in CI pipelines. |
Code Quality
Maintain consistent, safe Rust code with rustfmt and clippy.
Rustfmt Configuration
Create rustfmt.toml in project root:
max_width = 100
edition = "2021"
imports_granularity = "Crate"
group_imports = "StdExternalCrate"
reorder_imports = true
Run before every commit:
cargo fmt
cargo fmt --check
Clippy Configuration
Add to lib.rs or main.rs:
#![warn(clippy::all, clippy::pedantic)]
#![deny(clippy::unwrap_used)]
#![deny(clippy::expect_used)]
#![warn(missing_docs)]
Or in Cargo.toml:
[lints.clippy]
all = "warn"
pedantic = "warn"
unwrap_used = "deny"
expect_used = "deny"
Run clippy:
cargo clippy
cargo clippy -- -D warnings
Recommended Clippy Lints
#![deny(clippy::unwrap_used)]
#![deny(clippy::expect_used)]
#![deny(clippy::panic)]
#![deny(clippy::todo)]
#![warn(clippy::pedantic)]
#![warn(clippy::nursery)]
#![warn(clippy::cargo)]
#![warn(missing_docs)]
CI Integration
GitHub Actions example:
name: Rust CI
on: [push, pull_request]
jobs:
check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt, clippy
- name: Format check
run: cargo fmt --check
- name: Clippy
run: cargo clippy -- -D warnings
- name: Tests
run: cargo test
Common Clippy Fixes
let value = some_option.unwrap();
let value = some_option.ok_or(Error::MissingValue)?;
let arc2 = arc1.clone();
let arc2 = Arc::clone(&arc1);
items.iter().map(|x| process(x))
items.iter().map(process)
Pre-commit Hook
Create .git/hooks/pre-commit:
#!/bin/sh
cargo fmt --check || exit 1
cargo clippy -- -D warnings || exit 1
Guidelines
- Run
cargo fmt before every commit
- Fix all clippy warnings before merging
- Use
#[allow(clippy::...)] sparingly with justification
- Enable pedantic lints for library code
- Document all public items
Examples
See hercules-local-algo/src/lib.rs for lint configuration.