diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 86001b691b9..8c525987793 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -3345,6 +3345,9 @@ void CheckClass::checkReturnByReference() if (const Library::Container* container = mSettings->library.detectContainer(func.retDef)) if (container->view) continue; + if (!func.isConst() && func.hasRvalRefQualifier()) + // this method could be used by temporary objects, return by value can be dangerous + continue; if (const Variable* var = getSingleReturnVar(func.functionScope)) { if (!var->valueType()) continue; diff --git a/test/testclass.cpp b/test/testclass.cpp index f6490bf76b7..f440381c317 100644 --- a/test/testclass.cpp +++ b/test/testclass.cpp @@ -9045,6 +9045,13 @@ class TestClass : public TestFixture { "};\n" "U u;\n"); ASSERT_EQUALS("", errout_str()); + + checkReturnByReference("struct S {\n" // #13011 + " std::string s;\n" + " const std::string& foo() const & { return s; }\n" + " std::string foo() && { return s; }\n" // <- used for temporary objects + "};\n"); + ASSERT_EQUALS("", errout_str()); } };