Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
18 changes: 17 additions & 1 deletion lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1843,9 +1843,9 @@
const bool isCpp = (src && src->isCpp()) || (dst && dst->isCpp());
if (isNotEqual(decl, parentdecl) && !(isCpp && (Token::simpleMatch(decl.first, "auto") || Token::simpleMatch(parentdecl.first, "auto"))))
return true;
if (isNotEqual(decl, dst->valueType(), isCpp, settings))

Check failure on line 1846 in lib/valueflow.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Called C++ object pointer is null

See more on https://sonarcloud.io/project/issues?id=danmar_cppcheck&issues=AZ1oZdPDF56l7c_-6NR5&open=AZ1oZdPDF56l7c_-6NR5&pullRequest=8421
return true;
if (isNotEqual(parentdecl, src->valueType(), isCpp, settings))

Check failure on line 1848 in lib/valueflow.cpp

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Called C++ object pointer is null

See more on https://sonarcloud.io/project/issues?id=danmar_cppcheck&issues=AZ1oZdPDF56l7c_-6NR6&open=AZ1oZdPDF56l7c_-6NR6&pullRequest=8421
return true;
}
return false;
Expand Down 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,17 @@
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;
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 (bail)
continue;
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