Skip to content

Commit 554dccd

Browse files
Fix #12174 FP accessMoved with ternary operator (#5966)
1 parent 8c87b4b commit 554dccd

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

lib/valueflow.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5234,6 +5234,16 @@ static void valueFlowAfterMove(TokenList& tokenlist, const SymbolDatabase& symbo
52345234
Token* openParentesisOfMove = findOpenParentesisOfMove(varTok);
52355235
Token* endOfFunctionCall = findEndOfFunctionCallForParameter(openParentesisOfMove);
52365236
if (endOfFunctionCall) {
5237+
if (endOfFunctionCall->str() == ")") {
5238+
Token* ternaryColon = endOfFunctionCall->link()->astParent();
5239+
while (Token::simpleMatch(ternaryColon, "("))
5240+
ternaryColon = ternaryColon->astParent();
5241+
if (Token::simpleMatch(ternaryColon, ":")) {
5242+
endOfFunctionCall = ternaryColon->astOperand2();
5243+
if (Token::simpleMatch(endOfFunctionCall, "("))
5244+
endOfFunctionCall = endOfFunctionCall->link();
5245+
}
5246+
}
52375247
ValueFlow::Value value;
52385248
value.valueType = ValueFlow::Value::ValueType::MOVED;
52395249
value.moveKind = moveKind;

test/testother.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ class TestOther : public TestFixture {
266266
TEST_CASE(forwardAndUsed);
267267
TEST_CASE(moveAndReference);
268268
TEST_CASE(moveForRange);
269+
TEST_CASE(moveTernary);
269270

270271
TEST_CASE(funcArgNamesDifferent);
271272
TEST_CASE(funcArgOrderDifferent);
@@ -11150,6 +11151,35 @@ class TestOther : public TestFixture {
1115011151
ASSERT_EQUALS("", errout.str());
1115111152
}
1115211153

11154+
void moveTernary()
11155+
{
11156+
check("void gA(std::string);\n" // #12174
11157+
"void gB(std::string);\n"
11158+
"void f(bool b) {\n"
11159+
" std::string s = \"abc\";\n"
11160+
" b ? gA(std::move(s)) : gB(std::move(s));\n"
11161+
"}\n");
11162+
ASSERT_EQUALS("", errout.str());
11163+
11164+
check("int gA(std::string);\n"
11165+
"int gB(std::string);\n"
11166+
"void h(int);\n"
11167+
"void f(bool b) {\n"
11168+
" std::string s = \"abc\";\n"
11169+
" h(b ? gA(std::move(s)) : gB(std::move(s)));\n"
11170+
"}\n");
11171+
ASSERT_EQUALS("", errout.str());
11172+
11173+
check("int gA(int, std::string);\n"
11174+
"int gB(int, std::string);\n"
11175+
"int h(int);\n"
11176+
"void f(bool b) {\n"
11177+
" std::string s = \"abc\";\n"
11178+
" h(b ? h(gA(5, std::move(s))) : h(gB(7, std::move(s))));\n"
11179+
"}\n");
11180+
ASSERT_EQUALS("", errout.str());
11181+
}
11182+
1115311183
void funcArgNamesDifferent() {
1115411184
check("void func1(int a, int b, int c);\n"
1115511185
"void func1(int a, int b, int c) { }\n"

0 commit comments

Comments
 (0)