diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index d4f971347..8d13e8503 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -4476,6 +4476,7 @@ struct ConditionHandler { // start token of conditional code Token* startTokens[] = {nullptr, nullptr}; + bool inverted = false; // if astParent is "!" we need to invert codeblock { const Token* tok2 = tok; @@ -4484,6 +4485,7 @@ struct ConditionHandler { while (parent && parent->str() == "&&") parent = parent->astParent(); if (parent && (parent->str() == "!" || Token::simpleMatch(parent, "== false"))) { + inverted = true; std::swap(thenValues, elseValues); } tok2 = parent; @@ -4568,7 +4570,7 @@ struct ConditionHandler { } if (dead_if || dead_else) { - if (Token::Match(tok->astParent(), "&&|&")) { + if (!inverted && Token::Match(tok->astParent(), "&&|&")) { values.remove_if(std::mem_fn(&ValueFlow::Value::isImpossible)); changeKnownToPossible(values); } else { diff --git a/test/teststl.cpp b/test/teststl.cpp index 686b7bcf2..e210329ae 100644 --- a/test/teststl.cpp +++ b/test/teststl.cpp @@ -3805,6 +3805,15 @@ private: " }\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + check("int f(std::vector::iterator it, const std::vector& vector) {\n" + " if (!(it != vector.end() && it != vector.begin()))\n" + " throw std::out_of_range();\n" + " if (it != vector.end() && *it == 0)\n" + " return -1;\n" + " return *it;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void dereferenceInvalidIterator2() {