Skip to content

Commit 0aec648

Browse files
Fix #13628 FP: accessMoved with ternary
When std::move(x) is only in the true branch of a ternary operator, endOfFunctionCall was left pointing at the first token of the false branch, causing valueFlowForward to tag it as always-moved and report a spurious warning on the ? token. Fix by advancing past the entire false-branch subtree using nextAfterAstRightmostLeaf before starting propagation. Signed-off-by: Francois Berder <fberder@outlook.fr>
1 parent 21de4fa commit 0aec648

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

lib/valueflow.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3296,8 +3296,16 @@ static void valueFlowAfterMove(const TokenList& tokenlist, const SymbolDatabase&
32963296
ternaryColon = ternaryColon->astParent();
32973297
if (Token::simpleMatch(ternaryColon, ":")) {
32983298
endOfFunctionCall = ternaryColon->astOperand2();
3299-
if (Token::simpleMatch(endOfFunctionCall, "("))
3299+
if (Token::simpleMatch(endOfFunctionCall, "(")) {
33003300
endOfFunctionCall = endOfFunctionCall->link();
3301+
} else {
3302+
Token* next = nextAfterAstRightmostLeaf(endOfFunctionCall);
3303+
if (next)
3304+
endOfFunctionCall = next;
3305+
else
3306+
endOfFunctionCall = endOfFunctionCall->next();
3307+
3308+
}
33013309
}
33023310
}
33033311
ValueFlow::Value value;

test/testother.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12861,6 +12861,18 @@ class TestOther : public TestFixture {
1286112861
" h(b ? h(gA(5, std::move(s))) : h(gB(7, std::move(s))));\n"
1286212862
"}\n");
1286312863
ASSERT_EQUALS("", errout_str());
12864+
12865+
check("int cb(std::string);\n" // #13628
12866+
"void f(bool b, std::string s1) {\n"
12867+
" std::string s2 = b ? cb(std::move(s1)) : s1;\n"
12868+
"}\n");
12869+
ASSERT_EQUALS("", errout_str());
12870+
12871+
check("int cb(std::string);\n"
12872+
"void f(bool b, std::string s1) {\n"
12873+
" std::string s2 = b ? cb(std::move(s1)) : s1 + s1;\n"
12874+
"}\n");
12875+
ASSERT_EQUALS("", errout_str());
1286412876
}
1286512877

1286612878
void movePointerAlias()

0 commit comments

Comments
 (0)