Skip to content

Commit a89808b

Browse files
committed
Add linting for trailing whitespaces, newline symbols, etc.
This PR adds configuration for `pre-commit` tool, which can run `rustfmt` and a few other tools to ensure code style. Contrary to its name, `pre-commit` doesn't have to be installed as a pre-commit hook. Instead, it can be invoked manually instead of `cargo fmt`, or registered as a pre-push hook. I've enabled several useful checks/fixes: trailing whitespace, trailing newlines, line encoding, validity of json/yaml/toml files, lint that finds files broken by merge conflicts, and a few others. I've also added settings for VSCode to fix trailing whitespaces and newlines on file save. # Motivation Some contributors have their editors configured to trim trailing whitespaces and newlines on save, while others don't. This results in spurious changes because somebody's editor added or removed a newline at the end of file. Hopefully this change will make them less prevalent, if at all.
1 parent 0250a57 commit a89808b

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

.github/workflows/test.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,17 @@ jobs:
2525
uses: crate-ci/typos@master
2626

2727
reformat:
28-
name: Rustfmt
28+
name: Code Style Check
2929
runs-on: ubuntu-latest
3030
steps:
3131
- name: Checkout
3232
uses: actions/checkout@v4
3333
- name: Install Rust toolchain
3434
uses: dtolnay/rust-toolchain@stable
35-
- name: Run Rustfmt
36-
run: cargo fmt --all -- --check
35+
- name: Install pre-commit
36+
run: pip install pre-commit
37+
- name: Run style check
38+
run: pre-commit run --all
3739

3840
test:
3941
runs-on: ubuntu-latest

.pre-commit-config.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
repos:
2+
- repo: local
3+
hooks:
4+
- id: fmt
5+
name: fmt
6+
description: run cargo fmt
7+
entry: cargo fmt
8+
language: system
9+
types: [ rust ]
10+
args: [ "--all" ]
11+
pass_filenames: false
12+
- repo: https://github.com/pre-commit/pre-commit-hooks
13+
rev: v6.0.0
14+
hooks:
15+
- id: trailing-whitespace
16+
- id: end-of-file-fixer
17+
- id: fix-byte-order-marker
18+
- id: mixed-line-ending
19+
args: [ --fix=lf ]
20+
- id: check-added-large-files
21+
- id: check-yaml
22+
- id: check-toml
23+
- id: check-json
24+
exclude: ^.vscode/
25+
- id: check-merge-conflict
26+
- id: check-shebang-scripts-are-executable
27+
exclude_types: [ rust ]
28+
- id: check-symlinks
29+
- id: destroyed-symlinks
30+
- id: check-vcs-permalinks

.vscode/settings.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1-
{}
1+
{
2+
"files.eol": "\n",
3+
"files.insertFinalNewline": true,
4+
"files.trimFinalNewlines": true,
5+
"files.trimTrailingWhitespace": true,
6+
"files.trimTrailingWhitespaceInRegexAndStrings": true
7+
}

0 commit comments

Comments
 (0)