diff --git a/lib/checkother.cpp b/lib/checkother.cpp index f78401158..88431816b 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -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) == "&") { diff --git a/test/testother.cpp b/test/testother.cpp index 187707b89..10a27fc78 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -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() {