Repository navigation
Fix #12551: false positive: missingReturn with switch-like constexpr if-chain in template - #9098
ludviggunne wants to merge 2 commits into
Conversation
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
The fix works for the case in #12551, and the whole testrunner passes. I found one new problem, a false negative. isExhaustiveIfChain only rejects the compared expression when it has a value type whose scope differs from the enum's. An expression without a value type is accepted. That covers an uninstantiated template<int t> parameter and variables of unknown type, so missingReturn warnings that base reported are lost. A smaller point: the new eElse handling (an else branch is unreachable when the if condition is known true) changes results, for example it removes the warning for const int x = 1; if (x) {return 1;} else {}, but no test covers it.
Comments
| # | Comment | By | Where | Status |
|---|---|---|---|---|
| 1 | Expressions without a valueType (int template param, unknown type) are accepted, losing true missingReturn warnings | AI review | lib/astutils.cpp:3892 |
🆕 New (false negative) |
| 2 | New else-unreachable logic has no test coverage | AI review | lib/checkfunctions.cpp:419 |
🆕 New |
@ludviggunne, please fix, resolve or reply to the open and new comments: #1, #2.
For #1 (false negative), it is enough to create a Trac ticket and link it here. It does not need to be fixed in this PR.
Testing
I built cppcheck and testrunner in pr/ and cppcheck in base/.
./testrunner TestFunctions: 62 tests, 0 failed.- Full
./testrunner -q: 5436 tests, 0 failed.
Scratch cases. These still warn as on base, sometimes at a different column:
- a branch without a return in an exhaustive chain (first or last branch)
- reversed operands
E1 == e - conditions that call a function
g() == E1 - an
intcompared with enumerators - a non-exhaustive
!=chain in a template - nested ifs inside a branch
f9<5>()instantiated withtemplate<int t>
The case const int x = 1; if (x) { return 1; } else { } no longer warns. That is correct, but it is not tested.
False negative (a3.cpp, a4b.cpp):
// a3.cpp
enum E { E1, E2 };
template<int t> int f9() {
if constexpr (t == E1) return 1;
else if constexpr (t == E2) return 2;
}// a4b.cpp
enum E { E1, E2 };
int f10(Unknown u) {
if (u == E1) return 1;
else if (u == E2) return 2;
}cppcheck -q --std=c++17 a3.cpp a4b.cpp:
- base:
a3.cpp:4:41: error: Found an exit path from function with non-void return type that has missing return statement [missingReturn]anda4b.cpp:4:31: ... [missingReturn] - PR: no output for either file.
In my runs the unknown-type function sat inside a larger file (a2.cpp, line 8 on base). The line in a4b.cpp is where it falls in this smaller file. The same thing happens for a member of an incomplete struct, s->x == E1 / E2 (base: a1.cpp:18:34, PR: nothing).
Automated review of 926d2fe by Claude. The pull request was built and tested locally.
| values.insert(enumTok->enumerator()->value); | ||
| } | ||
|
|
||
| if (expr->valueType() && expr->valueType()->typeScope != enumScope) |
There was a problem hiding this comment.
AI review
If expr has no value type, this check passes and the chain counts as exhaustive. Then a non-enum or unknown-typed expression matched against all enumerators suppresses missingReturn. Base warned for both of these, the PR does not:
enum E { E1, E2 };
template<int t> int f9() {
if constexpr (t == E1) return 1;
else if constexpr (t == E2) return 2;
}
int f10(Unknown u) {
if (u == E1) return 1;
else if (u == E2) return 2;
}The same happens for s->x where S is incomplete. It may be better to accept a missing value type only for a non-type template parameter whose declared type is the enum, and reject it otherwise.
| } else if (tok->scope()->type == ScopeType::eElse) { | ||
| const Token *ifEnd = tok->link()->tokAt(-2); | ||
| if (Token::simpleMatch(ifEnd, "} else {")) { | ||
| // else branch is unreachable if condition is always true |
There was a problem hiding this comment.
AI review
This new handling of a known-true condition in front of an else changes results on its own. For example int f() { const int x = 1; if (x) { return 1; } else { } } no longer warns. checkMissingReturn10 has no test for it. Please add a test for it (and one where the if body lacks the return).
|
Test results for commit 926d2fe (tools/test-my-pr.py, main compared to this PR): Test: http://ec2-51-20-36-10.eu-north-1.compute.amazonaws.com/pr-9098/ 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. |
No description provided.