Fixed #6455: Show uninitvar even for const variables

This commit is contained in:
PKEuS 2015-01-31 13:21:22 +01:00
parent 27924d4ae2
commit 789665dadf
2 changed files with 9 additions and 2 deletions

View File

@ -411,7 +411,7 @@ private:
// Variable declaration.. // Variable declaration..
if (Token::Match(&tok, "%var% [[;]")) { if (Token::Match(&tok, "%var% [[;]")) {
const Variable* var2 = tok.variable(); const Variable* var2 = tok.variable();
if (var2 && var2->nameToken() == &tok && !var2->isStatic() && !var2->isExtern() && !var2->isConst() && !Token::simpleMatch(tok.linkAt(1), "] [")) { if (var2 && var2->nameToken() == &tok && !var2->isStatic() && !var2->isExtern() && !Token::simpleMatch(tok.linkAt(1), "] [")) {
if (tok.linkAt(1)) { // array if (tok.linkAt(1)) { // array
const Token* endtok = tok.next(); const Token* endtok = tok.next();
while (endtok->link()) while (endtok->link())
@ -1058,7 +1058,7 @@ void CheckUninitVar::checkScope(const Scope* scope)
{ {
for (std::list<Variable>::const_iterator i = scope->varlist.begin(); i != scope->varlist.end(); ++i) { for (std::list<Variable>::const_iterator i = scope->varlist.begin(); i != scope->varlist.end(); ++i) {
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->isArray() || i->isReference())
continue; continue;
// don't warn for try/catch exception variable // don't warn for try/catch exception variable

View File

@ -155,6 +155,13 @@ private:
"}"); "}");
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str()); ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str());
// Ticket #6455 - some compilers allow const variables to be uninitialized
checkUninitVar("void foo() {\n"
" const int a;\n"
" b = c - a;\n"
"}");
ASSERT_EQUALS("[test.cpp:3]: (error) Uninitialized variable: a\n", errout.str());
checkUninitVar("void foo() {\n" checkUninitVar("void foo() {\n"
" int *p;\n" " int *p;\n"
" realloc(p,10);\n" " realloc(p,10);\n"