From 38c25f96e5c5a71ec65e5a3c37add3733b5691a6 Mon Sep 17 00:00:00 2001 From: Paolo Tranquilli Date: Tue, 10 Sep 2024 10:22:45 +0200 Subject: [PATCH] Rust: add linting pre-commit hook --- .pre-commit-config.yaml | 7 +++++++ rust/lint.py | 16 ++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100755 rust/lint.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 5bd57474cef..cf38289452d 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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 diff --git a/rust/lint.py b/rust/lint.py new file mode 100755 index 00000000000..1af2470dbbc --- /dev/null +++ b/rust/lint.py @@ -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)