| name | rust-quality-checker |
| description | Validate Rust code quality with rustfmt, clippy, cargo check, and security analysis. Use for Rust codebases to ensure idiomatic Rust code and best practices. |
| allowed-tools | Read, Bash, Grep, Glob |
Rust Quality Checker Skill
Purpose
This skill provides comprehensive Rust code quality validation including formatting (rustfmt), linting (clippy), compilation checks (cargo check), testing (cargo test), security analysis (cargo audit), and best practices validation. Ensures code meets Rust idioms and project standards.
When to Use
- Validating Rust code quality before commit
- Running pre-commit quality checks
- CI/CD quality gate validation
- Code review preparation
- Ensuring idiomatic Rust code
- Security vulnerability detection
- Performance optimization validation
Quality Check Workflow
1. Environment Setup
Verify Rust Toolchain:
rustc --version
cargo --version
rustfmt --version
cargo clippy --version
Install/Update Components:
rustup update
rustup component add rustfmt
rustup component add clippy
cargo install cargo-audit
cargo install cargo-tarpaulin
Deliverable: Rust toolchain ready
2. Code Formatting Check (rustfmt)
Check Formatting:
cargo fmt -- --check
cargo fmt --edition 2021 -- --check
rustfmt --check src/main.rs
cargo fmt --verbose -- --check
Auto-Format Code:
cargo fmt
cargo fmt --edition 2021
rustfmt src/main.rs
Configuration (rustfmt.toml):
edition = "2021"
max_width = 100
hard_tabs = false
tab_spaces = 4
newline_style = "Unix"
use_small_heuristics = "Default"
reorder_imports = true
reorder_modules = true
remove_nested_parens = true
fn_single_line = false
where_single_line = false
imports_granularity = "Crate"
group_imports = "StdExternalCrate"
Deliverable: Formatting validation report
3. Compilation Check (cargo check)
Fast Compilation Check:
cargo check
cargo check --all-features
cargo check --workspace
cargo check --package my-package
cargo check --verbose
Full Compilation:
cargo build
cargo build --release
cargo build --all-features
cargo build --all-targets
Deliverable: Compilation status report
4. Linting (clippy)
Run Clippy:
cargo clippy
cargo clippy -- -D warnings
cargo clippy -- -W clippy::pedantic
cargo clippy -- -W clippy::all
cargo clippy -- -W clippy::nursery
cargo clippy --workspace
Clippy with Auto-Fix:
cargo clippy --fix
cargo clippy --fix -- -D warnings
Configuration (clippy.toml or .clippy.toml):
msrv = "1.70"
allow = [
"clippy::module_name_repetitions",
]
warn = [
"clippy::all",
"clippy::pedantic",
"clippy::cargo",
]
deny = [
"clippy::unwrap_used",
"clippy::expect_used",
"clippy::panic",
]
Deliverable: Clippy lint report
5. Testing (cargo test)
Run Tests:
cargo test
cargo test -- --nocapture
cargo test test_name
cargo test module::test_name
cargo test --doc
cargo test --all-features
Test with Coverage:
cargo install cargo-tarpaulin
cargo tarpaulin --out Html --out Xml
cargo tarpaulin --fail-under 80
cargo tarpaulin --workspace
Deliverable: Test execution and coverage report
6. Security Analysis (cargo audit)
Dependency Security Check:
cargo install cargo-audit
cargo audit
cargo audit --json
cargo audit fix
cargo audit --deny yanked
Update Dependencies:
cargo install cargo-outdated
cargo outdated
cargo update
cargo update -p dependency-name
Deliverable: Security audit report
7. Dependency Analysis
Check Dependency Tree:
cargo tree
cargo tree --package my-package
cargo tree --duplicates
cargo tree --features feature-name
cargo tree --invert
Check Unused Dependencies:
cargo install cargo-udeps
cargo +nightly udeps
Deliverable: Dependency analysis report
8. Code Quality Checks
Check for Unsafe Code:
grep -r "unsafe" src/
grep -r "unsafe" src/ | wc -l
cargo install cargo-geiger
cargo geiger
Check Documentation:
cargo doc
cargo rustdoc -- -D missing_docs
cargo test --doc
Deliverable: Code quality assessment
9. Performance Analysis
Benchmark Tests:
cargo bench
cargo bench benchmark_name
cargo bench
Performance Profiling:
cargo build --release
cargo install flamegraph
cargo flamegraph
perf record --call-graph dwarf ./target/release/binary
perf report
Deliverable: Performance analysis report
10. Comprehensive Quality Check
Run All Checks:
#!/bin/bash
set -e
echo "=== Rust Quality Checks ==="
echo "1. Code Formatting (rustfmt)..."
cargo fmt -- --check
echo "2. Compilation Check..."
cargo check --workspace
echo "3. Linting (clippy)..."
cargo clippy --workspace -- -D warnings
echo "4. Testing..."
cargo test --workspace
echo "5. Documentation Check..."
cargo doc --no-deps --document-private-items
echo "6. Security Audit..."
cargo audit
echo "7. Unused Dependencies..."
cargo +nightly udeps || true
echo "=== All Quality Checks Passed ✅ ==="
Makefile Integration:
.PHONY: check fmt lint test quality
check:
cargo check --workspace
fmt:
cargo fmt
fmt-check:
cargo fmt -- --check
lint:
cargo clippy --workspace -- -D warnings
test:
cargo test --workspace
quality: fmt-check check lint test
@echo "All quality checks passed ✅"
Deliverable: Comprehensive quality report
Quality Standards
Code Formatting
Compilation
Linting
Testing
Security
Code Quality
Quality Check Matrix
| Check | Tool | Threshold | Auto-Fix |
|---|
| Formatting | rustfmt | Must pass | Yes |
| Compilation | cargo check | 0 errors | No |
| Linting | clippy | 0 warnings | Partial |
| Testing | cargo test | All pass | No |
| Coverage | tarpaulin | ≥ 80% | No |
| Security | cargo audit | 0 critical | Partial |
| Unsafe code | cargo geiger | Minimize | No |
Pre-commit Integration
Setup Git Hooks:
#!/bin/bash
echo "Running Rust quality checks..."
if ! cargo fmt -- --check; then
echo "❌ Code not formatted. Run: cargo fmt"
exit 1
fi
if ! cargo clippy -- -D warnings; then
echo "❌ Clippy warnings found"
exit 1
fi
if ! cargo test --quiet; then
echo "❌ Tests failed"
exit 1
fi
echo "✅ All pre-commit checks passed"
Make hook executable:
chmod +x .git/hooks/pre-commit
Deliverable: Pre-commit hooks configured
CI/CD Integration
GitHub Actions Example:
name: Rust Quality Checks
on: [push, pull_request]
env:
CARGO_TERM_COLOR: always
jobs:
quality:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install Rust toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
components: rustfmt, clippy
override: true
- name: Cache cargo registry
uses: actions/cache@v3
with:
path: ~/.cargo/registry
key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}
- name: Check formatting
run: cargo fmt -- --check
- name: Check compilation
run: cargo check --workspace
- name: Run clippy
run: cargo clippy --workspace -- -D warnings
- name: Run tests
run: cargo test --workspace --verbose
- name: Check documentation
run: cargo doc --no-deps --document-private-items
- name: Security audit
run: |
cargo install cargo-audit
cargo audit
- name: Generate coverage
run: |
cargo install cargo-tarpaulin
cargo tarpaulin --out Xml --fail-under 80
- name: Upload coverage
uses: codecov/codecov-action@v3
with:
files: ./cobertura.xml
Deliverable: CI/CD quality pipeline
Quality Check Troubleshooting
rustfmt Formatting Failures
cargo fmt -- --check
cargo fmt
rustfmt --check src/main.rs
Compilation Errors
cargo check --verbose
cargo clean
cargo check
cargo tree
Clippy Warnings
cargo clippy -- -W clippy::all
cargo clippy --fix
Test Failures
cargo test -- --nocapture
cargo test test_name -- --exact
cargo test -- --show-output
Quality Report Template
# Rust Quality Check Report
## Summary
- **Status**: ✅ All checks passed
- **Date**: 2024-01-15
- **Project**: my-rust-project
## Checks Performed
### Formatting (rustfmt)
- **Status**: ✅ PASS
- **Files Checked**: 42
- **Issues**: 0
### Compilation (cargo check)
- **Status**: ✅ PASS
- **Workspace**: All packages compile
- **Warnings**: 0
### Linting (clippy)
- **Status**: ✅ PASS
- **Warnings**: 0
- **Pedantic**: Enabled
### Testing (cargo test)
- **Status**: ✅ PASS
- **Tests**: 156 passed
- **Doc Tests**: 23 passed
### Coverage (tarpaulin)
- **Status**: ✅ PASS
- **Coverage**: 87% (target: 80%)
### Security (cargo audit)
- **Status**: ✅ PASS
- **Vulnerabilities**: 0
- **Yanked Crates**: 0
### Documentation
- **Status**: ✅ PASS
- **Docs Build**: Success
- **Missing Docs**: 0
## Details
All Rust quality checks passed successfully. Code is well-formatted, compiles cleanly, passes all lints, has good test coverage, and is secure.
## Recommendations
- Continue maintaining high test coverage
- Keep dependencies updated
- Document all public APIs
- Minimize unsafe code usage
Integration with Code Quality Specialist
Input: Rust codebase quality check request
Process: Run all Rust quality tools and analyze results
Output: Comprehensive quality report with pass/fail status
Next Step: Report to code-quality-specialist for consolidation
Best Practices
Development
- Run
cargo fmt on save (IDE integration)
- Enable clippy in IDE for real-time feedback
- Use
cargo watch for continuous checking
- Fix warnings immediately
Pre-Commit
- Run full quality check script
- Ensure all tests pass
- Verify no clippy warnings
- Check documentation builds
CI/CD
- Run quality checks on every PR
- Fail build on warnings
- Generate coverage reports
- Track quality metrics over time
- Cache dependencies for speed
Code Review
- Verify quality checks passed
- Review clippy suggestions
- Check test coverage
- Validate unsafe code usage
Supporting Resources
Success Metrics