Skip to content

fix: handle templated lambdas in semicolon check - #454

Open
tusharmalpani20 wants to merge 2 commits into
cpplint:developfrom
tusharmalpani20:fix/templated-lambda-semicolon
Open

fix: handle templated lambdas in semicolon check#454
tusharmalpani20 wants to merge 2 commits into
cpplint:developfrom
tusharmalpani20:fix/templated-lambda-semicolon

Conversation

@tusharmalpani20

@tusharmalpani20 tusharmalpani20 commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Fixes #385.

The trailing-semicolon check now recognizes C++20 templated lambdas,
including multiline template parameter lists and requires-clauses.

The existing warnings for operator[] and ordinary constrained function
definitions remain unchanged.

Tests:

  • pytest — 231 passed, 96.43% coverage
  • pylint cpplint.py
  • pre-commit run --all-files
  • C++20 examples verified with g++ -std=c++20

Summary by CodeRabbit

  • Bug Fixes

    • Fixed false-positive readability warnings for C++20 templated lambdas, including multiline requires clauses.
    • Improved handling of multiline lambda syntax to avoid incorrectly flagging valid code for redundant trailing semicolons.
  • Tests

    • Added coverage for templated lambdas with multiline parameters, bodies, and constraints.
  • Documentation

    • Updated the changelog with details of the readability warning fix.

@coderabbitai

coderabbitai Bot commented Jul 29, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The change updates trailing-semicolon detection for C++20 templated lambdas, including multiline requires clauses. It adds regression tests and records the fix in the changelog.

Changes

Templated Lambda Detection

Layer / File(s) Summary
Lambda matching and trailing-semicolon handling
cpplint.py
New helpers match captures across template and requires clauses. CheckTrailingSemicolon uses them to avoid false positives while preserving operator[] warnings.
Templated lambda regression coverage and changelog
cpplint_unittest.py, CHANGELOG.rst
Adds multiline templated-lambda test cases and documents the corrected braces diagnostic.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant CheckTrailingSemicolon
  participant MatchLambdaCapture
  participant MatchLambdaRequiresClause
  CheckTrailingSemicolon->>MatchLambdaCapture: match lambda capture and template boundary
  MatchLambdaCapture-->>CheckTrailingSemicolon: return capture match
  CheckTrailingSemicolon->>MatchLambdaRequiresClause: inspect multiline requires clause
  MatchLambdaRequiresClause-->>CheckTrailingSemicolon: return preceding capture match
Loading

Possibly related PRs

  • cpplint/cpplint#458: Both changes update CheckTrailingSemicolon and lambda tests for C++20 templated-lambda brace warnings.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the fix for templated lambdas in the trailing-semicolon check.
Linked Issues check ✅ Passed The changes address issue #385 by preventing false positives after C++20 templated lambdas and adding relevant regression tests.
Out of Scope Changes check ✅ Passed The implementation, regression tests, and changelog entry are directly related to the linked issue and PR objectives.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@androvonx95 androvonx95 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicate — see my other review on this PR.

@androvonx95 androvonx95 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed at bce7f59 against current develop.

Fixes the #385 case and the harder valid C++20 forms that a same-line ]<...> check misses — multiline template parameter lists, ]<T> with ( on the next line, and trailing requires-clauses. I compiled those with g++ -std=c++20 -pedantic-errors -fsyntax-only and confirmed they stay clean here while ordinary templated/constrained functions and operator[] (including multiline) still warn. Full unit suite: 206 passed.

This conflicts with #458; I’d take this one — it’s the more complete coverage for the same issue.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

5787-5793: 🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Assert the diagnostic for foo.cc.

Line 5792 counts bar_header_error, but this invocation lints test/foo.cc. The assertion passes even if including foo.hpp incorrectly reports that foo.cc omitted its related header.

Proposed fix
-            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 5787 - 5793, Update the assertion in the
multiple-same-basename test to count the diagnostic associated with foo.cc
rather than bar_header_error, while preserving the expectation that including
foo.hpp produces no related-header error for foo.cc. Use the existing diagnostic
symbol or construction for foo.cc from the surrounding test setup.
🧹 Nitpick comments (1)
cpplint_unittest.py (1)

3169-3201: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add coverage for a trailing lambda requires clause.

The new tests only cover a requires clause before the lambda parameter list. C++20 also permits a clause after the parameter list. A matcher regression in that form will pass this suite.

Proposed test
+        self.TestMultiLineLint(
+            "auto identity = []<typename T>(T&& t)\n"
+            "    requires std::integral<T> &&\n"
+            "             std::copyable<T>\n"
+            "{\n"
+            "  return t;\n"
+            "};\n",
+            "",
+        )

Verify this form with the project C++20 compiler.

🤖 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 3169 - 3201, Add a TestMultiLineLint case
alongside the existing generic lambda requires-clause cases covering a trailing
requires clause after the lambda parameter list, including a multiline
constraint if appropriate. Use valid C++20 syntax and verify the test with the
project’s C++20 compiler, expecting no lint errors.
🤖 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 5787-5793: Update the assertion in the multiple-same-basename test
to count the diagnostic associated with foo.cc rather than bar_header_error,
while preserving the expectation that including foo.hpp produces no
related-header error for foo.cc. Use the existing diagnostic symbol or
construction for foo.cc from the surrounding test setup.

---

Nitpick comments:
In `@cpplint_unittest.py`:
- Around line 3169-3201: Add a TestMultiLineLint case alongside the existing
generic lambda requires-clause cases covering a trailing requires clause after
the lambda parameter list, including a multiline constraint if appropriate. Use
valid C++20 syntax and verify the test with the project’s C++20 compiler,
expecting no lint errors.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 6381b6bc-67e3-421c-ac55-69ec440f368d

📥 Commits

Reviewing files that changed from the base of the PR and between bce7f59 and fee646d.

📒 Files selected for processing (3)
  • CHANGELOG.rst
  • cpplint.py
  • cpplint_unittest.py
🚧 Files skipped from review as they are similar to previous changes (2)
  • CHANGELOG.rst
  • cpplint.py

@tusharmalpani20

Copy link
Copy Markdown
Contributor Author

Thanks for the review! I resolved the conflict with current develop in fee646d, preserving both changelog entries. All 231 tests and repository checks pass locally. Could you please take another look when you have time?

@androvonx95 androvonx95 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rechecked at fee646d. Merge with current develop looks clean — both changelog entries are preserved, and the templated-lambda logic is unchanged from bcf7f59.

Re-ran the earlier cases against this head: #385, multiline template params, ]<T>n(, and requires-clauses stay clean; templated/constrained functions and multiline operator[] still warn. Full suite: 206 passed. Still LGTM.

@yangfan-yf-yf yangfan-yf-yf left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed at fee646d.

The matcher keeps ordinary constrained functions and multiline operator[] declarations in the trailing-semicolon path while recognizing the C++20 templated-lambda forms. I also checked a trailing requires-clause lambda and confirmed it stays clean under the repository's brace-style rule.

Verified locally:

python -m pytest --no-cov -q
231 passed

git diff --check upstream/develop...HEAD
Passed

LGTM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

false positive: semicolon after a templated lambda

3 participants