diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 6817fdbf1..7cb672fc4 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1327,11 +1327,10 @@ bool CheckUninitVar::checkScopeForVariable(const Scope* scope, const Token *tok, const Token *tok2 = tok->next()->link()->next(); if (tok2 && tok2->str() == "{") { - bool possibleinit = false; - bool init = checkLoopBody(tok2, var, membervar, suppressErrors); + bool init = checkLoopBody(tok2, var, membervar, (number_of_if > 0) | suppressErrors); // variable is initialized in the loop.. - if (possibleinit || init) + if (init) return true; // is variable used in for-head? diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 72e39d612..6f1cb80b6 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -2618,6 +2618,15 @@ private: " i++;\n" // <- no error if b(x) is always true when a(x) is false "}"); ASSERT_EQUALS("", errout.str()); + + checkUninitVar2("void f(int x) {\n" + " int i;\n" + " if (x) i = 0;\n" + " while (condition) {\n" + " if (x) i++;\n" // <- no error + " }\n" + "}"); + ASSERT_EQUALS("", errout.str()); } void uninitvar2_structmembers() { // struct members