Skip to content

v0.1.0

Choose a tag to compare

@ry-sun ry-sun released this 24 Dec 17:13
· 1 commit to main since this release

Rust Pre-Commit Hooks

A set of pre-commit hooks for Rust projects

GitHub License
GitHub Release
GitHub Issues or Pull Requests
GitHub Issues or Pull Requests
Static Badge
GitHub Repo stars

Table of Contents

Implementation

This project uses a build.rs script to automatically install and configure necessary Rust components (rustfmt and clippy) for all installed toolchains. The build script:

  1. Detects all installed Rust toolchains
  2. Installs rustfmt and clippy components for each toolchain
  3. Ensures rustup is properly initialized

This approach ensures that the pre-commit hooks will work correctly regardless of which Rust toolchain you're using.

Hooks

The following pre-commit hooks are available:

Code Formatting

  • rustfmt: Formats individual Rust files using rustfmt

    • ID: rustfmt
    • Runs on: .rs files
  • rustfmt-check: Checks if Rust files are properly formatted without modifying them

    • ID: rustfmt-check
    • Runs on: .rs files
  • cargo-fmt: Formats all bin and lib files in the current crate

    • ID: cargo-fmt
    • Runs on entire project
  • cargo-fmt-check: Checks formatting of all bin and lib files without modifying them

    • ID: cargo-fmt-check
    • Runs on entire project

Linting

  • clippy: Runs Clippy lints on all crates in the project

    • ID: clippy
    • Runs on entire project
  • clippy-driver: Runs Clippy lints on individual Rust files

    • ID: clippy-driver
    • Runs on: .rs files

Example

# .pre-commit-config.yaml

  - repo: https://github.com/rockmagma02/rust-pre-commit-hooks
    rev: v0.1.0
    hooks:
      - id: rustfmt
      - id: rustfmt-check
      - id: cargo-fmt
      - id: cargo-fmt-check
      - id: clippy
      - id: clippy-driver

Known Issues

  1. When modifying (install new) Rust toolchains, you may need to reinstall the pre-commit hooks to ensure the components (rustfmt and clippy) are properly installed for the new toolchain.
pre-commit uninstall
pre-commit clean
pre-commit install
  1. the clippy-driver hook will compile all lib and bin files in the current crate. Will generate a set of binaries in the root of the project, which we strongly recommend not to use this hook.

License

This project is licensed under the MIT License - see the LICENSE file for details.

The MIT License is a permissive license that allows you to use, modify, and distribute this software for any purpose, provided that the license and copyright notice are included.

Full Changelog: https://github.com/rockmagma02/rust-pre-commit-hooks/commits/v0.1.0