Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #13011 (False positive: returnByReference should not be recommended for non const method marked with &&) #6689

Merged
merged 1 commit into from
Aug 13, 2024
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
Loading