Skip to content

Fix #15244, #15245 syntaxError and unsimplified function bodies in simplifyVarDecl - #9052

Open
tsobczynski wants to merge 2 commits into
cppcheck-opensource:mainfrom
tsobczynski:fix-trailing-return-specifiers
Open

tsobczynski wants to merge 2 commits into
cppcheck-opensource:mainfrom
tsobczynski:fix-trailing-return-specifiers

Conversation

@tsobczynski

Copy link
Copy Markdown

Two fixes in Tokenizer::simplifyVarDecl, one commit each.

  • #15244: isTrailingReturnType now steps back over the specifiers that can stand between the parameter list and -> (cv and ref qualifiers, noexcept, noexcept(...), throw(...), mutable, constexpr, consteval, static). Before, a function or lambda with one of them had its body treated as a braced initializer, so the body was not simplified. Test: vardecl37.
  • #15245: the K&R parameter pass no longer steps onto tokEnd when it skips a parenthesised group, which made the loop run past the end of its range. Test: vardecl38.

Both tests fail without the fixes. testrunner passes in full. selfcheck.sh, built with ENABLE_CHECK_INTERNAL and USE_MATCHCOMPILER=Verify, reports no new findings.

🤖 Generated with Claude Code

@tsobczynski

Copy link
Copy Markdown
Author

The three failing jobs (build_cmake (macos-15) and build_cmake_cxxstd (windows-2025, 14/17)) fail while CMake is configuring, before anything compiles. Those runners have CMake 4.5.0. That release adds a new developer warning, non-target-directive, for directory-scoped commands such as the add_compile_options calls in cmake/compileroptions.cmake. CI configures with -Werror=dev, which turns that warning into an error. The same jobs pass on main, whose runs got CMake 4.4.5, and #15225 fails in the same way. So this looks like an uneven runner-image rollout rather than something this PR changes.

tsobczynski and others added 2 commits November 22, 2026 09:53
…r precedes a trailing return type

isTrailingReturnType accepted only `) . type`, so a function or lambda
with a cv or ref qualifier, noexcept, noexcept(...), throw(...),
mutable, constexpr, consteval or static before `->` had its body taken
for a braced initializer, and simplifyVarDecl did not simplify it.
Inside such a body, findLambdaEndScope then accepted a structured
binding like `[p, c]{f()}` as a lambda.

Step back over those specifiers and return the parameter list's `)`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… parenthesis

In the K&R parameter pass, simplifyVarDecl skips a parenthesised group
and steps past its `)`. When that `)` is the last token before tokEnd,
the step lands on tokEnd and the loop increment then steps past it, so
the loop runs to the end of the token list and raises a syntax error.
findLambdaEndScope takes `[2]{(1)}` in `new int[2]{(1)}` for a lambda,
which produces such a range.

Don't step onto tokEnd.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tsobczynski
tsobczynski force-pushed the fix-trailing-return-specifiers branch from f19f0e1 to deaedb0 Compare November 22, 2026 17:29
@tsobczynski

Copy link
Copy Markdown
Author

Rebased onto main to pick up #9047's CMake 4.5 fix, which should clear the three configure failures.

@dmcppcheck dmcppcheck left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

AI review

I found no problems in this PR. isTrailingReturnType now steps back over cv and ref qualifiers, noexcept/noexcept(...), throw(...), mutable, constexpr, consteval and static. With that, function and lambda bodies are simplified correctly, and the structured binding case auto [p, c]{f()} is no longer mistaken for a lambda. The K&R fix stops the loop from stepping past tokEnd, and new int[2]{(1)} no longer causes a syntaxError. A lambda with no parameter list, such as [] mutable -> int {}, is still not simplified, but that was already the case before and has a TODO in the code. Running base and PR on several test/cfg files and samples gave the same results.

Testing

  • I built cppcheck and testrunner in pr/ and cppcheck in base/.
  • ./testrunner TestTokenizer TestVarID TestSymbolDatabase TestGarbage TestSimplifyTokens: 0 failures. vardecl37 and vardecl38 pass.
  • I wrote scratch files and ran cppcheck --debug-normal --std=c++23 on them with both builds:
    • All of these now get simplified bodies and variable ids: () const& -> int, throw() -> int, noexcept(noexcept(f())) -> int, []() constexpr|static|consteval -> int, []() mutable noexcept -> R { auto [p, c]{f()}; ... }, const & noexcept -> int as a member function, and -> decltype(...) / -> std::vector<int>. With base, the file got a syntax error, so it had no variable ids at all.
    • new int[2]{(1)}, new int[2]{(1),(2)} and int x{[](){return 1;}()} inside a function body are now handled with no syntax error.
    • Code that could have given a false match on the new specifier skipping gives the same output in base and PR: C designated initializers such as { .a = (x) & 1, .b = { 2 } }, and a K&R function with casts and parentheses.
    • [] mutable -> int { int a = 1, b = 2; ... } (no parameter list) is still not simplified. This was already the case before the PR and has a TODO in the code.
  • I ran cppcheck --enable=all --inconclusive --check-level=exhaustive with both builds on test/cfg/std.cpp, qt.cpp, boost.cpp, std.c and samples/*. The output was identical.

Automated review of deaedb0 by Claude. The pull request was built and tested locally.

@dmcppcheck

Copy link
Copy Markdown
Contributor

Test results for commit deaedb0 (tools/test-my-pr.py, main compared to this PR):

Test: http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9052/
packages: 1000
Differing warnings: 5 (+0 -5)
AI review: 5 improvement (http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9052/2026-11-22_14-53-19-deaedb0215c6-review.txt)
Timing: -0.2% (http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9052/2026-11-22_14-53-19-deaedb0215c6_timing.html)

Posted automatically by the cppcheck PR test runner. +N: warnings only with this PR, -N: warnings only with main. The AI review is written by Claude and can be wrong.

@danmar

danmar commented Nov 28, 2026

Copy link
Copy Markdown
Collaborator

I don't see why the commits are in the same PR. The seem pretty unrelated.

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.

3 participants