Skip to content

Commit 92176f7

Browse files
Fix #13317 FN constParameterReference when iterating over container member (danmar#7015)
1 parent 741fb09 commit 92176f7

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

lib/astutils.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2680,8 +2680,9 @@ bool isVariableChanged(const Token *tok, int indirect, const Settings &settings,
26802680
return false;
26812681

26822682
const Token *ftok = tok2->astParent()->astOperand2();
2683-
if (astIsContainer(tok2->astParent()->astOperand1()) && vt && vt->container) {
2684-
const Library::Container* c = vt->container;
2683+
const Token* const ctok = tok2->str() == "." ? tok2->astOperand2() : tok2;
2684+
if (astIsContainer(ctok) && ctok->valueType() && ctok->valueType()->container) {
2685+
const Library::Container* c = ctok->valueType()->container;
26852686
const Library::Container::Action action = c->getAction(ftok->str());
26862687
if (contains({Library::Container::Action::INSERT,
26872688
Library::Container::Action::ERASE,

lib/valueflow.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5549,7 +5549,7 @@ static void valueFlowLibraryFunction(Token* tok, const std::string& returnValue,
55495549
}
55505550

55515551
static void valueFlowSubFunction(const TokenList& tokenlist,
5552-
SymbolDatabase& symboldatabase,
5552+
const SymbolDatabase& symboldatabase,
55535553
ErrorLogger& errorLogger,
55545554
const Settings& settings)
55555555
{

test/testother.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3753,6 +3753,23 @@ class TestOther : public TestFixture {
37533753
" return g(a, s.x);\n"
37543754
"}\n");
37553755
ASSERT_EQUALS("", errout_str());
3756+
3757+
check("struct S { std::vector<int> v; };\n" // #13317
3758+
"struct T { S s; };\n"
3759+
"int f(S& s) {\n"
3760+
" for (std::vector<int>::const_iterator it = s.v.cbegin(); it != s.v.cend(); ++it) {}\n"
3761+
" return *s.v.cbegin();\n"
3762+
"}\n"
3763+
"int f(T& t) {\n"
3764+
" return *t.s.v.cbegin();\n"
3765+
"}\n"
3766+
"int f(std::vector<int>& v) {\n"
3767+
" return *v.cbegin();\n"
3768+
"}\n");
3769+
ASSERT_EQUALS("[test.cpp:3]: (style) Parameter 's' can be declared as reference to const\n"
3770+
"[test.cpp:7]: (style) Parameter 't' can be declared as reference to const\n"
3771+
"[test.cpp:10]: (style) Parameter 'v' can be declared as reference to const\n",
3772+
errout_str());
37563773
}
37573774

37583775
void constParameterCallback() {

0 commit comments

Comments
 (0)