From fd4bc12ed3d04ea2c0dcdafe2909d9b4ee2415df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Mon, 26 Dec 2011 08:12:23 +0100 Subject: [PATCH] Fixed #3438 (false positive: (style) Variable 'dBuf' is not assigned a value) --- lib/checkunusedvar.cpp | 20 +++++++++++++------- test/testunusedvar.cpp | 10 ++++++++++ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index e51c57308..75b66db0a 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -570,6 +570,15 @@ static bool isPartOfClassStructUnion(const Token* tok) return false; } +// Skip [ .. ] +static const Token * skipBrackets(const Token *tok) +{ + while (tok && tok->str() == "[") + tok = tok->link()->next(); + return tok; +} + + //--------------------------------------------------------------------------- // Usage of function variables //--------------------------------------------------------------------------- @@ -785,13 +794,10 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const } } - const Token *equal = tok->next(); - - if (Token::Match(tok->next(), "[ %any% ]")) - equal = tok->tokAt(4); + const Token *equal = skipBrackets(tok->next()); // checked for chained assignments - if (tok != start && equal->str() == "=") { + if (tok != start && equal && equal->str() == "=") { Variables::VariableUsage *var = variables.find(tok->varId()); if (var && var->_type != Variables::reference) @@ -802,14 +808,14 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const } // assignment - else if (Token::Match(tok, "%var% [") && Token::simpleMatch(tok->next()->link(), "] =")) { + else if (Token::Match(tok, "%var% [") && Token::simpleMatch(skipBrackets(tok->next()), "=")) { unsigned int varid = tok->varId(); const Variables::VariableUsage *var = variables.find(varid); if (var) { // Consider allocating memory separately because allocating/freeing alone does not constitute using the variable if (var->_type == Variables::pointer && - Token::Match(tok->next()->link(), "] = new|malloc|calloc|g_malloc|kmalloc|vmalloc")) { + Token::Match(skipBrackets(tok->next()), "= new|malloc|calloc|g_malloc|kmalloc|vmalloc")) { variables.allocateMemory(varid); } else if (var->_type == Variables::pointer || var->_type == Variables::reference) { variables.read(varid); diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index fbe2289be..fc6ddd487 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -99,6 +99,7 @@ private: TEST_CASE(localvardynamic1); TEST_CASE(localvardynamic2); // ticket #2904 TEST_CASE(localvararray1); // ticket #2780 + TEST_CASE(localvararray2); // ticket #3438 TEST_CASE(localvarstring1); TEST_CASE(localvarstring2); // ticket #2929 TEST_CASE(localvarconst); @@ -2861,6 +2862,15 @@ private: ASSERT_EQUALS("", errout.str()); } + void localvararray2() { + functionVariableUsage("int foo() {\n" + " int p[5][5];\n" + " p[0][0] = 0;\n" + " return p[0][0];\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + void localvarstring1() { // ticket #1597 functionVariableUsage("void foo() {\n" " std::string s;\n"