Skip to content

Commit cf64cce

Browse files
Fix #12147 false negative: passedByValue (#5626)
1 parent bc174c5 commit cf64cce

File tree

3 files changed

+36
-20
lines changed

3 files changed

+36
-20
lines changed

lib/astutils.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2579,8 +2579,8 @@ bool isVariableChanged(const Token *tok, int indirect, const Settings *settings,
25792579
if (indirect == 0 && astIsPointer(tok))
25802580
return false;
25812581

2582-
const Token *ftok = tok->tokAt(2);
2583-
if (astIsContainer(tok) && vt && vt->container) {
2582+
const Token *ftok = tok2->astParent()->astOperand2();
2583+
if (astIsContainer(tok2->astParent()->astOperand1()) && vt && vt->container) {
25842584
const Library::Container* c = vt->container;
25852585
const Library::Container::Action action = c->getAction(ftok->str());
25862586
if (contains({Library::Container::Action::INSERT,

lib/checkclass.cpp

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -659,12 +659,11 @@ std::vector<CheckClass::Usage> CheckClass::createUsageList(const Scope *scope)
659659

660660
void CheckClass::assignVar(std::vector<Usage> &usageList, nonneg int varid)
661661
{
662-
for (Usage& usage: usageList) {
663-
if (usage.var->declarationId() == varid) {
664-
usage.assign = true;
665-
return;
666-
}
667-
}
662+
auto it = std::find_if(usageList.begin(), usageList.end(), [varid](const Usage& usage) {
663+
return usage.var->declarationId() == varid;
664+
});
665+
if (it != usageList.end())
666+
it->assign = true;
668667
}
669668

670669
void CheckClass::assignVar(std::vector<Usage> &usageList, const Token* vartok)
@@ -673,23 +672,21 @@ void CheckClass::assignVar(std::vector<Usage> &usageList, const Token* vartok)
673672
assignVar(usageList, vartok->varId());
674673
return;
675674
}
676-
for (Usage& usage: usageList) {
675+
auto it = std::find_if(usageList.begin(), usageList.end(), [vartok](const Usage& usage) {
677676
// FIXME: This is a workaround when varid is not set for a derived member
678-
if (usage.var->name() == vartok->str()) {
679-
usage.assign = true;
680-
return;
681-
}
682-
}
677+
return usage.var->name() == vartok->str();
678+
});
679+
if (it != usageList.end())
680+
it->assign = true;
683681
}
684682

685683
void CheckClass::initVar(std::vector<Usage> &usageList, nonneg int varid)
686684
{
687-
for (Usage& usage: usageList) {
688-
if (usage.var->declarationId() == varid) {
689-
usage.init = true;
690-
return;
691-
}
692-
}
685+
auto it = std::find_if(usageList.begin(), usageList.end(), [varid](const Usage& usage) {
686+
return usage.var->declarationId() == varid;
687+
});
688+
if (it != usageList.end())
689+
it->init = true;
693690
}
694691

695692
void CheckClass::assignAllVar(std::vector<Usage> &usageList)

test/testother.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2191,6 +2191,25 @@ class TestOther : public TestFixture {
21912191
"}\n");
21922192
ASSERT_EQUALS("", errout.str());
21932193

2194+
check("struct S { std::list<int> l; };\n" // #12147
2195+
"class C { public: std::list<int> l; };\n"
2196+
"bool f(S s) {\n"
2197+
" return s.l.empty();\n"
2198+
"}\n"
2199+
"bool f(C c) {\n"
2200+
" return c.l.empty();\n"
2201+
"}\n");
2202+
ASSERT_EQUALS("[test.cpp:3]: (performance) Function parameter 's' should be passed by const reference.\n"
2203+
"[test.cpp:6]: (performance) Function parameter 'c' should be passed by const reference.\n",
2204+
errout.str());
2205+
2206+
check("struct S { std::list<int> a[1][1]; };\n"
2207+
"bool f(S s) {\n"
2208+
" return s.a[0][0].empty();\n"
2209+
"}\n");
2210+
ASSERT_EQUALS("[test.cpp:2]: (performance) Function parameter 's' should be passed by const reference.\n",
2211+
errout.str());
2212+
21942213
Settings settings1 = settingsBuilder().platform(Platform::Type::Win64).build();
21952214
check("using ui64 = unsigned __int64;\n"
21962215
"ui64 Test(ui64 one, ui64 two) { return one + two; }\n",

0 commit comments

Comments
 (0)