Skip to content

Add cargo clippy and rustfmt checks to CI #21

@cbaugus

Description

@cbaugus

Summary

Add static analysis and formatting checks to the CI pipeline to maintain code quality and consistency. Currently, there are no automated linting or formatting gates.

Tools to Add

1. Clippy (Rust Linter)

Catches common mistakes, suggests improvements, and enforces Rust best practices.

cargo clippy --all-targets --all-features -- -D warnings

2. Rustfmt (Code Formatter)

Ensures consistent code formatting across the project.

cargo fmt --check

3. Cargo Audit (Optional - Security)

Checks dependencies for known security vulnerabilities.

cargo audit

Proposed CI Configuration

Add to .github/workflows/build-cicd.yaml:

jobs:
  lint:
    name: Lint and Format
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      
      - name: Install Rust toolchain
        uses: dtolnay/rust-action@stable
        with:
          components: rustfmt, clippy
          
      - name: Cache dependencies
        uses: Swatinem/rust-cache@v2
        
      - name: Check formatting
        run: cargo fmt --all --check
        
      - name: Run Clippy
        run: cargo clippy --all-targets --all-features -- -D warnings
        
      - name: Check for security vulnerabilities
        run: |
          cargo install cargo-audit
          cargo audit

  test:
    needs: lint  # Run tests only if lint passes
    # ... test configuration

  build:
    needs: test  # Build only if tests pass
    # ... build configuration

Clippy Configuration

Create clippy.toml in project root for custom lint configuration:

# clippy.toml
avoid-breaking-exported-api = false

Or configure via Cargo.toml:

[lints.clippy]
pedantic = "warn"
nursery = "warn"
unwrap_used = "warn"
expect_used = "warn"

Rustfmt Configuration

Create rustfmt.toml for consistent formatting:

# rustfmt.toml
edition = "2021"
max_width = 100
tab_spaces = 4
use_small_heuristics = "Default"

Acceptance Criteria

  • cargo fmt --check runs in CI
  • cargo clippy runs in CI with -D warnings (treat warnings as errors)
  • Lint job runs before test job
  • Failed lint checks block PR merge
  • rustfmt.toml created with project formatting standards
  • Existing code passes all checks (fix any existing issues first)
  • Optional: cargo audit for security checks

Pre-Implementation Steps

Before enabling these checks in CI, fix any existing issues:

# Fix formatting
cargo fmt --all

# Check for clippy warnings and fix them
cargo clippy --all-targets --all-features

# Check for security vulnerabilities
cargo audit

Benefits

  • Consistency: All code follows same formatting standards
  • Quality: Clippy catches common bugs and anti-patterns
  • Security: Audit catches vulnerable dependencies
  • Faster Reviews: No need to comment on style issues in PRs
  • Learning: Clippy suggestions help developers learn Rust idioms

Priority

Medium - Important for code quality but can be added after core testing is in place.

Related Issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions