diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 915246810..ab94ca328 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -790,13 +790,15 @@ private: const Token *tok2; // parse setup - for (tok2 = tok.tokAt(3); tok2; tok2 = tok2->next()) + for (tok2 = tok.tokAt(3); tok2 != tok.link(); tok2 = tok2->next()) { if (tok2->str() == ";") break; if (tok2->varId()) varid1.insert(tok2->varId()); } + if (tok2 == tok.link()) + return &tok; // parse condition if (Token::Match(tok2, "; %var% <|<=|>=|> %num% ;")) diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 724985df4..585b8f07c 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -757,6 +757,15 @@ private: " a++;" "}\n"); ASSERT_EQUALS("[test.cpp:6]: (error) Uninitialized variable: a\n", errout.str()); + + // Ticket #2226: C++0x loop + checkUninitVar("void f() {\n" + " container c;\n" + " for (iterator it : c) {\n" + " }\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } // switch..