diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index d50201662f6..f45d6ffbe42 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -5234,6 +5234,16 @@ static void valueFlowAfterMove(TokenList& tokenlist, const SymbolDatabase& symbo Token* openParentesisOfMove = findOpenParentesisOfMove(varTok); Token* endOfFunctionCall = findEndOfFunctionCallForParameter(openParentesisOfMove); if (endOfFunctionCall) { + if (endOfFunctionCall->str() == ")") { + Token* ternaryColon = endOfFunctionCall->link()->astParent(); + while (Token::simpleMatch(ternaryColon, "(")) + ternaryColon = ternaryColon->astParent(); + if (Token::simpleMatch(ternaryColon, ":")) { + endOfFunctionCall = ternaryColon->astOperand2(); + if (Token::simpleMatch(endOfFunctionCall, "(")) + endOfFunctionCall = endOfFunctionCall->link(); + } + } ValueFlow::Value value; value.valueType = ValueFlow::Value::ValueType::MOVED; value.moveKind = moveKind; diff --git a/test/testother.cpp b/test/testother.cpp index 769a2ab427a..2c949a47877 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -266,6 +266,7 @@ class TestOther : public TestFixture { TEST_CASE(forwardAndUsed); TEST_CASE(moveAndReference); TEST_CASE(moveForRange); + TEST_CASE(moveTernary); TEST_CASE(funcArgNamesDifferent); TEST_CASE(funcArgOrderDifferent); @@ -11150,6 +11151,35 @@ class TestOther : public TestFixture { ASSERT_EQUALS("", errout.str()); } + void moveTernary() + { + check("void gA(std::string);\n" // #12174 + "void gB(std::string);\n" + "void f(bool b) {\n" + " std::string s = \"abc\";\n" + " b ? gA(std::move(s)) : gB(std::move(s));\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + + check("int gA(std::string);\n" + "int gB(std::string);\n" + "void h(int);\n" + "void f(bool b) {\n" + " std::string s = \"abc\";\n" + " h(b ? gA(std::move(s)) : gB(std::move(s)));\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + + check("int gA(int, std::string);\n" + "int gB(int, std::string);\n" + "int h(int);\n" + "void f(bool b) {\n" + " std::string s = \"abc\";\n" + " h(b ? h(gA(5, std::move(s))) : h(gB(7, std::move(s))));\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + void funcArgNamesDifferent() { check("void func1(int a, int b, int c);\n" "void func1(int a, int b, int c) { }\n"