Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/checkclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions test/testclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9045,6 +9045,13 @@ class TestClass : public TestFixture {
"};\n"
"U<std::string> 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());
}
};

Expand Down