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
4 changes: 2 additions & 2 deletions lib/valueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5738,11 +5738,11 @@ static void valueFlowSubFunction(const TokenList& tokenlist,
// passing value(s) to function
std::list<ValueFlow::Value> argvalues(getFunctionArgumentValues(argtok));

// Remove non-local lifetimes
// Remove non-local lifetimes and moved values
argvalues.remove_if([](const ValueFlow::Value& v) {
if (v.isLifetimeValue())
return !v.isLocalLifetimeValue() && !v.isSubFunctionLifetimeValue();
return false;
return v.isMovedValue();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

This removes moved values for every parameter kind, so it loses true positives when a moved-from object is passed by non-const reference. The caller does not warn in that case, because the callee could reinitialize the object, so before this PR the callee was the only place where the warning came from:

void g1(std::vector<int>& v) { int x = v[0]; (void)x; }
void f1() { std::vector<int> v; auto w = std::move(v); g1(v); }

base: accessMoved at v[0]. PR: no warning.

Restricting the removal to by-value parameters would fix #15150 and keep these warnings, e.g. return v.isMovedValue() && !argvar->isReference(); (the lambda would need to capture argvar). A test with a reference parameter would also help.

});
// Remove uninit values if argument is passed by value
if (argtok->variable() && !argtok->variable()->isPointer() && argvalues.size() == 1 &&
Expand Down
15 changes: 15 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ class TestOther : public TestFixture {
TEST_CASE(movePointerAlias);
TEST_CASE(moveOutparam);
TEST_CASE(moveTryEmplace);
TEST_CASE(moveSubFunction);

TEST_CASE(funcArgNamesDifferent);
TEST_CASE(funcArgOrderDifferent);
Expand Down Expand Up @@ -13342,6 +13343,20 @@ class TestOther : public TestFixture {
ASSERT_EQUALS("", errout_str());
}

void moveSubFunction()
{
check("struct T {\n" // #15150
" void set(std::string a) { m = std::move(a); }\n"
" std::string m;\n"
"};\n"
"void f(std::string s) {\n"
" T t;\n"
" t.set(std::move(s));\n"
" t.set(s);\n"
"}\n");
ASSERT_EQUALS("[test.cpp:8:11]: (warning) Access of moved variable 's'. [accessMoved]\n", 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