Skip to content

Commit

Permalink
Fix #13317 FN constParameterReference when iterating over container m…
Browse files Browse the repository at this point in the history
…ember (danmar#7015)
  • Loading branch information
chrchr-github authored Dec 7, 2024
1 parent 741fb09 commit 92176f7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
5 changes: 3 additions & 2 deletions lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2680,8 +2680,9 @@ 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* const ctok = tok2->str() == "." ? tok2->astOperand2() : tok2;
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 @@ -5549,7 +5549,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

0 comments on commit 92176f7

Please sign in to comment.