Repository navigation
Fix #15244 syntaxError and unsimplified function body when a specifier precedes a trailing return type - #9052
Conversation
|
The three failing jobs ( |
f19f0e1 to
deaedb0
Compare
|
Rebased onto |
dmcppcheck
left a comment
There was a problem hiding this comment.
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
cppcheckandtestrunnerin pr/ andcppcheckin base/. ./testrunner TestTokenizer TestVarID TestSymbolDatabase TestGarbage TestSimplifyTokens: 0 failures. vardecl37 and vardecl38 pass.- I wrote scratch files and ran
cppcheck --debug-normal --std=c++23on 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 -> intas 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)}andint 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.
- All of these now get simplified bodies and variable ids:
- I ran
cppcheck --enable=all --inconclusive --check-level=exhaustivewith 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.
|
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/ 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. |
|
I don't see why the commits are in the same PR. The seem pretty unrelated. |
deaedb0 to
7549fdd
Compare
|
I'm still learning your preferences for how PRs are scoped. I've split them: the fix for Trac #15245 is now in #9087. |
|
understood. sorry for my unfriendly answer. thanks for splitting it up. |
dmcppcheck
left a comment
There was a problem hiding this comment.
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
cppcheckandtestrunnerin pr/ andcppcheckin base/. ./testrunner TestTokenizer TestVarID TestSymbolDatabase TestGarbage: 1283 tests, 0 failures, including the newvardecl37. Full./testrunner -q: 5424 tests, 0 failures.- I ran
cppcheck --debug-normal --std=c++23with 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() -> intnoexcept(noexcept(f())) -> int[]() mutable noexcept -> int[]() static -> inttemplate<class T> auto n(T t) const -> decltype(t)noexcept -> R*
- Now simplified, where base left the body unsimplified:
auto S::operator()() const -> intauto S::v() const & -> std::vector<int>[&](int y) constexpr -> int {...}(3)inside a function body
- Unchanged and still correct:
[]<class T>(T x) noexcept -> intandnoexcept -> 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) constandthrow(1).
- Now simplified and given variable ids, where base gave a syntax error for the whole file:
Automated review of 7549fdd by Claude. The pull request was built and tested locally.
|
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>
7549fdd to
2b7be90
Compare
dmcppcheck
left a comment
There was a problem hiding this comment.
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:
cppcheckandtestrunnerbuilt in pr/, andcppcheckin base/. - Unit tests:
./testrunner TestTokenizer TestVarID TestSymbolDatabase TestGarbagehad 0 failures, includingvardecl37. The full./testrunner -qalso 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() -> intandmutable 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
staticlambda the parameterxgets no variable id. - The structured binding
auto [p, q]{...}gets no variable ids.
- In a
- Bodies now simplified and given variable ids (
- Checks: a member function
auto S::f() const & noexcept -> intwith aconstexprlambda 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.
isTrailingReturnTypenow 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