Repository navigation
Partial fix for #15166 FP constParameterReference for unknown container with operator() - #9080
chrchr-github wants to merge 2 commits into
Conversation
dmcppcheck
left a comment
There was a problem hiding this comment.
AI review
The change fixes the case in the test: &v(1) passed to std::sort/std::fill or to a function that takes int*. But at indirect == 0 the shortcut still applies, so the same unknown-container operator() FP stays when the element is written directly or bound to a non-const reference. void f(V& v) { v(1) = 0; } and h(v(1)) with void h(int&) still report constParameterReference. The PR title says "partial", but these are the most obvious forms of the ticket's pattern and they have no test, not even a TODO. On the files I compared, the PR caused no new warnings and no lost warnings, and the full testrunner passes.
Comments
| # | Comment | By | Where | Status |
|---|---|---|---|---|
| 1 | Still FP constParameterReference for v(1) = 0; and h(v(1)) (indirect 0) |
AI review | lib/astutils.cpp:2645 |
🆕 New |
| 2 | Test covers only one variant; no TODO for the remaining cases | AI review | test/testother.cpp:4307 |
🆕 New |
@chrchr-github, please fix, resolve or reply to the open and new comments: #1, #2.
Testing
I built cppcheck and testrunner in pr/ and cppcheck in base/. The full ./testrunner -q passes: 5427 tests, 0 failures.
Test file scratch/a.cpp:
#include <algorithm>
void f1(V& v) { std::sort(&v(1), &v(10)); }
void f2(V& v) { int* p = &v(1); *p = 0; }
void g(int*);
void f3(V& v) { g(&v(1)); }
void h(int&);
void f4(V& v) { h(v(1)); }
void f5(V& v) { v(1) = 0; }
void f6(V& v, int i) { std::sort(&v(i), &v(i+1)); }
void f7(V& v) { int& r = v(1); r = 0; }
void f8(V& v) { int* p = v.data(1); *p = 0; }
void f9(V& v) { std::sort(&v[1], &v[10]); }
void f10(V& v) { int* p = &v[1]; *p=0; }
void f11(V& v) { std::fill(&v(1), &v(10), 0); }
void f12(V& v) { std::sort(v(1), v(10)); }Command: cppcheck -q --enable=style,warning --inconclusive a.cpp
- base: constParameterReference for f1, f3, f4, f5, f11 and f12.
- PR: the warnings for f1, f3 and f11 are gone. These remain:
a.cpp:7:12: style: Parameter 'v' can be declared as reference to const [constParameterReference]
void f4(V& v) { h(v(1)); }
a.cpp:8:12: style: Parameter 'v' can be declared as reference to const [constParameterReference]
void f5(V& v) { v(1) = 0; }
a.cpp:15:13: style: Parameter 'v' can be declared as reference to const [constParameterReference]
void f12(V& v) { std::sort(v(1), v(10)); }
f5 is a direct write through v, so this warning is clearly wrong.
Both base and PR also report mismatchingContainers ("Addresses of different objects 'v(1)' and 'v(10)'") for f1, f6 and f11. The PR did not introduce this; it is a separate existing FP for the same pattern.
Regression check: I ran base and PR with style, warning and inconclusive enabled on lib/token.cpp, lib/tokenize.cpp, lib/checkother.cpp, samples/, test/cfg/std.cpp, test/cfg/std.c and test/cfg/posix.c. The results are identical.
Automated review of 72c8fbd by Claude. The pull request was built and tested locally.
| return false; | ||
|
|
||
| if (isConstVarExpression(tok)) | ||
| if (indirect == 0 && isConstVarExpression(tok)) |
There was a problem hiding this comment.
AI review
The FP is fixed only when indirect > 0. At indirect == 0, isConstVarExpression(v(1)) still returns true: for an unknown V, v(1) looks like a call to a function v with constant arguments. So isVariableChanged returns false before it looks at the parent, and these cases still get constParameterReference:
void f5(V& v) { v(1) = 0; } // direct write
void h(int&);
void f4(V& v) { h(v(1)); }I checked both with the PR's cppcheck. A more targeted fix might be to not treat %var% ( as a constant expression when the token is a variable (it is a call to operator(), not a function call). Then the indirect special case would not be needed. If this stays a partial fix, please add a TODO test for the v(1) = 0 case.
| check("void f(V& v) {\n" | ||
| " std::sort(&v(1), &v(10));\n" | ||
| "}\n"); | ||
| ASSERT_EQUALS("", errout_str()); |
There was a problem hiding this comment.
AI review
Only one variant is tested. Please also cover g(&v(1)) with void g(int*), which this PR fixes, and add TODO asserts for the cases that are still wrong, like v(1) = 0;.
|
Test results for commit 72c8fbd (tools/test-my-pr.py, main compared to this PR): Test: http://ec2-16-170-140-253.eu-north-1.compute.amazonaws.com/pr-9080/ 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.