Skip to content

Commit 3de3155

Browse files
committed
Fix
1 parent 392f09e commit 3de3155

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

lib/checkother.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3337,8 +3337,10 @@ static bool checkVariableAssignment(const Token* tok, const Settings& settings)
33373337
const Variable* var = tok->variable();
33383338
if (!var || !isLargeObject(var, settings))
33393339
return false;
3340-
if (isVariableChanged(var, settings))
3340+
if (findVariableChanged(tok->tokAt(2), tok->scope()->bodyEnd, /*indirect*/ 0, var->declarationId(), /*globalvar*/ false, settings))
33413341
return false;
3342+
if (var->isLocal() || (var->isArgument() && !var->isReference()))
3343+
return true;
33423344
const Scope* scope = tok->scope();
33433345
while (scope && scope->type != ScopeType::eFunction)
33443346
scope = scope->nestedIn;

test/testother.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9910,6 +9910,19 @@ class TestOther : public TestFixture {
99109910
" return c.size();\n"
99119911
"}\n");
99129912
ASSERT_EQUALS("", errout_str());
9913+
9914+
check("struct S {\n"
9915+
" std::string m;\n"
9916+
" int f(std::string s);\n"
9917+
"};\n"
9918+
"int S::f(std::string s) {\n"
9919+
" s += m;\n"
9920+
" std::string c = s;\n"
9921+
" m.clear();\n"
9922+
" return c.size();\n"
9923+
"}\n");
9924+
ASSERT_EQUALS("[test.cpp:7:17]: (performance, inconclusive) Use const reference for 'c' to avoid unnecessary data copying. [redundantCopyLocalConst]\n",
9925+
errout_str());
99139926
}
99149927

99159928
void checkNegativeShift() {

0 commit comments

Comments
 (0)