From d250cb5aa2dc1e43b4d27696898c5ef1300913db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 15 Nov 2010 17:53:55 +0100 Subject: [PATCH] Fixed #2206 (False positive: variable is assigned a value that is never used (array initializers)) --- lib/checkother.cpp | 48 +++++++++++++++++++++++++++--------------- test/testunusedvar.cpp | 11 ++++++++++ 2 files changed, 42 insertions(+), 17 deletions(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 9b47fc046..3628b2f89 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -1258,34 +1258,48 @@ void CheckOther::functionVariableUsage() // standard type declaration of array of with possible initialization // int i[10]; int j[2] = { 0, 1 }; static int k[2] = { 2, 3 }; - else if (Token::Match(tok, "[;{}] static| %type% %var% [ %any% ] ;|=") && + else if (Token::Match(tok, "[;{}] static| const| %type% *| %var% [ %any% ] ;|=") && nextIsStandardType(tok)) { + bool isStatic = false; + tok = tok->next(); if (tok->str() == "static") + { + tok = tok->next(); + isStatic = true; + } + + if (tok->str() == "const") tok = tok->next(); - variables.addVar(tok->tokAt(1), Variables::array, scope, - tok->tokAt(5)->str() == "=" || tok->str() == "static"); - - // check for reading array size from local variable - if (tok->tokAt(3)->varId() != 0) - variables.read(tok->tokAt(3)->varId()); - - // look at initializers - if (Token::Match(tok->tokAt(5), "= {")) + if (tok->str() != "return" && tok->str() != "throw") { - tok = tok->tokAt(7); - while (tok->str() != "}") + bool isPointer = bool(tok->strAt(1) == "*"); + const Token * const nametok = tok->tokAt(isPointer ? 2 : 1); + + variables.addVar(nametok, isPointer ? Variables::pointerArray : Variables::array, scope, + nametok->tokAt(4)->str() == "=" || isStatic); + + // check for reading array size from local variable + if (nametok->tokAt(2)->varId() != 0) + variables.read(nametok->tokAt(2)->varId()); + + // look at initializers + if (Token::Match(nametok->tokAt(4), "= {")) { - if (Token::Match(tok, "%var%")) - variables.read(tok->varId()); - tok = tok->next(); + tok = nametok->tokAt(6); + while (tok->str() != "}") + { + if (Token::Match(tok, "%var%")) + variables.read(tok->varId()); + tok = tok->next(); + } } + else + tok = nametok->tokAt(3); } - else - tok = tok->tokAt(4); } // pointer or reference declaration with possible initialization diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index a98a8994d..5f11e0db0 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -75,6 +75,7 @@ private: TEST_CASE(localvar26); // ticket #1894 TEST_CASE(localvar27); // ticket #2160 TEST_CASE(localvar28); // ticket #2205 + TEST_CASE(localvar29); // ticket #2206 (array initialization) TEST_CASE(localvaralias1); TEST_CASE(localvaralias2); // ticket #1637 TEST_CASE(localvaralias3); // ticket #1639 @@ -1293,6 +1294,16 @@ private: ASSERT_EQUALS("", errout.str()); } + void localvar29() // ticket #2206 + { + functionVariableUsage("void f() {\n" + " float s_ranges[] = { 0, 256 };\n" + " float* ranges[] = { s_ranges };\n" + " cout << ranges[0][0];\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + void localvaralias1() { functionVariableUsage("void foo()\n"