Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3223,6 +3223,14 @@
return nextAfterAstRightmostLeaf(parent);
}

static bool isTryEmplace(const Token* tok)
{
if (tok->str() != "(" || !Token::simpleMatch(tok->astOperand1(), ".") || !tok->astOperand1()->astOperand1() || !Token::simpleMatch(tok->astOperand1()->astOperand2(), "try_emplace"))
return false;
const ValueType* vt = tok->astOperand1()->astOperand1()->valueType();
return vt && vt->container && vt->container->stdAssociativeLike;
}

static void valueFlowAfterMove(const TokenList& tokenlist, const SymbolDatabase& symboldatabase, ErrorLogger& errorLogger, const Settings& settings)
{
if (!tokenlist.isCPP() || settings.standards.cpp < Standards::CPP11)
Expand Down Expand Up @@ -3265,9 +3273,15 @@
const nonneg int varId = varTok->varId();
// x is not MOVED after assignment if code is: x = ... std::move(x) .. ;
const Token *parent = tok->astParent();
bool bail = false;

Check warning

Code scanning / Cppcheck Premium

The scope of the variable 'bail' can be reduced. Warning

The scope of the variable 'bail' can be reduced.
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed
while (parent && parent->str() != "=" && parent->str() != "return" &&
!(parent->str() == "(" && isOpenParenthesisMemberFunctionCallOfVarId(parent, varId)))
!(parent->str() == "(" && isOpenParenthesisMemberFunctionCallOfVarId(parent, varId))) {
if (isTryEmplace(parent)) {
bail = true;
break;
}
parent = parent->astParent();
}
if (parent &&
(parent->str() == "return" || // MOVED in return statement
parent->str() == "(")) // MOVED in self assignment, isOpenParenthesisMemberFunctionCallOfVarId == true
Expand Down
12 changes: 12 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ class TestOther : public TestFixture {
TEST_CASE(moveForRange);
TEST_CASE(moveTernary);
TEST_CASE(movePointerAlias);
TEST_CASE(moveTryEmplace);

TEST_CASE(funcArgNamesDifferent);
TEST_CASE(funcArgOrderDifferent);
Expand Down Expand Up @@ -12730,6 +12731,17 @@ class TestOther : public TestFixture {
ASSERT_EQUALS("[test.cpp:5:8]: (warning) Access of moved variable '.'. [accessMoved]\n", errout_str());
}

void moveTryEmplace()
{
check("void f(std::map<std::string, std::string>& m, std::string& s) {\n" // #12773
" bool b = m.try_emplace(\"a\", std::move(s)).second;\n"
" if (!b) {\n"
" std::cout << s;\n"
" }\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
Loading