diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 062dfcc868e..8e3be76260a 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -2991,6 +2991,8 @@ struct ValueFlowAnalyzer : Analyzer { }); if (value) return {value->intvalue == 0}; + if (!match(tok)) + return {}; ProgramMemory pm = pms.get(tok, ctx, getProgramState()); MathLib::bigint out = 0; if (pm.getContainerEmptyValue(tok->exprId(), out)) diff --git a/test/teststl.cpp b/test/teststl.cpp index 252239307a0..76f3a2565ce 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -586,7 +586,10 @@ class TestStl : public TestFixture { " return true;\n" " return false;\n" "}\n"); - ASSERT_EQUALS("test.cpp:7:style:Consider using std::any_of algorithm instead of a raw loop.\n", errout.str()); + TODO_ASSERT_EQUALS("test.cpp:7:style:Consider using std::any_of algorithm instead of a raw loop.\n", + "test.cpp:8:error:Out of bounds access in expression 'v[i]' because 'v' is empty.\n" + "test.cpp:7 : style : Consider using std::any_of algorithm instead of a raw loop.\n", + errout.str()); checkNormal("bool g();\n" "int f(int x) {\n" diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 2c4b5f8eff7..e560ede0e46 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -6855,6 +6855,19 @@ class TestValueFlow : public TestFixture { " return x;\n" "}\n"; ASSERT_EQUALS(false, testValueOfXKnown(code, 9U, 1)); + + code = "int f(std::vector& v) {\n" + " std::vector o = v;\n" + " v.clear();\n" + " for (int i : o) {\n" + " if (i == 1) {\n" + " v.push_back(0);\n" + " }\n" + " }\n" + " auto x = v.empty();\n" + " return x;\n" + "}\n"; + ASSERT_EQUALS(false, testValueOfXKnown(code, 10U, 1)); } void valueFlowContainerElement()