Skip to content

Commit 5f4b06c

Browse files
committed
isVariableChangedByFunctionCall: Fix FN when constructor argument is const reference
1 parent 3e231a9 commit 5f4b06c

2 files changed

Lines changed: 14 additions & 3 deletions

File tree

lib/astutils.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -418,10 +418,10 @@ bool isVariableChangedByFunctionCall(const Token *tok, const Settings *settings,
418418
const ::Scope *typeScope = tok->variable()->typeScope();
419419
if (typeScope) {
420420
for (std::list<Function>::const_iterator it = typeScope->functionList.begin(); it != typeScope->functionList.end(); ++it) {
421-
if (!it->isConstructor() || it->argCount() != argCount)
421+
if (!it->isConstructor() || it->argCount() < argCount)
422422
continue;
423423
const Variable *arg = it->getArgumentVar(argnr);
424-
if (arg && arg->isReference())
424+
if (arg && arg->isReference() && !arg->isConst())
425425
return true;
426426
}
427427
return false;

test/testvalueflow.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1804,14 +1804,25 @@ class TestValueFlow : public TestFixture {
18041804

18051805
code = "class C {\n"
18061806
"public:\n"
1807-
" C(int &i);\n"
1807+
" C(int &i);\n" // non-const argument => might be changed
18081808
"};\n"
18091809
"int f() {\n"
18101810
" int x=1;\n"
18111811
" C c(x);\n"
18121812
" return x;\n"
18131813
"}";
18141814
ASSERT_EQUALS(false, testValueOfX(code, 8U, 1));
1815+
1816+
code = "class C {\n"
1817+
"public:\n"
1818+
" C(const int &i);\n" // const argument => is not changed
1819+
"};\n"
1820+
"int f() {\n"
1821+
" int x=1;\n"
1822+
" C c(x);\n"
1823+
" return x;\n"
1824+
"}";
1825+
ASSERT_EQUALS(true, testValueOfX(code, 8U, 1));
18151826
}
18161827

18171828
void valueFlowForwardLambda() {

0 commit comments

Comments
 (0)