diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 40fd10a97d4..faca0dce18a 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2913,7 +2913,7 @@ void CheckOther::checkRedundantCopy() for (const Variable* var : symbolDatabase->variableList()) { if (!var || var->isReference() || var->isPointer() || - (!var->type() && !var->isStlType()) || // bailout if var is of standard type, if it is a pointer or non-const + (!var->type() && !var->isStlType() && !(var->valueType() && var->valueType()->container)) || // bailout if var is of standard type, if it is a pointer or non-const (!var->isConst() && isVariableChanged(var, *mSettings))) continue; diff --git a/test/testother.cpp b/test/testother.cpp index da58be85e81..db7f3b0d9f7 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -9046,6 +9046,16 @@ class TestOther : public TestFixture { TODO_ASSERT_EQUALS("", "[test.cpp:16] -> [test.cpp:18]: (style) The comparison 'c == m->get()' is always true because 'c' and 'm->get()' represent the same value.\n", errout_str()); + + check("struct T {\n" + " std::string s;\n" + " const std::string& get() const { return s; }\n" + "};\n" + "void f(const T& t) {\n" + " const auto s = t.get();\n" + " if (s.empty()) {}\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:6]: (performance, inconclusive) Use const reference for 's' to avoid unnecessary data copying.\n", errout_str()); } void checkNegativeShift() {