tests: check: ast-grep: add static analysis rules#545
Conversation
|
@claude review |
Claude review of PR #545 (6c8bf90)Suggestions
Nits
|
| sed \ | ||
| xxd && \ | ||
| dnf clean all -y | ||
|
|
There was a problem hiding this comment.
Claude: suggestion: ast-grep-cli is installed without a version pin (pipx install --global ast-grep-cli). A breaking change in ast-grep's rule syntax or --off flag behavior could cause CI failures across all branches simultaneously. Consider pinning to a specific version (e.g. pipx install --global ast-grep-cli==0.38.0) and updating periodically via a dedicated PR. Same applies to all other Dockerfiles (Fedora via pipx, Ubuntu via pip).
There was a problem hiding this comment.
On the other hand, it'd be nice to see failures introduced in new version immediately, not "whenever we remember to bump hardcoded versions". I think fetching the latest version should be fine.
| kind: return_statement | ||
| has: | ||
| kind: identifier | ||
| regex: "^E[A-Z0-9]{2,}$" |
There was a problem hiding this comment.
Claude: nit: The rule uses has: without stopBy: end, so return (ENOMEM); (parenthesized) would escape detection because the direct child of return_statement is parenthesized_expression, not identifier. This doesn't occur in the current codebase but is a latent gap. Adding stopBy: end to the has: clause would make it robust against this pattern.
There was a problem hiding this comment.
This is very niche case, ignoring.
0677b44 to
3aa5c03
Compare
3aa5c03 to
0b9abfb
Compare
qdeslandes
left a comment
There was a problem hiding this comment.
Quick question, otherwise LGTM.
| xxd && \ | ||
| dnf clean all -y | ||
|
|
||
| RUN pipx install --global ast-grep-cli && \ |
There was a problem hiding this comment.
We already use pip for Ubuntu containers, why not using it too here?
|
Hi @pzmarzly! Thank you for your pull request. We require contributors to sign our Contributor License Agreement, and yours needs attention. You currently have a record in our system, but the CLA is no longer valid, and will need to be resubmitted. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
Pawel's contribution to this PR were made during his employment at Meta. While he would have to sign the CLA for new contribution, this specific one doesn't require it. |
If https://ast-grep.github.io/ is installed (e.g. via
pip install ast-grep-cli), we can use it to enforce many of our coding standards. See #510. Previous attempt in #502.Each rule is added in separate commit, so it's best to review this PR commit-by-commit.
Rules with existing violations are added as disabled. We should reenable them one by one later. Each reenablement will create a large PR, that may create merge conflicts for everyone.
Test
ast-grep scan --config tests/check/ast-grep/sgconfig.yml src/bfcli/ src/libbpfilter/https://gist.github.com/pzmarzly/7dfba8ca061a942a271b14c6320ea13b
ast-grep scan --config tests/check/ast-grep/sgconfig.yml src/bfcli/ src/libbpfilter/ --off=assert-pointer-params --off=doxygen-prefer-backticks --off=doxygen-public-functions --off=no-direct-free --off=single-line-comment-style(current invocation inCMakeLists.txt):[empty result, i.e. all OK]
List of rules
assert()them at entry.bf_/bfc_prefix (or_bf_/_bfc_when static).bf_*_new()must take aTYPE **output parameter for ownership transfer.`name`over@p name,@ref name,@c namein Doxygen comments./** @brief ... */comment.bf_/bfc_prefix._BF_*_MAX/_BFC_*_MAXsentinel (with listed exceptions for bitmasks and kernel ABI mirrors)._cleanup_close_must be initialized to-1to avoid closing fd 0 on error paths.bf_*_free()/_bf_*_free()must returnvoidand accept a double-pointer parameter.-ENOMEM, notENOMEM).free(); usefreep()or a typedbf_*_free()cleanup function instead (with exempt callers).bf_err,bf_warn, …) instead offprintf(stderr, …)/vfprintf(stderr, …).#pragma once, not#ifndef/#defineinclude guards.//, reserving/* */for multi-line (SPDX excluded).SPDX-License-Identifiercomment.bf_/bfc_prefix; external type references are ignored.