Skip to content

Run check_syntax as pre-commit hook#3310

Merged
Lestropie merged 3 commits into
devfrom
check_syntax_precommit_hook
Apr 18, 2026
Merged

Run check_syntax as pre-commit hook#3310
Lestropie merged 3 commits into
devfrom
check_syntax_precommit_hook

Conversation

@Lestropie
Copy link
Copy Markdown
Member

@Lestropie Lestropie commented Apr 17, 2026

Felt the itch to give this a try, and Claude seems to have done a functional job of it.

I tried adding a second commit that contained code that would violate check_syntax. The pre-commit hook rejected it. It was clear from the response time that check_syntax was only executing against the git diff and not the entire codebase. So might be a useful one to drag in just to cut back on pluralistic commits.

Closes #3301.

Prompt: "Modify script "check_syntax" to optionally take as input the currently staged git diff. When this is present, execute the same test of tests as are otherwise executed on all C++ source files upon the set of line additions present in the diff. Then configure a new git pre-commit hook to execute "check_syntax" upon the staged diff."

Generated-by: Claude Sonnet 4.6 <noreply@anthropic.com>.
@github-actions

This comment was marked as outdated.

@Lestropie
Copy link
Copy Markdown
Member Author

Lestropie commented Apr 18, 2026

There is a mild risk in this approach:

check_syntax performs many of its checks based on a modified text file where newlines have been replaced by whitespace, so the code per file is just one long line. This is to catch erroneous usage where the offending code crosses one or more newlines.

In this "diff" mode however, the input will be a concatenation of all newly added lines.
This means that:

  • If the interaction between a new line if code and the surrounding code violates a rule, it won't be caught, ie. false negative.
  • If two lines of code that are separated, but sequential once only newly added lines are considered, manage to combine together to violate a rule, that would be a false positive.

I think these are decreasingly likely to occur, but it's imperfect nonetheless.

Possible strategies:

  • Have check_syntax analyse not only any added lines, but also however many surrounding lines are typically included in a git diff.
  • Have check_syntax introduce some kind of "barrier" in the code it generates from the git diff, so that the regexes can't get any hits based on merging added lines and skipping existing lines between them.

Prompts:
"In script "check_syntax", when operating in diff mode, ensure that what is passed to the grep checks is not only newly added lines, but also the surrounding context lines."
"In script "check_syntax", when operating in diff mode, where there is a discontinuity in the lines of the originating file shown, introduce a delimiter into the file that will prevent any of the regular expression tests from matching across that delimiter."

Generated-by: Claude Opus 4.6 <noreply@anthropic.com>
@github-actions

This comment was marked as outdated.

@github-actions
Copy link
Copy Markdown

clang-tidy review says "All clean, LGTM! 👍"

@Lestropie Lestropie merged commit 86a181f into dev Apr 18, 2026
6 checks passed
@Lestropie Lestropie deleted the check_syntax_precommit_hook branch April 18, 2026 05:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant