From 63a0c6ad4ac217e8fec80a6fd06270dd2ff2549a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 25 Jul 2011 16:35:30 +0200 Subject: [PATCH] Fixed #2796 (uninitialized and leaving scope) --- lib/executionpath.cpp | 8 +++++++- test/testuninitvar.cpp | 11 +++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/executionpath.cpp b/lib/executionpath.cpp index 3b72bdbe5..f719f14f0 100644 --- a/lib/executionpath.cpp +++ b/lib/executionpath.cpp @@ -160,9 +160,15 @@ void ExecutionPath::checkScope(const Token *tok, std::list &che return; } - if (tok->str() == "}" || tok->str() == "break") + if (tok->str() == "}") return; + if (tok->str() == "break") + { + ExecutionPath::bailOut(checks); + return; + } + if (Token::simpleMatch(tok, "while (")) { // parse condition diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 9db1d8525..170e975a9 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -837,6 +837,17 @@ private: " return x;\n" "}"); ASSERT_EQUALS("[test.cpp:11]: (error) Uninitialized variable: x\n", errout.str()); + + // Ticket #2796 + checkUninitVar("void foo() {\n" + " while (true) {\n" + " int x;\n" + " if (y) x = 0;\n" + " else break;\n" + " return x;\n" // <- x is initialized + " }\n" + "}"); + ASSERT_EQUALS("", errout.str()); } // switch..