diff --git a/lib/vf_analyzers.cpp b/lib/vf_analyzers.cpp index aef5465bc9f..ca38fc4e96d 100644 --- a/lib/vf_analyzers.cpp +++ b/lib/vf_analyzers.cpp @@ -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); + })) { + return std::any_of(tok->values().cbegin(), tok->values().cend(), [&](const ValueFlow::Value& v) { + return v.isSymbolicValue() && v.isKnown() && v.intvalue == 0 && + stateCoversCondition(v.tokvalue, pm, depth - 1); + }); + } + return std::all_of(values->cbegin(), values->cend(), [](const ValueFlow::Value& v) { return v.isIntValue(); }); } diff --git a/test/testother.cpp b/test/testother.cpp index 085b39cfffb..088298d5115 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -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); @@ -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 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 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"