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
14 changes: 13 additions & 1 deletion lib/vf_analyzers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,19 @@ static bool stateCoversCondition(const Token* tok, const ProgramMemory& pm, int
return true;
if (tok->varId() > 0 || Token::simpleMatch(tok, ".")) {
const ProgramMemory::Values* values = pm.getValues(tok->exprId());
return values && !values->empty() && std::all_of(values->cbegin(), values->cend(), [](const ValueFlow::Value& v) {
if (!values || values->empty())
return false;
// The range of a bool (not <= -1, not >= 2) does not say anything about its value, use the
// expression that the bool is known to be equal to instead
if (astIsBool(tok) && std::all_of(values->cbegin(), values->cend(), [](const ValueFlow::Value& v) {
return v.isImpossible() && (v.intvalue < 0 || v.intvalue > 1);

@pfultz2 pfultz2 Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think we should to filter out these values in the all_of. And when that returns false we can check the symbolic values:

auto filterValues = [&](const ValueFlow::Value& v) {
    return not isOutOfRangeImpossibleValue(tok, value);
};
bool isInt = std::count_if(values->cbegin(), values->cend(), filterValues) && std::all_of(values->cbegin(), values->cend(), [](const ValueFlow::Value& v) {
    if(filterValues(value))
        return v.isIntValue();
    return true;
});
if(isInt)
    return true;
return std::any_of(tok->values().cbegin(), tok->values().cend(), [&](const ValueFlow::Value& v) {
    return v.isSymbolicValue() && v.intvalue == 0 &&
        stateCoversCondition(v.tokvalue, pm, depth - 1);
});

I think that would be a better structure here. The isOutOfRangeImpossibleValue can work for bool here but we can extend it for other integers in the future.

Another option would be to use copy_if to filter the values.

})) {
return std::any_of(tok->values().cbegin(), tok->values().cend(), [&](const ValueFlow::Value& v) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

AI review

When a bool's memory only holds the impossible range values, this now returns false unless the bool's known symbolic value is covered too. In practice that hides true positives that base reports:

void h3(W m, bool c) {
    for (int i = 0; i < 2; i++) {
        const bool b = c;
        if (b) w[i] = std::move(m); else w[i] = m.flip();   // base: accessMoved 16:33, PR: nothing
    }
}
void h5(W m) {
    for (int i = 0; i < 3; i++) {
        const bool first = i == 0;
        if (!first) w[i] = std::move(m);                    // base: accessMoved, PR: nothing (moved at i==1 and i==2)
    }
}

With if (c) instead of if (b), h3 is still reported, so the result now depends on whether the condition goes through a local. Please add one of these cases as a regression test that must still warn.

return v.isSymbolicValue() && v.isKnown() && v.intvalue == 0 &&

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I dont think ProgramMemory stores values as known. Also, why are we allowing symbolic values here?

stateCoversCondition(v.tokvalue, pm, depth - 1);
});
}
return std::all_of(values->cbegin(), values->cend(), [](const ValueFlow::Value& v) {
return v.isIntValue();
});
}
Expand Down
26 changes: 26 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ class TestOther : public TestFixture {
TEST_CASE(doubleMoveMemberInitialization2);
TEST_CASE(doubleMoveMemberInitialization3); // #9974
TEST_CASE(doubleMoveMemberInitialization4);
TEST_CASE(moveInLoopBoolCondition);
TEST_CASE(moveAndAssign1);
TEST_CASE(moveAndAssign2);
TEST_CASE(moveAssignMoveAssign);
Expand Down Expand Up @@ -13040,6 +13041,31 @@ class TestOther : public TestFixture {
ASSERT_EQUALS("[test.cpp:3:51]: (warning, inconclusive) Access of moved variable 'd'. [accessMoved]\n", errout_str());
}

void moveInLoopBoolCondition() { // #15288
check("struct W { std::vector<int> v; W flip() const; };\n"
"W w[2];\n"
"void f(W m) {\n"
" for (int i = 0; i < 2; i++) {\n"
" const bool first = (i == 0);\n"
" if (first) {\n"
" w[i] = m.flip();\n"
" } else {\n"
" w[i] = std::move(m);\n"
" }\n"
" }\n"
"}\n");
ASSERT_EQUALS("", errout_str());

check("struct W { std::vector<int> v; };\n"
"W w[2];\n"
"void f(W m) {\n"
" for (int i = 0; i < 2; i++) {\n"
" w[i] = std::move(m);\n"
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:5:26]: (warning) Access of moved variable 'm'. [accessMoved]\n", errout_str());
}

void moveAndAssign1() {
check("A g(A a);\n"
"void f() {\n"
Expand Down
Loading