Refactorized CheckUninitVar::checkScope(), fixed false negative
This commit is contained in:
parent
5f36c7c914
commit
54de731cac
|
@ -1060,35 +1060,36 @@ void CheckUninitVar::checkScope(const Scope* scope)
|
||||||
if ((_tokenizer->isCPP() && i->type() && !i->isPointer() && i->type()->needInitialization != Type::True) ||
|
if ((_tokenizer->isCPP() && i->type() && !i->isPointer() && i->type()->needInitialization != Type::True) ||
|
||||||
i->isStatic() || i->isExtern() || i->isConst() || i->isArray() || i->isReference())
|
i->isStatic() || i->isExtern() || i->isConst() || i->isArray() || i->isReference())
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
// don't warn for try/catch exception variable
|
// don't warn for try/catch exception variable
|
||||||
{
|
if (i->isThrow())
|
||||||
const Token *start = i->typeStartToken();
|
continue;
|
||||||
while (start && start->isName())
|
|
||||||
start = start->previous();
|
|
||||||
if (start && Token::simpleMatch(start->previous(), "catch ("))
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (i->nameToken()->strAt(1) == "(" || i->nameToken()->strAt(1) == "{")
|
if (i->nameToken()->strAt(1) == "(" || i->nameToken()->strAt(1) == "{")
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
if (Token::Match(i->nameToken(), "%var% =")) { // Variable is initialized, but Rhs might be not
|
||||||
|
checkRhs(i->nameToken(), *i, false, "");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
bool stdtype = _tokenizer->isC();
|
bool stdtype = _tokenizer->isC();
|
||||||
const Token* tok = i->typeStartToken();
|
const Token* tok = i->typeStartToken();
|
||||||
for (; tok && tok->str() != ";" && tok->str() != "<"; tok = tok->next()) {
|
for (; tok && tok->str() != ";" && tok->str() != "<"; tok = tok->next()) {
|
||||||
if (tok->isStandardType())
|
if (tok->isStandardType())
|
||||||
stdtype = true;
|
stdtype = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (tok && tok->str() != ";")
|
while (tok && tok->str() != ";")
|
||||||
tok = tok->next();
|
tok = tok->next();
|
||||||
if (!tok)
|
if (!tok)
|
||||||
continue;
|
continue;
|
||||||
if (Token::Match(i->nameToken(), "%var% =")) {
|
|
||||||
checkRhs(i->nameToken(), *i, false, "");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (stdtype || i->isPointer()) {
|
if (stdtype || i->isPointer()) {
|
||||||
bool alloc = false;
|
bool alloc = false;
|
||||||
checkScopeForVariable(scope, tok, *i, nullptr, nullptr, &alloc, "");
|
checkScopeForVariable(scope, tok, *i, nullptr, nullptr, &alloc, "");
|
||||||
}
|
}
|
||||||
if (Token::Match(i->typeStartToken(), "struct %type% *| %var% ;"))
|
if (i->type())
|
||||||
checkStruct(scope, tok, *i);
|
checkStruct(scope, tok, *i);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2965,6 +2965,13 @@ private:
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized struct member: ab.a\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized struct member: ab.a\n", errout.str());
|
||||||
|
|
||||||
|
checkUninitVar2("struct AB { int a; int b; };\n"
|
||||||
|
"void f(void) {\n"
|
||||||
|
" AB ab;\n"
|
||||||
|
" int a = ab.a;\n"
|
||||||
|
"}");
|
||||||
|
ASSERT_EQUALS("[test.cpp:4]: (error) Uninitialized struct member: ab.a\n", errout.str());
|
||||||
|
|
||||||
checkUninitVar2("struct AB { int a; int b; };\n"
|
checkUninitVar2("struct AB { int a; int b; };\n"
|
||||||
"void f(void) {\n"
|
"void f(void) {\n"
|
||||||
" struct AB ab;\n"
|
" struct AB ab;\n"
|
||||||
|
|
Loading…
Reference in New Issue