From c5da030674dc91cb3e75f66b75874b185a10acb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 8 Jun 2012 16:17:55 +0200 Subject: [PATCH] Fixed #3869 (References to items in array result it 'Uninitialized variable' errors) --- lib/checkuninitvar.cpp | 10 +++++----- test/testuninitvar.cpp | 12 ++++++++++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/checkuninitvar.cpp b/lib/checkuninitvar.cpp index 1b9996bf5..0cab834a9 100644 --- a/lib/checkuninitvar.cpp +++ b/lib/checkuninitvar.cpp @@ -390,10 +390,10 @@ private: const Variable* var2 = symbolDatabase->getVariableFromVarId(tok.varId()); if (var2 && var2->nameToken() == &tok && !var2->isStatic() && !var2->isConst()) { if (tok.linkAt(1)) { // array - const Token* end = tok.next(); - while (end->link()) - end = end->link()->next(); - if (end->str() != ";") + const Token* endtok = tok.next(); + while (endtok->link()) + endtok = endtok->link()->next(); + if (endtok->str() != ";") return &tok; } const Scope* parent = var2->scope()->nestedIn; @@ -1036,7 +1036,7 @@ void CheckUninitVar::check() void CheckUninitVar::checkScope(const Scope* scope) { for (std::list::const_iterator i = scope->varlist.begin(); i != scope->varlist.end(); ++i) { - if ((i->type() && !i->isPointer()) || i->isStatic() || i->isConst() || i->isArray()) + if ((i->type() && !i->isPointer()) || i->isStatic() || i->isConst() || i->isArray() || i->isReference()) continue; if (i->nameToken()->strAt(1) == "(") continue; diff --git a/test/testuninitvar.cpp b/test/testuninitvar.cpp index 2fe51c2a8..3e397ee38 100644 --- a/test/testuninitvar.cpp +++ b/test/testuninitvar.cpp @@ -53,6 +53,7 @@ private: // ExecutionPath functionality TEST_CASE(uninitvar2); TEST_CASE(uninitvar3); // #3844 + TEST_CASE(uninitvar4); // #3869 (reference) } void checkUninitVar(const char code[]) { @@ -2149,6 +2150,17 @@ private: " )\n" "}\n"); } + + // #3869 - reference variable + void uninitvar4() { + checkUninitVar2("void f() {\n" + " int buf[10];\n" + " int &x = buf[0];\n" + " buf[0] = 0;\n" + " x++;\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } }; REGISTER_TEST(TestUninitVar)