diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 92d3127cc..2b609ecf1 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -1197,8 +1197,8 @@ bool CheckUninitVar::checkScopeForVariable(const Token *tok, const Variable& var if (Token::Match(tok, "sizeof|typeof|offsetof|decltype (")) tok = tok->next()->link(); - // for.. - if (Token::simpleMatch(tok, "for (")) { + // for/while.. + if (Token::Match(tok, "for|while (")) { // is variable initialized in for-head (don't report errors yet)? if (checkIfForWhileHead(tok->next(), var, true, false)) return true; diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index d00712014..8bace803e 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -2116,6 +2116,14 @@ private: "}\n"); ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: x\n", errout.str()); + checkUninitVar2("int f() {\n" + " int x;\n" + " while (foo)\n" + " if (bar) break;\n" + " return x;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized variable: x\n", errout.str()); + // if goto is simplified there might be conditions that are always true checkUninitVar2("void f() {\n" " int i;\n"