Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #13317 FN constParameterReference when iterating over container member #7015

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
9 changes: 7 additions & 2 deletions lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2676,8 +2676,13 @@ bool isVariableChanged(const Token *tok, int indirect, const Settings &settings,
return false;

const Token *ftok = tok2->astParent()->astOperand2();
if (astIsContainer(tok2->astParent()->astOperand1()) && vt && vt->container) {
const Library::Container* c = vt->container;
const Token* ctok = tok2->astParent();
if (Token::simpleMatch(ctok->astOperand1(), "."))
ctok = ctok->astOperand1()->astOperand2();
else
ctok = ctok->astOperand1();
if (astIsContainer(ctok) && ctok->valueType() && ctok->valueType()->container) {
const Library::Container* c = ctok->valueType()->container;
const Library::Container::Action action = c->getAction(ftok->str());
if (contains({Library::Container::Action::INSERT,
Library::Container::Action::ERASE,
Expand Down
2 changes: 1 addition & 1 deletion lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5494,7 +5494,7 @@ static void valueFlowLibraryFunction(Token* tok, const std::string& returnValue,
}

static void valueFlowSubFunction(const TokenList& tokenlist,
SymbolDatabase& symboldatabase,
const SymbolDatabase& symboldatabase,
ErrorLogger& errorLogger,
const Settings& settings)
{
Expand Down
17 changes: 17 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3753,6 +3753,23 @@ class TestOther : public TestFixture {
" return g(a, s.x);\n"
"}\n");
ASSERT_EQUALS("", errout_str());

check("struct S { std::vector<int> v; };\n" // #13317
"struct T { S s; };\n"
"int f(S& s) {\n"
" for (std::vector<int>::const_iterator it = s.v.cbegin(); it != s.v.cend(); ++it) {}\n"
" return *s.v.cbegin();\n"
"}\n"
"int f(T& t) {\n"
" return *t.s.v.cbegin();\n"
"}\n"
"int f(std::vector<int>& v) {\n"
" return *v.cbegin();\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3]: (style) Parameter 's' can be declared as reference to const\n"
"[test.cpp:7]: (style) Parameter 't' can be declared as reference to const\n"
"[test.cpp:10]: (style) Parameter 'v' can be declared as reference to const\n",
errout_str());
}

void constParameterCallback() {
Expand Down
Loading