Fixed #4383 (Improve check: uninitialized member variable not detected when initialization is not proper)

This commit is contained in:
Robert Reif 2012-12-21 19:36:45 +01:00 committed by Daniel Marjamäki
parent 2050cd71db
commit 80848c6e0e
2 changed files with 15 additions and 3 deletions

View File

@ -364,9 +364,10 @@ void CheckClass::initializeVarList(const Function &func, std::list<std::string>
// Class constructor.. initializing variables like this
// clKalle::clKalle() : var(value) { }
if (initList) {
if (level == 0 && Token::Match(ftok, "%var% ("))
if (level == 0 && Token::Match(ftok, "%var% (")) {
if (ftok->strAt(2) != ")")
initVar(ftok->str(), scope, usage);
else if (level != 0 && Token::Match(ftok, "%var% =")) // assignment in the initializer: var(value = x)
} else if (level != 0 && Token::Match(ftok, "%var% =")) // assignment in the initializer: var(value = x)
assignVar(ftok->str(), scope, usage);
else if (ftok->str() == "(")

View File

@ -109,6 +109,7 @@ private:
TEST_CASE(uninitVar22); // ticket #3043
TEST_CASE(uninitVar23); // ticket #3702
TEST_CASE(uninitVar24); // ticket #3190
TEST_CASE(uninitVar25); // ticket #4383
TEST_CASE(uninitVarEnum);
TEST_CASE(uninitVarStream);
TEST_CASE(uninitVarTypedef);
@ -1616,6 +1617,16 @@ private:
"[test.cpp:12]: (warning) Member variable 'Sub::b' is not initialized in the constructor.\n", errout.str());
}
void uninitVar25() { // ticket #4383
check("class Fred {\n"
" bool b;\n"
"public:\n"
" Fred() : b() { }\n"
"};\n");
ASSERT_EQUALS("[test.cpp:4]: (warning) Member variable 'Fred::b' is not initialized in the constructor.\n" , errout.str());
}
void uninitVarArray1() {
check("class John\n"
"{\n"