Fix #10191 FP redundantCopyLocalConst - referenced variable changes (#4202)

This commit is contained in:
chrchr-github 2022-06-11 16:58:33 +02:00 committed by GitHub
parent cb382ac52c
commit ecb24e28bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 1 deletions

View File

@ -2742,7 +2742,7 @@ void CheckOther::checkRedundantCopy()
const Token* startTok = var->nameToken();
if (startTok->strAt(1) == "=") // %type% %name% = ... ;
;
else if (startTok->strAt(1) == "(" && var->isClass() && var->typeScope()) {
else if (Token::Match(startTok->next(), "(|{") && var->isClass() && var->typeScope()) {
// Object is instantiated. Warn if constructor takes arguments by value.
if (constructorTakesReference(var->typeScope()))
continue;
@ -2760,6 +2760,8 @@ void CheckOther::checkRedundantCopy()
const Token* dot = tok->astOperand1();
if (Token::simpleMatch(dot, ".") && dot->astOperand1() && isVariableChanged(dot->astOperand1()->variable(), mSettings, mTokenizer->isCPP()))
continue;
if (exprDependsOnThis(tok->previous()))
continue;
const Function* func = tok->previous()->function();
if (func && func->tokenDef->strAt(-1) == "&") {

View File

@ -7509,6 +7509,14 @@ private:
"}");
ASSERT_EQUALS("", errout.str());
check("class A {};\n"
"class B { B(const A& a); };\n"
"const A& getA();\n"
"void f() {\n"
" const B b{ getA() };\n"
"}");
ASSERT_EQUALS("", errout.str());
// #5618
const char* code5618 = "class Token {\n"
"public:\n"
@ -7557,6 +7565,24 @@ private:
" return {};\n"
"}", nullptr, /*experimental*/ false, /*inconclusive*/ true);
ASSERT_EQUALS("", errout.str());
check("struct X { int x; };\n" // #10191
"struct S {\n"
" X _x;\n"
" X& get() { return _x; }\n"
" void modify() { _x.x += 42; }\n"
" int copy() {\n"
" const X x = get();\n"
" modify();\n"
" return x.x;\n"
" }\n"
" int constref() {\n"
" const X& x = get();\n"
" modify();\n"
" return x.x;\n"
" }\n"
"};\n", nullptr, /*experimental*/ false, /*inconclusive*/ true);
ASSERT_EQUALS("", errout.str());
}
void checkNegativeShift() {