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
-
Checkout Code
- Use
actions/checkout@v4 to fetch PR code
-
Install Rust Toolchain
- Install stable Rust toolchain
- Ensure
rustfmt and clippy components are available
-
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"
-
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"
-
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
Verification
-
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
-
Valid PR:
- Create a PR with properly formatted code and no clippy warnings
- Verify workflow passes
-
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
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 fmtformatting standards andcargo clippylinting rules automatically on every pull request.Specifications
Workflow File
.github/workflows/code-quality.yml(or similar)pull_requestevents to any branchWorkflow Steps
Checkout Code
actions/checkout@v4to fetch PR codeInstall Rust Toolchain
rustfmtandclippycomponents are availableRun cargo fmt --check
cargo fmt --all -- --checkcargo fmt"Run cargo clippy
cargo clippy --all --all-targets -- -D warningscargo clippychecks"Optional: Code Coverage (Future enhancement)
Workflow Configuration
Acceptance Criteria
.github/workflows/code-quality.ymlcargo fmt --checkruns and validates formatting for all packagescargo clippy -- -D warningsruns and treats warnings as errorsVerification
Manual Testing:
Valid PR:
Performance:
Contributor Hints
actions/checkout@v4for reliable code checkoutactions-rs/toolchain@v1ordtolnay/rust-toolchainfor Rust installationcache: cargoaction to speed up buildsExample Workflow Template