Fixed #2273 (False Positive: Member variable not initialized in the constructor)

This commit is contained in:
Robert Reif 2010-12-04 17:47:00 +01:00 committed by Daniel Marjamäki
parent a29f54c122
commit 04b811b74f
3 changed files with 52 additions and 1 deletions

View File

@ -109,7 +109,9 @@ void CheckClass::constructors()
for (func = info->functionList.begin(); func != info->functionList.end(); ++func) for (func = info->functionList.begin(); func != info->functionList.end(); ++func)
{ {
if (!func->hasBody || !(func->type == SymbolDatabase::Func::Constructor || func->type == SymbolDatabase::Func::CopyConstructor || func->type == SymbolDatabase::Func::OperatorEqual)) if (!func->hasBody || !(func->type == SymbolDatabase::Func::Constructor ||
func->type == SymbolDatabase::Func::CopyConstructor ||
func->type == SymbolDatabase::Func::OperatorEqual))
continue; continue;
// Mark all variables not used // Mark all variables not used

View File

@ -1459,8 +1459,22 @@ void SymbolDatabase::SpaceInfo::initializeVarList(const Func &func, std::list<st
break; break;
} }
// Calling member variable function?
if (Token::Match(ftok->next(), "%var% . %var% (")) if (Token::Match(ftok->next(), "%var% . %var% ("))
{
std::list<Var>::const_iterator var;
for (var = varlist.begin(); var != varlist.end(); ++var)
{
if (var->token->varId() == ftok->next()->varId())
{
/** @todo false negative: we assume function changes variable state */
assignVar(ftok->next()->str());
continue;
}
}
ftok = ftok->tokAt(2); ftok = ftok->tokAt(2);
}
if (!Token::Match(ftok->next(), "%var%") && if (!Token::Match(ftok->next(), "%var%") &&
!Token::Match(ftok->next(), "this . %var%") && !Token::Match(ftok->next(), "this . %var%") &&

View File

@ -59,6 +59,7 @@ private:
TEST_CASE(uninitVar13); // ticket #1195 TEST_CASE(uninitVar13); // ticket #1195
TEST_CASE(uninitVar14); // ticket #2149 TEST_CASE(uninitVar14); // ticket #2149
TEST_CASE(uninitVar15); TEST_CASE(uninitVar15);
TEST_CASE(uninitVar16);
TEST_CASE(uninitVarEnum); TEST_CASE(uninitVarEnum);
TEST_CASE(uninitVarStream); TEST_CASE(uninitVarStream);
TEST_CASE(uninitVarTypedef); TEST_CASE(uninitVarTypedef);
@ -1952,6 +1953,40 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void uninitVar16()
{
checkUninitVar("struct Foo\n"
"{\n"
" int a;\n"
" void set(int x) { a = x; }\n"
"};\n"
"class Bar\n"
"{\n"
" Foo foo;\n"
"public:\n"
" Bar()\n"
" {\n"
" foo.set(0);\n"
" }\n"
"};\n");
ASSERT_EQUALS("", errout.str());
checkUninitVar("struct Foo\n"
"{\n"
" int a;\n"
" void set(int x) { a = x; }\n"
"};\n"
"class Bar\n"
"{\n"
" Foo foo;\n"
"public:\n"
" Bar()\n"
" {\n"
" }\n"
"};\n");
ASSERT_EQUALS("[test.cpp:10]: (warning) Member variable not initialized in the constructor 'Bar::foo'\n", errout.str());
}
void uninitVarArray1() void uninitVarArray1()
{ {
checkUninitVar("class John\n" checkUninitVar("class John\n"