From b26c5815567278d7325d33442dda2c78e4736d0f Mon Sep 17 00:00:00 2001 From: Paul Date: Thu, 10 Sep 2020 22:38:31 -0500 Subject: [PATCH] Fix issue 9888: False positive: Using reference to dangling temporary with std::move --- lib/symboldatabase.cpp | 4 +++- test/testautovariables.cpp | 7 +++++++ test/testsymboldatabase.cpp | 2 +- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 8b6e5219a..4fe0efb92 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -5468,7 +5468,9 @@ void SymbolDatabase::setValueType(Token *tok, const ValueType &valuetype) } // std::move if (vt2 && parent->str() == "(" && Token::simpleMatch(parent->tokAt(-3), "std :: move (")) { - setValueType(parent, valuetype); + ValueType vt = valuetype; + vt.reference = Reference::RValue; + setValueType(parent, vt); return; } if (parent->str() == "*" && !parent->astOperand2() && valuetype.pointer > 0U) { diff --git a/test/testautovariables.cpp b/test/testautovariables.cpp index 2b5668ff0..e0ea8075b 100644 --- a/test/testautovariables.cpp +++ b/test/testautovariables.cpp @@ -1695,6 +1695,13 @@ private: " std::cout << str_cref2 << std::endl;\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + check("char f() {\n" + " char c = 0;\n" + " char&& cr = std::move(c);\n" + " return cr;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void testglobalnamespace() { diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index 303282942..78c8817f4 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -6895,7 +6895,7 @@ private: ASSERT_EQUALS("signed int", typeOf("int (*a)(int); a(5);", "( 5")); ASSERT_EQUALS("s", typeOf("struct s { s foo(); s(int, int); }; s s::foo() { return s(1, 2); } ", "( 1 , 2 )")); // Some standard template functions.. TODO library configuration - ASSERT_EQUALS("signed int", typeOf("std::move(5);", "( 5 )")); + ASSERT_EQUALS("signed int &&", typeOf("std::move(5);", "( 5 )")); // struct member.. ASSERT_EQUALS("signed int", typeOf("struct AB { int a; int b; } ab; x = ab.a;", "."));