Skip to content

Commit 6d667c7

Browse files
Fix #15150 Duplicate accessMoved (passed by value to subfunction) (#8969)
Co-authored-by: chrchr-github <noreply@github.com>
1 parent 103143d commit 6d667c7

2 files changed

Lines changed: 28 additions & 3 deletions

File tree

‎lib/valueflow.cpp‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5754,11 +5754,11 @@ static void valueFlowSubFunction(const TokenList& tokenlist,
57545754
// passing value(s) to function
57555755
std::list<ValueFlow::Value> argvalues(getFunctionArgumentValues(argtok));
57565756

5757-
// Remove non-local lifetimes
5758-
argvalues.remove_if([](const ValueFlow::Value& v) {
5757+
// Remove non-local lifetimes and moved values
5758+
argvalues.remove_if([argvar](const ValueFlow::Value& v) {
57595759
if (v.isLifetimeValue())
57605760
return !v.isLocalLifetimeValue() && !v.isSubFunctionLifetimeValue();
5761-
return false;
5761+
return v.isMovedValue() && !argvar->isReference();
57625762
});
57635763
// Remove uninit values if argument is passed by value
57645764
if (argtok->variable() && !argtok->variable()->isPointer() && argvalues.size() == 1 &&

‎test/testother.cpp‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ class TestOther : public TestFixture {
312312
TEST_CASE(movePointerAlias);
313313
TEST_CASE(moveOutparam);
314314
TEST_CASE(moveTryEmplace);
315+
TEST_CASE(moveSubFunction);
315316

316317
TEST_CASE(funcArgNamesDifferent);
317318
TEST_CASE(funcArgOrderDifferent);
@@ -13408,6 +13409,30 @@ class TestOther : public TestFixture {
1340813409
ASSERT_EQUALS("", errout_str());
1340913410
}
1341013411

13412+
void moveSubFunction()
13413+
{
13414+
check("struct T {\n" // #15150
13415+
" void set(std::string a) { m = std::move(a); }\n"
13416+
" std::string m;\n"
13417+
"};\n"
13418+
"void f(std::string s) {\n"
13419+
" T t;\n"
13420+
" t.set(std::move(s));\n"
13421+
" t.set(s);\n"
13422+
"}\n");
13423+
ASSERT_EQUALS("[test.cpp:8:11]: (warning) Access of moved variable 's'. [accessMoved]\n", errout_str());
13424+
13425+
check("void g(const std::string& s) {}\n"
13426+
"void h(std::string& r) { x = std::move(r); }\n"
13427+
"void f(std::string s1, std::string s2) {\n"
13428+
" t1 = std::move(s1);\n"
13429+
" t2 = std::move(s2);\n"
13430+
" g(s1);\n"
13431+
" h(s2);\n"
13432+
"}\n");
13433+
ASSERT_EQUALS("[test.cpp:2:40]: (warning) Access of moved variable 'r'. [accessMoved]\n", errout_str());
13434+
}
13435+
1341113436
void funcArgNamesDifferent() {
1341213437
check("void func1(int a, int b, int c);\n"
1341313438
"void func1(int a, int b, int c) { }\n"

0 commit comments

Comments
 (0)