diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index afdee8834..d4f96ff29 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -3617,8 +3617,8 @@ static void valueFlowContainerSize(TokenList *tokenlist, SymbolDatabase* symbold // known value in conditional code if (conditionToken->str() == "==" || conditionToken->str() == "(") { - const Token *parent = conditionToken; - while (parent && parent->str() != "!") + const Token *parent = conditionToken->astParent(); + while (parent && !Token::Match(parent, "!|==|!=")) parent = parent->astParent(); if (!parent) { value.setKnown(); diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index 015f39e0b..9afd51b11 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -3326,6 +3326,13 @@ private: "}"; ASSERT_EQUALS("", isKnownContainerSizeValue(tokenValues(code, "ints . front"), 0)); + code = "void f(const std::list &ints) {\n" + " if (ints.empty() == false) {\n" + " ints.front();\n" // <- container is not empty + " }\n" + "}"; + ASSERT(tokenValues(code, "ints . front").empty()); + code = "void f(const std::vector &v) {\n" " if (v.empty()) {}\n" " if (!v.empty() && v[10]==0) {}\n" // <- no container size for 'v[10]'