Skip to content

Fix #15244 syntaxError and unsimplified function body when a specifier precedes a trailing return type - #9052

Merged
danmar merged 1 commit into
cppcheck-opensource:mainfrom
tsobczynski:fix-trailing-return-specifiers
Nov 29, 2026
Merged

danmar merged 1 commit into
cppcheck-opensource:mainfrom
tsobczynski:fix-trailing-return-specifiers

Conversation

@tsobczynski

@tsobczynski tsobczynski commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

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.

🤖 Generated with Claude Code

@tsobczynski

Copy link
Copy Markdown
Contributor 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
tsobczynski force-pushed the fix-trailing-return-specifiers branch from f19f0e1 to deaedb0 Compare November 22, 2026 17:29
@tsobczynski

Copy link
Copy Markdown
Contributor 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.

@tsobczynski
tsobczynski force-pushed the fix-trailing-return-specifiers branch from deaedb0 to 7549fdd Compare November 28, 2026 17:35
@tsobczynski tsobczynski changed the title Fix #15244, #15245 syntaxError and unsimplified function bodies in simplifyVarDecl Fix #15244 syntaxError and unsimplified function body when a specifier precedes a trailing return type Nov 28, 2026
@tsobczynski

tsobczynski commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor Author

I'm still learning your preferences for how PRs are scoped. I've split them: the fix for Trac #15245 is now in #9087.

@danmar

danmar commented Nov 28, 2026

Copy link
Copy Markdown
Collaborator

understood. sorry for my unfriendly answer. thanks for splitting it up.

@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 new problems. isTrailingReturnType now steps back over cv/ref qualifiers, noexcept, noexcept(...), throw(...), mutable, constexpr, consteval and static and returns the ) of the parameter list. With that, function and lambda bodies get simplified correctly, and the syntax error with auto [p, c]{f()} inside such a body is gone. I could not find an expression that the new skipping wrongly takes for a trailing return type. The scope concern from danmar was dealt with: the unrelated commit is now in a separate PR.

Comments

# Comment By Where Status
1 The PR's commits are unrelated and should not be in the same PR @danmar general ✅ Fixed

Testing

  • I built cppcheck and testrunner in pr/ and cppcheck in base/.
  • ./testrunner TestTokenizer TestVarID TestSymbolDatabase TestGarbage: 1283 tests, 0 failures, including the new vardecl37. Full ./testrunner -q: 5424 tests, 0 failures.
  • I ran cppcheck --debug-normal --std=c++23 with both builds on scratch files:
    • Now simplified and given variable ids, where base gave a syntax error for the whole file:
      • auto g() noexcept -> int { auto [p, c]{f()}; ... }
      • throw() -> int
      • noexcept(noexcept(f())) -> int
      • []() mutable noexcept -> int
      • []() static -> int
      • template<class T> auto n(T t) const -> decltype(t)
      • noexcept -> R*
    • Now simplified, where base left the body unsimplified:
      • auto S::operator()() const -> int
      • auto S::v() const & -> std::vector<int>
      • [&](int y) constexpr -> int {...}(3) inside a function body
    • Unchanged and still correct: []<class T>(T x) noexcept -> int and noexcept -> int requires true { ... }.
    • Expressions that could look like the new pattern give identical output in base and PR: (x) & p->m, (x) && p->m, (x) const and throw(1).

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

danmar
danmar previously approved these changes Nov 29, 2026
@dmcppcheck

Copy link
Copy Markdown
Contributor

This pull request has merge conflicts that must be resolved.

…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>

@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 new problems. With the change, isTrailingReturnType also recognises functions and lambdas that have cv/ref qualifiers, noexcept/noexcept(...), throw(...), mutable, constexpr, consteval or static before ->, and their bodies now get simplified. I could not find valid code that the new skipping wrongly takes for a trailing return type. In my tests the PR either improved the output or left it the same as base; nothing got worse. The only earlier request, to split out the unrelated commit, has been done.

Comments

# Comment By Where Status
1 The PR bundles unrelated commits @danmar general ✅ Fixed

Testing

  • Build: cppcheck and testrunner built in pr/, and cppcheck in base/.
  • Unit tests: ./testrunner TestTokenizer TestVarID TestSymbolDatabase TestGarbage had 0 failures, including vardecl37. The full ./testrunner -q also had 0 failures.
  • Scratch files, --debug-normal --std=c++23, PR compared with base:
    • Bodies now simplified and given variable ids (int a = 1, b = 2; is split into two declarations):
      • auto operator[](int i) const -> int {...}
      • auto operator()() const & -> int {...}
      • [](int x) static -> int {...}
    • Unchanged and correct: the lambdas with throw() -> int and mutable noexcept(noexcept(x)) -> int.
    • Unchanged: the C++20 designated initializer D s{ .a = 1, .b{2} }; gives the same tokens in base and PR.
    • Gaps that are also in base, not caused by the PR:
      • In a static lambda the parameter x gets no variable id.
      • The structured binding auto [p, q]{...} gets no variable ids.
  • Checks: a member function auto S::f() const & noexcept -> int with a constexpr lambda inside, run with --enable=all. Base and PR give identical warnings (nullPointer, uninitvar and others).

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

@danmar
danmar merged commit 1aea10f into cppcheck-opensource:main Nov 29, 2026
74 checks passed
@tsobczynski
tsobczynski deleted the fix-trailing-return-specifiers branch November 29, 2026 18:14
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