Skip to content

Commit

Permalink
Fix #13011 (False positive: returnByReference should not be recommend…
Browse files Browse the repository at this point in the history
…ed for non const method marked with &&) (#6689)
  • Loading branch information
danmar authored Aug 13, 2024
1 parent 35a0df1 commit a25bc07
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
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

0 comments on commit a25bc07

Please sign in to comment.