From a25bc07b77296bb7f6aab4c850bb16d83fc96ced Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 13 Aug 2024 14:59:46 +0200 Subject: [PATCH] Fix #13011 (False positive: returnByReference should not be recommended for non const method marked with &&) (#6689) --- lib/checkclass.cpp | 3 +++ test/testclass.cpp | 7 +++++++ 2 files changed, 10 insertions(+) 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()); } };