Skip to content

Commit d520020

Browse files
authored
pre-commit: Add pre-commit-hooks and auto-walrus (#351)
https://github.com/pre-commit/pre-commit-hooks https://github.com/MarcoGorelli/auto-walrus The three TODOs can be done in follow-on pull requests.
1 parent b66973c commit d520020

File tree

3 files changed

+49
-20
lines changed

3 files changed

+49
-20
lines changed

.pre-commit-config.yaml

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,45 @@
55
# Then in the project root directory run `pre-commit install`
66

77
repos:
8+
- repo: https://github.com/pre-commit/pre-commit-hooks
9+
rev: v5.0.0
10+
hooks:
11+
- id: check-added-large-files
12+
- id: check-ast
13+
- id: check-builtin-literals
14+
- id: check-case-conflict
15+
- id: check-docstring-first
16+
- id: check-executables-have-shebangs
17+
- id: check-json
18+
- id: check-merge-conflict
19+
- id: check-shebang-scripts-are-executable
20+
- id: check-symlinks
21+
- id: check-toml
22+
- id: check-vcs-permalinks
23+
- id: check-xml
24+
- id: check-yaml
25+
- id: debug-statements
26+
- id: destroyed-symlinks
27+
- id: detect-private-key
28+
# - id: end-of-file-fixer # TODO(cclauss): Causes some tests to fail.
29+
- id: file-contents-sorter
30+
- id: fix-byte-order-marker
31+
- id: forbid-new-submodules
32+
- id: forbid-submodules
33+
# - id: mixed-line-ending # TODO(cclauss): Causes some tests to fail.
34+
# args:
35+
# - --fix=lf
36+
- id: name-tests-test
37+
- id: pretty-format-json
38+
- id: requirements-txt-fixer
39+
- id: sort-simple-yaml
40+
# - id: trailing-whitespace # TODO(cclauss): Causes some tests to fail.
41+
42+
- repo: https://github.com/MarcoGorelli/auto-walrus
43+
rev: 0.3.4
44+
hooks:
45+
- id: auto-walrus
46+
847
- repo: https://github.com/codespell-project/codespell
948
rev: v2.4.1
1049
hooks:

cpplint.py

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,8 +1103,7 @@ def ParseNolintSuppressions(filename, raw_line, linenum, error):
11031103
linenum: int, the number of the current line.
11041104
error: function, an error handler.
11051105
"""
1106-
matched = re.search(r"\bNOLINT(NEXTLINE|BEGIN|END)?\b(\([^)]+\))?", raw_line)
1107-
if matched:
1106+
if matched := re.search(r"\bNOLINT(NEXTLINE|BEGIN|END)?\b(\([^)]+\))?", raw_line):
11081107
no_lint_type = matched.group(1)
11091108
if no_lint_type == "NEXTLINE":
11101109

@@ -2460,8 +2459,7 @@ def GetIndentLevel(line):
24602459
Returns:
24612460
An integer count of leading spaces, possibly zero.
24622461
"""
2463-
indent = re.match(r"^( *)\S", line)
2464-
if indent:
2462+
if indent := re.match(r"^( *)\S", line):
24652463
return len(indent.group(1))
24662464
return 0
24672465

@@ -3935,8 +3933,7 @@ def CheckForFunctionLengths(filename, clean_lines, linenum, function_state, erro
39353933

39363934
starting_func = False
39373935
regexp = r"(\w(\w|::|\*|\&|\s)*)\(" # decls * & space::name( ...
3938-
match_result = re.match(regexp, line)
3939-
if match_result:
3936+
if match_result := re.match(regexp, line):
39403937
# If the name is all caps and underscores, figure it's a macro and
39413938
# ignore it, unless it's TEST or TEST_F.
39423939
function_name = match_result.group(1).split()[-1]
@@ -4410,8 +4407,7 @@ def _IsType(clean_lines, nesting_state, expr):
44104407
True, if token looks like a type.
44114408
"""
44124409
# Keep only the last token in the expression
4413-
last_word = re.match(r"^.*(\b\S+)$", expr)
4414-
if last_word:
4410+
if last_word := re.match(r"^.*(\b\S+)$", expr):
44154411
token = last_word.group(1)
44164412
else:
44174413
token = expr
@@ -4479,9 +4475,8 @@ def CheckBracesSpacing(filename, clean_lines, linenum, nesting_state, error):
44794475
# And since you should never have braces at the beginning of a line,
44804476
# this is an easy test. Except that braces used for initialization don't
44814477
# follow the same rule; we often don't want spaces before those.
4482-
match = re.match(r"^(.*[^ ({>]){", line)
44834478

4484-
if match:
4479+
if match := re.match(r"^(.*[^ ({>]){", line):
44854480
# Try a bit harder to check for brace initialization. This
44864481
# happens in one of the following forms:
44874482
# Constructor() : initializer_list_{} { ... }
@@ -5017,8 +5012,7 @@ def CheckEmptyBlockBody(filename, clean_lines, linenum, error):
50175012
# We also check "if" blocks here, since an empty conditional block
50185013
# is likely an error.
50195014
line = clean_lines.elided[linenum]
5020-
matched = re.match(r"\s*(for|while|if)\s*\(", line)
5021-
if matched:
5015+
if matched := re.match(r"\s*(for|while|if)\s*\(", line):
50225016
# Find the end of the conditional expression.
50235017
(end_line, end_linenum, end_pos) = CloseExpression(clean_lines, linenum, line.find("("))
50245018

@@ -5911,8 +5905,7 @@ def CheckLanguage(
59115905
# convention of the whole function to process multiple line to handle it.
59125906
# printf(
59135907
# boy_this_is_a_really_long_variable_that_cannot_fit_on_the_prev_line);
5914-
printf_args = _GetTextInside(line, r"(?i)\b(string)?printf\s*\(")
5915-
if printf_args:
5908+
if printf_args := _GetTextInside(line, r"(?i)\b(string)?printf\s*\("):
59165909
match = re.match(r"([\w.\->()]+)$", printf_args)
59175910
if match and match.group(1) != "__VA_ARGS__":
59185911
function_name = re.search(r"\b((?:string)?printf)\s*\(", line, re.IGNORECASE).group(1)
@@ -6858,8 +6851,7 @@ def FilesBelongToSameModule(filename_cc, filename_h):
68586851
return (False, "")
68596852

68606853
filename_cc = filename_cc[: -(len(fileinfo_cc.Extension()))]
6861-
matched_test_suffix = re.search(_TEST_FILE_SUFFIX, fileinfo_cc.BaseName())
6862-
if matched_test_suffix:
6854+
if matched_test_suffix := re.search(_TEST_FILE_SUFFIX, fileinfo_cc.BaseName()):
68636855
filename_cc = filename_cc[: -len(matched_test_suffix.group(1))]
68646856

68656857
filename_cc = filename_cc.replace("/public/", "/")
@@ -7067,8 +7059,7 @@ def CheckRedundantOverrideOrFinal(filename, clean_lines, linenum, error):
70677059
# the declarator ends and where the virt-specifier starts to avoid
70687060
# false positives.
70697061
line = clean_lines.elided[linenum]
7070-
declarator_end = line.rfind(")")
7071-
if declarator_end >= 0:
7062+
if (declarator_end := line.rfind(")")) >= 0:
70727063
fragment = line[declarator_end:]
70737064
else:
70747065
if linenum > 1 and clean_lines.elided[linenum - 1].rfind(")") >= 0:

cpplint_unittest.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5727,8 +5727,7 @@ def testRuntimePrintfFormat(self):
57275727

57285728
def TestLintLogCodeOnError(self, code, expected_message):
57295729
# Special TestLint which logs the input code on error.
5730-
result = self.PerformSingleLineLint(code)
5731-
assert result == expected_message, (
5730+
assert (result := self.PerformSingleLineLint(code)) == expected_message, (
57325731
f'For code: "{code}"\nGot: "{result}"\nExpected: "{expected_message}"'
57335732
)
57345733

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy