Fixed #1851 (false positive: Member variable not initialized int ctor)

This commit is contained in:
Daniel Marjamäki 2010-07-14 18:50:29 +02:00
parent d7f971c347
commit a6b6022497
2 changed files with 35 additions and 16 deletions

View File

@ -275,10 +275,13 @@ void CheckClass::initializeVarList(const Token *tok1, const Token *ftok, Var *va
initVar(varlist, ftok->strAt(1));
}
// Before a new statement there is "[{};)=]" or "else"
if (! Token::Match(ftok, "[{};()=]") && ftok->str() != "else")
// Before a new statement there is "[{};)=]"
if (! Token::Match(ftok, "[{};()=]"))
continue;
if (Token::simpleMatch(ftok, "( !"))
ftok = ftok->next();
// Using the operator= function to initialize all variables..
if (Token::simpleMatch(ftok->next(), "* this = "))
{

View File

@ -75,6 +75,7 @@ private:
TEST_CASE(uninitOperator); // No FP about uninitialized 'operator[]'
TEST_CASE(uninitFunction1); // No FP when initialized in function
TEST_CASE(uninitFunction2); // No FP when initialized in function
TEST_CASE(uninitFunction3); // No FP when initialized in function
TEST_CASE(uninitSameClassName); // No FP when two classes have the same name
TEST_CASE(uninitFunctionOverload); // No FP when there are overloaded functions
@ -2002,6 +2003,21 @@ private:
ASSERT_EQUALS("", errout.str());
}
void uninitFunction3()
{
checkUninitVar("class Fred\n"
"{\n"
"public:\n"
" Fred() { if (!init()); }\n"
"\n"
" bool init()\n"
" { d = 0; return true; }\n"
"\n"
" double d;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void uninitSameClassName()
{
checkUninitVar("class B\n"