Fixed false positives in CheckUninitVar::checkStruct()
This commit is contained in:
parent
bd7765e008
commit
346532d312
|
@ -1126,29 +1126,31 @@ void CheckUninitVar::checkStruct(const Scope* scope, const Token *tok, const Var
|
|||
if (scope2->className == structname && scope2->numConstructors == 0U) {
|
||||
for (std::list<Variable>::const_iterator it = scope2->varlist.begin(); it != scope2->varlist.end(); ++it) {
|
||||
const Variable &var = *it;
|
||||
if (!var.hasDefault() && !var.isArray()) {
|
||||
// is the variable declared in a inner union?
|
||||
bool innerunion = false;
|
||||
for (std::list<Scope>::const_iterator it2 = symbolDatabase->scopeList.begin(); it2 != symbolDatabase->scopeList.end(); ++it2) {
|
||||
const Scope &innerScope = *it2;
|
||||
if (innerScope.type == Scope::eUnion && innerScope.nestedIn == scope2) {
|
||||
if (var.typeStartToken()->linenr() >= innerScope.classStart->linenr() &&
|
||||
var.typeStartToken()->linenr() <= innerScope.classEnd->linenr()) {
|
||||
innerunion = true;
|
||||
break;
|
||||
}
|
||||
|
||||
if (var.hasDefault() || var.isArray() || (!_tokenizer->isC() && var.isClass() && (!var.type() || var.type()->needInitialization != Type::True)))
|
||||
continue;
|
||||
|
||||
// is the variable declared in a inner union?
|
||||
bool innerunion = false;
|
||||
for (std::list<Scope>::const_iterator it2 = symbolDatabase->scopeList.begin(); it2 != symbolDatabase->scopeList.end(); ++it2) {
|
||||
const Scope &innerScope = *it2;
|
||||
if (innerScope.type == Scope::eUnion && innerScope.nestedIn == scope2) {
|
||||
if (var.typeStartToken()->linenr() >= innerScope.classStart->linenr() &&
|
||||
var.typeStartToken()->linenr() <= innerScope.classEnd->linenr()) {
|
||||
innerunion = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!innerunion) {
|
||||
bool alloc = false;
|
||||
const Token *tok2 = tok;
|
||||
if (tok->str() == "}")
|
||||
tok2 = tok2->next();
|
||||
checkScopeForVariable(scope, tok2, structvar, nullptr, nullptr, &alloc, var.name());
|
||||
}
|
||||
}
|
||||
|
||||
if (!innerunion) {
|
||||
bool alloc = false;
|
||||
const Token *tok2 = tok;
|
||||
if (tok->str() == "}")
|
||||
tok2 = tok2->next();
|
||||
checkScopeForVariable(scope, tok2, structvar, nullptr, nullptr, &alloc, var.name());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3301,6 +3301,51 @@ private:
|
|||
" int b = ab.b;\n"
|
||||
"}");
|
||||
ASSERT_EQUALS("[test.cpp:5]: (error) Uninitialized struct member: ab.b\n", errout.str());
|
||||
|
||||
// STL class member
|
||||
checkUninitVar2("struct A {\n"
|
||||
" std::map<int, int> m;\n"
|
||||
" int i;\n"
|
||||
"};\n"
|
||||
"void foo() {\n"
|
||||
" A a;\n"
|
||||
" x = a.m;\n"
|
||||
"}");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
// Unknown type (C++)
|
||||
checkUninitVar2("struct A {\n"
|
||||
" C m;\n"
|
||||
" int i;\n"
|
||||
"};\n"
|
||||
"void foo() {\n"
|
||||
" A a;\n"
|
||||
" x = a.m;\n"
|
||||
"}", "test.cpp");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
// Unknown type (C)
|
||||
checkUninitVar2("struct A {\n"
|
||||
" C m;\n"
|
||||
" int i;\n"
|
||||
"};\n"
|
||||
"void foo() {\n"
|
||||
" A a;\n"
|
||||
" x = a.m;\n"
|
||||
"}", "test.c");
|
||||
ASSERT_EQUALS("[test.c:7]: (error) Uninitialized struct member: a.m\n", errout.str());
|
||||
|
||||
// Type with constructor
|
||||
checkUninitVar2("class C { C(); }\n"
|
||||
"struct A {\n"
|
||||
" C m;\n"
|
||||
" int i;\n"
|
||||
"};\n"
|
||||
"void foo() {\n"
|
||||
" A a;\n"
|
||||
" x = a.m;\n"
|
||||
"}");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
}
|
||||
|
||||
void uninitvar2_while() {
|
||||
|
|
Loading…
Reference in New Issue