Skip to content

Add automated GitHub Actions workflow to run cargo clippy and cargo fmt --check across all contract packages on PRs #599

Description

@Bilalishaq7

Context

Code quality and consistency are critical for smart contract development. Currently, there is no automated validation of code formatting and linting across the contract packages. This leads to inconsistent code style, potential bugs, and manual review overhead. A GitHub Actions workflow will enforce cargo fmt formatting standards and cargo clippy linting rules automatically on every pull request.

Specifications

Workflow File

  • Location: .github/workflows/code-quality.yml (or similar)
  • Trigger: On pull_request events to any branch
  • Runtime: GitHub-hosted runner (ubuntu-latest recommended)

Workflow Steps

  1. Checkout Code

    • Use actions/checkout@v4 to fetch PR code
  2. Install Rust Toolchain

    • Install stable Rust toolchain
    • Ensure rustfmt and clippy components are available
  3. Run cargo fmt --check

    • Validate formatting across all contract packages
    • Command: cargo fmt --all -- --check
    • Fail the workflow if formatting violations are found
    • Provide clear error message: "Code must be formatted using cargo fmt"
  4. Run cargo clippy

    • Lint all contract packages
    • Command: cargo clippy --all --all-targets -- -D warnings
    • Treat warnings as errors (fail on warnings)
    • Provide clear error message: "Code must pass cargo clippy checks"
  5. Optional: Code Coverage (Future enhancement)

    • Run tests and generate coverage reports

Workflow Configuration

  • Concurrency: Cancel in-progress workflows for the same PR to save resources
  • Matrix (Optional): Test against multiple Rust versions if needed
  • Caching: Cache Rust build artifacts to speed up subsequent runs
  • Notifications: GitHub will automatically comment on PR with failures

Acceptance Criteria

  • GitHub Actions workflow file created at .github/workflows/code-quality.yml
  • Workflow triggers on all pull requests
  • cargo fmt --check runs and validates formatting for all packages
  • cargo clippy -- -D warnings runs and treats warnings as errors
  • Workflow fails if formatting or linting violations are found
  • Workflow passes when all checks succeed
  • Clear, actionable error messages guide developers to fix issues
  • Workflow uses caching for faster execution
  • Workflow runs on all contract packages without modification
  • Documentation added to CONTRIBUTING.md or similar guide

Verification

  1. Manual Testing:

    • Create a test PR with intentional formatting violations
    • Verify workflow detects and blocks the PR
    • Create a test PR with intentional clippy warnings
    • Verify workflow detects and blocks the PR
  2. Valid PR:

    • Create a PR with properly formatted code and no clippy warnings
    • Verify workflow passes
  3. Performance:

    • Verify workflow completes in < 5 minutes

Contributor Hints

  • Use actions/checkout@v4 for reliable code checkout
  • Use actions-rs/toolchain@v1 or dtolnay/rust-toolchain for Rust installation
  • Add cache: cargo action to speed up builds
  • Document the workflow in your repository's CONTRIBUTING.md
  • Consider adding a pre-commit hook for local development (optional but recommended)
  • Example pre-commit configuration:
    cargo fmt --all
    cargo clippy --all -- -D warnings
  • Ensure all developers run these checks locally before pushing

Example Workflow Template

name: Code Quality

on:
  pull_request:

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  format:
    name: Format Check
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - run: cargo fmt --all -- --check

  lint:
    name: Clippy Lint
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy
      - uses: Swatinem/rust-cache@v2
      - run: cargo clippy --all --all-targets -- -D warnings

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Stellar WaveIssues in the Stellar wave programeasyGood for newcomers, low complexity

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions