From 27578e9c4c1f90c62b6938867735a054082e178e Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 30 Jun 2022 13:50:31 +0200 Subject: [PATCH] Fix FP returnStdMoveLocal (#4244) --- lib/checkfunctions.cpp | 2 +- test/testfunctions.cpp | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/checkfunctions.cpp b/lib/checkfunctions.cpp index 8fdf20d29..e831fb5d8 100644 --- a/lib/checkfunctions.cpp +++ b/lib/checkfunctions.cpp @@ -658,7 +658,7 @@ void CheckFunctions::returnLocalStdMove() if (retval->variable() && retval->variable()->isLocal() && !retval->variable()->isVolatile()) copyElisionError(retval); // RVO - if (Token::Match(retval, "(|{") && !retval->isCast()) + if (Token::Match(retval, "(|{") && !retval->isCast() && !(retval->valueType() && retval->valueType()->reference != Reference::None)) copyElisionError(retval); } } diff --git a/test/testfunctions.cpp b/test/testfunctions.cpp index eef345b5d..1503ae27f 100644 --- a/test/testfunctions.cpp +++ b/test/testfunctions.cpp @@ -1731,6 +1731,18 @@ private: check("struct A{} a; A f1() { return std::move(a); }\n" "A f2() { volatile A var; return std::move(var); }"); ASSERT_EQUALS("", errout.str()); + + check("struct S { std::string msg{ \"abc\" }; };\n" + "std::unique_ptr get(std::vector>& v) {\n" + " return std::move(v.front());\n" + "}\n" + "int main() {\n" + " std::vector> v;\n" + " v.emplace_back(std::make_unique());\n" + " auto p = get(v);\n" + " std::cout << p->msg;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void negativeMemoryAllocationSizeError() { // #389