diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 3feb049e1..e8e7e56d6 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -3259,7 +3259,7 @@ void CheckOther::checkRedundantCopy() for (std::size_t i = 0; i < symbolDatabase->getVariableListSize(); i++) { const Variable* var = symbolDatabase->getVariableFromVarId(i); - if (!var || var->isReference() || !var->isConst() || var->isPointer() || !var->type()) // bailout if var is of standard type, if it is a pointer or non-const + if (!var || var->isReference() || !var->isConst() || var->isPointer() || (!var->type() && !var->isStlType())) // bailout if var is of standard type, if it is a pointer or non-const continue; const Token* startTok = var->nameToken(); diff --git a/test/testother.cpp b/test/testother.cpp index 8e895746c..0a6089d5d 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -6049,6 +6049,12 @@ private: checkOther.checkRedundantCopy(); } void checkRedundantCopy() { + check_redundant_copy("const std::string& getA(){static std::string a;return a;}\n" + "void foo() {\n" + " const std::string a = getA();\n" + "}"); + ASSERT_EQUALS("[test.cpp:3]: (performance) Use const reference for 'a' to avoid unnecessary data copying.\n", errout.str()); + check_redundant_copy("class A{public:A(){}};\n" "const A& getA(){static A a;return a;}\n" "int main()\n"