Fixed #6646 (False positive uninitvar - loopvariable initialized inside loop)

This commit is contained in:
Daniel Marjamäki 2016-08-04 14:39:54 +02:00
parent 0bec604627
commit 9711064b74
2 changed files with 17 additions and 0 deletions

View File

@ -101,6 +101,10 @@ void CheckUninitVar::checkScope(const Scope* scope)
if (!tok)
continue;
if (tok->astParent() && Token::simpleMatch(tok->astParent()->previous(), "for (") &&
checkLoopBody(tok->astParent()->link()->next(), *i, i->isArray() ? ARRAY : NO_ALLOC, "", true))
continue;
if (i->isArray()) {
Alloc alloc = ARRAY;
checkScopeForVariable(tok, *i, nullptr, nullptr, &alloc, "");

View File

@ -3541,6 +3541,19 @@ private:
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
// #6646 - init in for loop
checkUninitVar("void f() {\n" // No FP
" for (int i;;i++)\n"
" dostuff(&i);\n"
"}");
ASSERT_EQUALS("", errout.str());
checkUninitVar("void f() {\n" // No FN
" for (int i;;i++)\n"
" a=i;\n"
"}");
ASSERT_EQUALS("[test.cpp:2]: (error) Uninitialized variable: i\n", errout.str());
}
void uninitvar2_4494() {