From 7c78c12d893b2536150744dbc024708804b2844c Mon Sep 17 00:00:00 2001 From: chrchr Date: Fri, 9 Feb 2024 12:36:17 +0100 Subject: [PATCH 1/3] Fix #12174 FP accessMoved with ternary operator --- lib/valueflow.cpp | 10 ++++++++++ test/testother.cpp | 21 +++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index e181bf3cc89..24e05c9c534 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -5232,6 +5232,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(parent, "(")) + 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 90247330a9f..1af8c7c3f98 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,26 @@ 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()); + } + void funcArgNamesDifferent() { check("void func1(int a, int b, int c);\n" "void func1(int a, int b, int c) { }\n" From f1744bb03170d9ad77ba587b69da9a208751d1c4 Mon Sep 17 00:00:00 2001 From: chrchr Date: Fri, 9 Feb 2024 12:37:37 +0100 Subject: [PATCH 2/3] Simplify --- lib/valueflow.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 24e05c9c534..6ccc3d62fac 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -5234,8 +5234,6 @@ static void valueFlowAfterMove(TokenList& tokenlist, const SymbolDatabase& symbo if (endOfFunctionCall) { if (endOfFunctionCall->str() == ")") { Token* ternaryColon = endOfFunctionCall->link()->astParent(); - while (Token::simpleMatch(parent, "(")) - ternaryColon = ternaryColon->astParent(); if (Token::simpleMatch(ternaryColon, ":")) { endOfFunctionCall = ternaryColon->astOperand2(); if (Token::simpleMatch(endOfFunctionCall, "(")) From 2e03976c09bba1ada2976ebf94ba8ddd7ef59062 Mon Sep 17 00:00:00 2001 From: chrchr Date: Fri, 9 Feb 2024 13:02:36 +0100 Subject: [PATCH 3/3] Add test --- lib/valueflow.cpp | 2 ++ test/testother.cpp | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 6ccc3d62fac..f5ac41c58f1 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -5234,6 +5234,8 @@ static void valueFlowAfterMove(TokenList& tokenlist, const SymbolDatabase& symbo 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, "(")) diff --git a/test/testother.cpp b/test/testother.cpp index 1af8c7c3f98..dd07900d9aa 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -11169,6 +11169,15 @@ class TestOther : public TestFixture { " 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() {