Fixed false positives about uninitialized member variables if variable has a default value (#5500)

This commit is contained in:
Frank Zingsheim 2014-10-16 21:57:43 +02:00 committed by PKEuS
parent 3923618b8d
commit 41a54fceba
4 changed files with 27 additions and 2 deletions

View File

@ -1128,7 +1128,7 @@ void CheckUninitVar::checkStruct(const Scope* scope, const Token *tok, const Var
if (scope2->className == structname && scope2->numConstructors == 0U) { if (scope2->className == structname && scope2->numConstructors == 0U) {
for (std::list<Variable>::const_iterator it = scope2->varlist.begin(); it != scope2->varlist.end(); ++it) { for (std::list<Variable>::const_iterator it = scope2->varlist.begin(); it != scope2->varlist.end(); ++it) {
const Variable &var = *it; const Variable &var = *it;
if (!var.isArray()) { if (!var.hasDefault() && !var.isArray()) {
// is the variable declared in a inner union? // is the variable declared in a inner union?
bool innerunion = false; bool innerunion = false;
for (std::list<Scope>::const_iterator it2 = symbolDatabase->scopeList.begin(); it2 != symbolDatabase->scopeList.end(); ++it2) { for (std::list<Scope>::const_iterator it2 = symbolDatabase->scopeList.begin(); it2 != symbolDatabase->scopeList.end(); ++it2) {

View File

@ -912,7 +912,7 @@ SymbolDatabase::SymbolDatabase(const Tokenizer *tokenizer, const Settings *setti
else if (var->type()->needInitialization == Type::Unknown) else if (var->type()->needInitialization == Type::Unknown)
unknown = true; unknown = true;
} }
} else } else if (!var->hasDefault())
needInitialization = true; needInitialization = true;
} }

View File

@ -2427,6 +2427,22 @@ private:
" { }\n" " { }\n"
"};"); "};");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
// non static data-member initialization
check("struct POINT\n"
"{\n"
" int x=0;\n"
" int y=0;\n"
"};\n"
"class Fred\n"
"{\n"
"private:\n"
" POINT p;\n"
"public:\n"
" Fred()\n"
" { }\n"
"};");
ASSERT_EQUALS("", errout.str());
} }
void uninitVarUnion1() { // ticket #3196 void uninitVarUnion1() { // ticket #3196

View File

@ -3203,6 +3203,15 @@ private:
" int *p = ab.a;\n" " int *p = ab.a;\n"
"}"); "}");
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
// non static data-member initialization
checkUninitVar2("struct AB { int a=1; int b; };\n"
"void f(void) {\n"
" struct AB ab;\n"
" int a = ab.a;\n"
" int b = ab.b;\n"
"}");
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized struct member: ab.b\n", errout.str());
} }
void uninitvar2_while() { void uninitvar2_while() {