Skip to content

Commit

Permalink
Fix #12174 FP accessMoved with ternary operator (#5966)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github authored Feb 12, 2024
1 parent 8c87b4b commit 554dccd
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
30 changes: 30 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 554dccd

Please sign in to comment.