diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index bf8402a73..1f80810dc 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -859,6 +859,12 @@ const Token* CheckUninitVar::checkLoopBodyRecursive(const Token *start, const Va } if (tok->str() == "{") { + // switch => bailout + if (tok->scope() && tok->scope()->type == Scope::ScopeType::eSwitch) { + bailout = true; + return nullptr; + } + const Token *errorToken1 = checkLoopBodyRecursive(tok, var, alloc, membervar, bailout); tok = tok->link(); if (Token::simpleMatch(tok, "} else {")) { diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 402f926b6..044efca34 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -1330,6 +1330,22 @@ private: " }\n" "}\n"); ASSERT_EQUALS("", errout.str()); + + // switch in loop + checkUninitVar("int foo(int *p) {\n" + " int x;\n" + " while (true) {\n" + " switch (*p) {\n" + " case 1:\n" + " return x;\n" + " case 2:\n" + " x = 123;\n" + " break;\n" + " };\n" + " ++p\n" + " }\n" + "}"); + ASSERT_EQUALS("", errout.str()); } // switch..