Uninitialized variables; Fix false positive in switch inside loop

This commit is contained in:
Daniel Marjamäki 2021-05-19 13:06:44 +02:00
parent c70b8793a3
commit abe810d718
2 changed files with 22 additions and 0 deletions

View File

@ -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 {")) {

View File

@ -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..