fix(readability/casting): ignore calling-convention function pointers - #448
fix(readability/casting): ignore calling-convention function pointers#448androvonx95 wants to merge 4 commits into
Conversation
Treat typedef/using forms like (CALLCONV* name)(args) as function pointer types instead of deprecated or C-style casts (cpplint#409). Co-authored-by: Cursor <cursoragent@cursor.com>
📝 WalkthroughWalkthroughThe casting checks now avoid reporting function-pointer typedef and alias declarations with calling-convention macros before ChangesCasting false-positive fix
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
cpplint.py (2)
6689-6689: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick winConsider supporting arrays of function pointers.
Using
\w*for the function pointer name restricts it to word characters. This means the regex will fail to match arrays of function pointers, such as(CALLCONV* FuncArray[3])(void), because\w*does not match the[and]characters.To seamlessly handle these valid cases while avoiding spaces and parentheses, consider replacing
\w*with[^() ]*. This mirrors the logic used in the adjacent pointer-to-member regex (line 6688).🛠 Proposed fix
- or re.match(r"\((?:\w+\s*)?\*\s*\w*\)\s*\(", matched_funcptr) + or re.match(r"\((?:\w+\s*)?\*\s*[^() ]*\)\s*\(", matched_funcptr)🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@cpplint.py` at line 6689, Update the function-pointer regex alternative near the adjacent pointer-to-member pattern to allow array declarators in names such as FuncArray[3]. Replace the restrictive word-character name match with a character class that excludes parentheses and spaces, preserving the existing surrounding function-pointer matching behavior.
6831-6836: 🎯 Functional Correctness | 🔵 Trivial | 💤 Low valueConsider edge cases like function parameters and trailing return types.
The
typedefandusingcheck perfectly addresses the alias cases mentioned in the issue. However, be aware that this same false positive can still occur if the function-pointer type is used directly as a function parameter or trailing return type, for example:
void Register(int32_t(CALLCONV *)(void));Since
(CALLCONV *)is followed by(void), it will still be flagged as a C-style cast here because the line lackstypedeforusing.While fully addressing this might be out of scope for this specific PR, it's worth keeping in mind or adding a TODO for future improvement.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@cpplint.py` around lines 6831 - 6836, Extend the false-positive guard near the existing typedef/using check to recognize function-pointer type syntax used directly in function parameters or trailing return types, including patterns such as `(CALLCONV *)` followed by `(void)`. Preserve the current alias handling and add a focused TODO if complete support is out of scope.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@cpplint.py`:
- Line 6689: Update the function-pointer regex alternative near the adjacent
pointer-to-member pattern to allow array declarators in names such as
FuncArray[3]. Replace the restrictive word-character name match with a character
class that excludes parentheses and spaces, preserving the existing surrounding
function-pointer matching behavior.
- Around line 6831-6836: Extend the false-positive guard near the existing
typedef/using check to recognize function-pointer type syntax used directly in
function parameters or trailing return types, including patterns such as
`(CALLCONV *)` followed by `(void)`. Preserve the current alias handling and add
a focused TODO if complete support is out of scope.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: c4f26bba-62e4-47aa-828a-ae55db35b0dd
📒 Files selected for processing (3)
CHANGELOG.rstcpplint.pycpplint_unittest.py
There was a problem hiding this comment.
One declaration form in the stated scope is still flagged:
typedef int32_t(CALLCONV* FuncArray[3])(void);
On the current head it emits Using deprecated casting style. Use static_cast<int32_t>(...) instead. The new function-pointer alternative uses \w* for the declarator name, so it stops before [3]; the adjacent member-function-pointer pattern already accepts array declarators. Please broaden that declarator portion and add a regression case for this typedef form.
|
Below: This branch has conflicts that must be resolved |
…f/using Broaden the declarator part of the function-pointer pattern so that array declarators such as `typedef int32_t(CALLCONV* FuncArray[3])(void);` are recognized instead of being reported as a deprecated cast.
|
I'm going to see if I can port changelog-addition to towncrier. |
|
For slow moving repos, Towncrier always seems to be more process complexity than rebasing but do what you think is best. |
|
I also reran the relevant tests plus the full suite, and everything passes. I resolved the |
FTR: A rebase isn't needed; we have a workflow that automatically adds a changenote to dependabot PRs, but in the case of the PR you've tagged, it was misconfigured (missing a required permission) as part of a recent spring cleaning in that project. |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
cpplint_unittest.py (1)
5761-5766: 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick winAssert the result of the
foo.cccase.The test reuses
error_collectorfrom the previousBar.ccchecks and assertsbar_header_error. A new error from thefoo.cc/foo.hppcase cannot fail this assertion. Reseterror_collectorbeforeProcessFileDataand assertfoo_header_errorfor this case.Proposed fix
+ error_collector = ErrorCollector(self.assertTrue) cpplint.ProcessFileData( "test/foo.cc", "cc", [r'`#include` "foo.hpp"', ""], error_collector, ) - assert error_collector.Results().count(bar_header_error) == 0 + assert error_collector.Results().count(foo_header_error) == 0🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@cpplint_unittest.py` around lines 5761 - 5766, Update the foo.cc test case around ProcessFileData to reset error_collector before processing, then assert the collected results against foo_header_error rather than bar_header_error, preserving the existing Bar.cc assertions.
🧹 Nitpick comments (1)
cpplint_unittest.py (1)
1119-1133: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick winAdd a positive control for the new suppression.
These tests verify only that function-pointer declarations produce no warning. Add a nearby case that must still report a real cast with similar tokens. This will detect overmatching in the new
typedefandusingsuppression.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@cpplint_unittest.py` around lines 1119 - 1133, Add a nearby positive-control test in the relevant cpplint unittest alongside the existing function-pointer cases, using similar calling-convention/function-pointer tokens but an actual C-style cast that must produce the expected warning. Keep the current no-warning assertions unchanged and use TestLint’s expected-output argument to verify the suppression does not overmatch.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Outside diff comments:
In `@cpplint_unittest.py`:
- Around line 5761-5766: Update the foo.cc test case around ProcessFileData to
reset error_collector before processing, then assert the collected results
against foo_header_error rather than bar_header_error, preserving the existing
Bar.cc assertions.
---
Nitpick comments:
In `@cpplint_unittest.py`:
- Around line 1119-1133: Add a nearby positive-control test in the relevant
cpplint unittest alongside the existing function-pointer cases, using similar
calling-convention/function-pointer tokens but an actual C-style cast that must
produce the expected warning. Keep the current no-warning assertions unchanged
and use TestLint’s expected-output argument to verify the suppression does not
overmatch.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: 682e69f1-af3c-4a0c-90ba-f742bc819288
📒 Files selected for processing (3)
CHANGELOG.rstcpplint.pycpplint_unittest.py
🚧 Files skipped from review as they are similar to previous changes (2)
- CHANGELOG.rst
- cpplint.py
yangfan-yf-yf
left a comment
There was a problem hiding this comment.
Rechecked at 62c0094.
The function-pointer declarator pattern now accepts array declarators, so the previously reported typedef int32_t(CALLCONV* FuncArray[3])(void); case is clean. I also verified a using alias with the calling-convention form stays clean while an ordinary C-style cast still reports readability/casting.
Verified locally:
python -m pytest --no-cov cpplint_unittest.py -k 'testDeprecatedCast or testCStyleCast' -q
2 passed
python -m pytest --no-cov -q
231 passed
git diff --check upstream/develop...HEAD
Passed
LGTM.
PNHD
left a comment
There was a problem hiding this comment.
Re-reviewed current head 62c0094. The previously reported array-declarator case is fixed, the calling-convention typedef/alias cases are covered, and ordinary C-style casts remain diagnosed. I found no remaining blocker on this head. LGTM.
Summary
readability/castingfor function-pointer typedefs and alias declarations that use a calling-convention macro before*(e.g. Node.jsNAPI_CDECL).CheckCastsand skips lookalike pointer casts intypedef/usinglines insideCheckCStyleCast.Fixes #409
Test plan
pytest --no-cov cpplint_unittest.py -k 'testDeprecatedCast or testCStyleCast'pytest --no-cov cpplint_unittest.py(205 passed locally)Summary by CodeRabbit
readability/castingwarnings for function-pointer typedef and alias declarations using calling-convention macros, including array declarators.