From ffaa19459e3ffbe794e20f36bca92f20d73fad7f Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 1 Apr 2025 21:10:06 +0200 Subject: [PATCH] pre-commit: Add pre-commit-hooks and auto-walrus --- .pre-commit-config.yaml | 39 +++++++++++++++++++++++++++++++++++++++ cpplint.py | 27 +++++++++------------------ cpplint_unittest.py | 3 +-- 3 files changed, 49 insertions(+), 20 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index ac664642..c5553dcd 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -5,6 +5,45 @@ # Then in the project root directory run `pre-commit install` repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v5.0.0 + hooks: + - id: check-added-large-files + - id: check-ast + - id: check-builtin-literals + - id: check-case-conflict + - id: check-docstring-first + - id: check-executables-have-shebangs + - id: check-json + - id: check-merge-conflict + - id: check-shebang-scripts-are-executable + - id: check-symlinks + - id: check-toml + - id: check-vcs-permalinks + - id: check-xml + - id: check-yaml + - id: debug-statements + - id: destroyed-symlinks + - id: detect-private-key + # - id: end-of-file-fixer # TODO(cclauss): Causes some tests to fail. + - id: file-contents-sorter + - id: fix-byte-order-marker + - id: forbid-new-submodules + - id: forbid-submodules + # - id: mixed-line-ending # TODO(cclauss): Causes some tests to fail. + # args: + # - --fix=lf + - id: name-tests-test + - id: pretty-format-json + - id: requirements-txt-fixer + - id: sort-simple-yaml + # - id: trailing-whitespace # TODO(cclauss): Causes some tests to fail. + + - repo: https://github.com/MarcoGorelli/auto-walrus + rev: 0.3.4 + hooks: + - id: auto-walrus + - repo: https://github.com/codespell-project/codespell rev: v2.4.1 hooks: diff --git a/cpplint.py b/cpplint.py index 9a032f80..6e2cf5d2 100755 --- a/cpplint.py +++ b/cpplint.py @@ -1103,8 +1103,7 @@ def ParseNolintSuppressions(filename, raw_line, linenum, error): linenum: int, the number of the current line. error: function, an error handler. """ - matched = re.search(r"\bNOLINT(NEXTLINE|BEGIN|END)?\b(\([^)]+\))?", raw_line) - if matched: + if matched := re.search(r"\bNOLINT(NEXTLINE|BEGIN|END)?\b(\([^)]+\))?", raw_line): no_lint_type = matched.group(1) if no_lint_type == "NEXTLINE": @@ -2460,8 +2459,7 @@ def GetIndentLevel(line): Returns: An integer count of leading spaces, possibly zero. """ - indent = re.match(r"^( *)\S", line) - if indent: + if indent := re.match(r"^( *)\S", line): return len(indent.group(1)) return 0 @@ -3935,8 +3933,7 @@ def CheckForFunctionLengths(filename, clean_lines, linenum, function_state, erro starting_func = False regexp = r"(\w(\w|::|\*|\&|\s)*)\(" # decls * & space::name( ... - match_result = re.match(regexp, line) - if match_result: + if match_result := re.match(regexp, line): # If the name is all caps and underscores, figure it's a macro and # ignore it, unless it's TEST or TEST_F. function_name = match_result.group(1).split()[-1] @@ -4410,8 +4407,7 @@ def _IsType(clean_lines, nesting_state, expr): True, if token looks like a type. """ # Keep only the last token in the expression - last_word = re.match(r"^.*(\b\S+)$", expr) - if last_word: + if last_word := re.match(r"^.*(\b\S+)$", expr): token = last_word.group(1) else: token = expr @@ -4479,9 +4475,8 @@ def CheckBracesSpacing(filename, clean_lines, linenum, nesting_state, error): # And since you should never have braces at the beginning of a line, # this is an easy test. Except that braces used for initialization don't # follow the same rule; we often don't want spaces before those. - match = re.match(r"^(.*[^ ({>]){", line) - if match: + if match := re.match(r"^(.*[^ ({>]){", line): # Try a bit harder to check for brace initialization. This # happens in one of the following forms: # Constructor() : initializer_list_{} { ... } @@ -5017,8 +5012,7 @@ def CheckEmptyBlockBody(filename, clean_lines, linenum, error): # We also check "if" blocks here, since an empty conditional block # is likely an error. line = clean_lines.elided[linenum] - matched = re.match(r"\s*(for|while|if)\s*\(", line) - if matched: + if matched := re.match(r"\s*(for|while|if)\s*\(", line): # Find the end of the conditional expression. (end_line, end_linenum, end_pos) = CloseExpression(clean_lines, linenum, line.find("(")) @@ -5911,8 +5905,7 @@ def CheckLanguage( # convention of the whole function to process multiple line to handle it. # printf( # boy_this_is_a_really_long_variable_that_cannot_fit_on_the_prev_line); - printf_args = _GetTextInside(line, r"(?i)\b(string)?printf\s*\(") - if printf_args: + if printf_args := _GetTextInside(line, r"(?i)\b(string)?printf\s*\("): match = re.match(r"([\w.\->()]+)$", printf_args) if match and match.group(1) != "__VA_ARGS__": function_name = re.search(r"\b((?:string)?printf)\s*\(", line, re.IGNORECASE).group(1) @@ -6858,8 +6851,7 @@ def FilesBelongToSameModule(filename_cc, filename_h): return (False, "") filename_cc = filename_cc[: -(len(fileinfo_cc.Extension()))] - matched_test_suffix = re.search(_TEST_FILE_SUFFIX, fileinfo_cc.BaseName()) - if matched_test_suffix: + if matched_test_suffix := re.search(_TEST_FILE_SUFFIX, fileinfo_cc.BaseName()): filename_cc = filename_cc[: -len(matched_test_suffix.group(1))] filename_cc = filename_cc.replace("/public/", "/") @@ -7067,8 +7059,7 @@ def CheckRedundantOverrideOrFinal(filename, clean_lines, linenum, error): # the declarator ends and where the virt-specifier starts to avoid # false positives. line = clean_lines.elided[linenum] - declarator_end = line.rfind(")") - if declarator_end >= 0: + if (declarator_end := line.rfind(")")) >= 0: fragment = line[declarator_end:] else: if linenum > 1 and clean_lines.elided[linenum - 1].rfind(")") >= 0: diff --git a/cpplint_unittest.py b/cpplint_unittest.py index 241c1318..079c7d36 100755 --- a/cpplint_unittest.py +++ b/cpplint_unittest.py @@ -5727,8 +5727,7 @@ def testRuntimePrintfFormat(self): def TestLintLogCodeOnError(self, code, expected_message): # Special TestLint which logs the input code on error. - result = self.PerformSingleLineLint(code) - assert result == expected_message, ( + assert (result := self.PerformSingleLineLint(code)) == expected_message, ( f'For code: "{code}"\nGot: "{result}"\nExpected: "{expected_message}"' )