diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 5d91f35e3..f154ad8c7 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -4510,9 +4510,10 @@ struct ValueFlowConditionHandler { if (Token::simpleMatch(top->link(), ") {")) { Token *after = top->link()->linkAt(1); const Token* unknownFunction = nullptr; - bool dead_if = isReturnScope(after, &settings->library, &unknownFunction) || - (tok->astParent() && Token::simpleMatch(tok->astParent()->previous(), "while (") && - !isBreakScope(after)); + const bool isWhile = + tok->astParent() && Token::simpleMatch(tok->astParent()->previous(), "while ("); + bool dead_if = (!isBreakScope(after) && isWhile) || + (isReturnScope(after, &settings->library, &unknownFunction) && !isWhile); bool dead_else = false; if (!dead_if && unknownFunction) { diff --git a/test/testvalueflow.cpp b/test/testvalueflow.cpp index ba990f833..7cc852f39 100644 --- a/test/testvalueflow.cpp +++ b/test/testvalueflow.cpp @@ -2478,6 +2478,20 @@ private: " return x;\n" "}\n"; ASSERT_EQUALS(false, testValueOfXImpossible(code, 8U, 0)); + + code = "bool c();\n" + "long f() {\n" + " bool stop = false;\n" + " while (!stop) {\n" + " if (c())\n" + " stop = true;\n" + " break;\n" + " }\n" + " int x = !stop;\n" + " return x;\n" + "}\n"; + ASSERT_EQUALS(false, testValueOfXImpossible(code, 10U, 1)); + ASSERT_EQUALS(false, testValueOfXKnown(code, 10U, 0)); } void valueFlowAfterConditionExpr() {