Fixed #4072 (False positive: Structure is not initialized in the constructor (1.55))

This commit is contained in:
Daniel Marjamäki 2012-09-23 18:29:05 +02:00
parent 68240fffc6
commit 990340ba98
2 changed files with 27 additions and 0 deletions

View File

@ -536,6 +536,18 @@ void CheckClass::initializeVarList(const Function &func, std::list<std::string>
callstack.push_back(ftok->str());
initializeVarList(*it, callstack, scope, usage);
callstack.pop_back();
// Assume that variables that are passed to it are initialized..
for (const Token *tok2 = ftok; tok2; tok2 = tok2->next()) {
if (Token::Match(tok2, "[;{}]"))
break;
if (Token::Match(tok2, "[(,] &| %var% [,)]")) {
tok2 = tok2->next();
if (tok2->str() == "&")
tok2 = tok2->next();
assignVar(tok2->str(), scope, usage);
}
}
}
// there is a called member function, but it has no implementation, so we assume it initializes everything

View File

@ -139,6 +139,7 @@ private:
TEST_CASE(uninitFunction2); // No FP when initialized in function
TEST_CASE(uninitFunction3); // No FP when initialized in function
TEST_CASE(uninitFunction4);
TEST_CASE(uninitFunction5);
TEST_CASE(uninitSameClassName); // No FP when two classes have the same name
TEST_CASE(uninitFunctionOverload); // No FP when there are overloaded functions
TEST_CASE(uninitJava); // Java: no FP when variable is initialized in declaration
@ -2207,6 +2208,20 @@ private:
TODO_ASSERT_EQUALS("[test.cpp:4]: (warning) Member variable 'Fred::d' is not initialized in the constructor.\n", "", errout.str());
}
void uninitFunction5() { // #4072 - FP about struct that is initialized in function
check("struct Structure {\n"
" int C;\n"
"};\n"
"\n"
"class A {\n"
" Structure B;\n"
"public:\n"
" A() { Init( B ); };\n"
" void Init( Structure& S ) { S.C = 0; };\n"
"};");
ASSERT_EQUALS("", errout.str());
}
void uninitSameClassName() {
check("class B\n"
"{\n"