Fix ticket #613 (array members variables are not checked in constructor)

http://sourceforge.net/apps/trac/cppcheck/ticket/613
Patch from micres
This commit is contained in:
Reijo Tomperi 2009-08-23 22:54:41 +03:00
parent 522f4c18c8
commit c581753040
2 changed files with 36 additions and 0 deletions

View File

@ -109,6 +109,11 @@ CheckClass::Var *CheckClass::getVarList(const Token *tok1, bool withClasses)
varname = next->strAt(3);
}
else if (Token::Match(next, "%type% %var% ["))
{
varname = next->strAt(1);
}
// std::string..
else if (withClasses && Token::Match(next, "std :: string %var% ;"))
{
@ -243,6 +248,12 @@ void CheckClass::initializeVarList(const Token *tok1, const Token *ftok, Var *va
initVar(varlist, ftok->str().c_str());
}
// Assignment of array item of member variable?
else if (Token::Match(ftok, "%var% [ %any% ] ="))
{
initVar(varlist, ftok->str().c_str());
}
// The functions 'clear' and 'Clear' are supposed to initialize variable.
if (Token::Match(ftok, "%var% . clear|Clear ("))
{

View File

@ -48,6 +48,7 @@ private:
TEST_CASE(uninitVarEnum);
TEST_CASE(uninitVarStream);
TEST_CASE(uninitVarTypedef);
TEST_CASE(uninitVarArray);
TEST_CASE(privateCtor1); // If constructor is private..
TEST_CASE(privateCtor2); // If constructor is private..
TEST_CASE(function); // Function is not variable
@ -392,6 +393,30 @@ private:
ASSERT_EQUALS("", errout.str());
}
void uninitVarArray()
{
checkUninitVar("class John\n"
"{\n"
"public:\n"
" John() {}\n"
"\n"
"private:\n"
" char name[255];\n"
"};\n");
ASSERT_EQUALS("[test.cpp:4]: (style) Member variable not initialized in the constructor 'John::name'\n", errout.str());
checkUninitVar("class John\n"
"{\n"
"public:\n"
" John() {John::name[0] = '\0';}\n"
"\n"
"private:\n"
" char name[255];\n"
"};\n");
ASSERT_EQUALS("", errout.str());
}
void uninitVarEnum()
{
checkUninitVar("class Fred\n"