Rust: add linting pre-commit hook

This commit is contained in:
Paolo Tranquilli
2024-09-10 10:22:45 +02:00
parent 37afad2f70
commit 38c25f96e5
2 changed files with 23 additions and 0 deletions

View File

@@ -76,3 +76,10 @@ repos:
language: system
entry: bazel run //rust/codegen -- --quiet
pass_filenames: false
- id: rust-lint
name: Run fmt and clippy on Rust code
files: ^rust/extractor/(.*rs|Cargo.toml)$
language: system
entry: python3 rust/lint.py
pass_filenames: false

16
rust/lint.py Executable file
View File

@@ -0,0 +1,16 @@
#!/bin/env python3
import subprocess
import pathlib
import shutil
import sys
extractor_dir = pathlib.Path(__file__).resolve().parent / "extractor"
cargo = shutil.which("cargo")
assert cargo, "no cargo binary found on `PATH`"
fmt = subprocess.run([cargo, "fmt", "--quiet"], cwd=extractor_dir)
clippy = subprocess.run([cargo, "clippy", "--fix", "--allow-dirty", "--allow-staged", "--quiet"],
cwd=extractor_dir)
sys.exit(fmt.returncode or clippy.returncode)