Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2642,7 +2642,7 @@ bool isVariableChanged(const Token *tok, int indirect, const Settings &settings,
if (!tok->isMutableExpr())
return false;

if (isConstVarExpression(tok))
if (indirect == 0 && isConstVarExpression(tok))

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

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.

return false;

const Token *tok2 = tok;
Expand Down
5 changes: 5 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4300,6 +4300,11 @@ class TestOther : public TestFixture {
"}\n");
ASSERT_EQUALS("[test.cpp:3:7]: (style) Variable 'a' can be declared as const array [constVariable]\n",
errout_str());

check("void f(V& v) {\n"
" std::sort(&v(1), &v(10));\n"
"}\n");
ASSERT_EQUALS("", errout_str());

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

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;.

}

void constParameterCallback() {
Expand Down
Loading