From 2ae48c7aef90ff9dbe51e0fa64c418b81380153f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Fri, 30 Dec 2011 09:47:15 +0100 Subject: [PATCH] Fixed #3454 (false positive: (style) Variable 'iFaktor' is assigned a value that is never used) --- lib/checkunusedvar.cpp | 7 ++----- test/testunusedvar.cpp | 9 +++++++++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index 75b66db0a..6141486ce 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -662,20 +662,17 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const break; } - if (Token::Match(tok, "%type% const| *|&| const| *| const| %var% ;|[|,|)|=|(") && tok->str() != "return" && tok->str() != "throw") { // Declaration: Skip - const Token* old = tok; + if (Token::Match(tok, "%type% const| *|&| const| *| const| %var% [;=[(]") && tok->str() != "return" && tok->str() != "throw") { // Declaration: Skip tok = tok->next(); while (Token::Match(tok, "const|*|&")) tok = tok->next(); - tok = Token::findmatch(tok, "[,;)=(]"); + tok = Token::findmatch(tok, "[;=[(]"); if (tok && Token::Match(tok, "( %var% )")) // Simple initialization through copy ctor tok = tok->next(); else if (tok && Token::Match(tok, "= %var% ;")) // Simple initialization tok = tok->next(); if (!tok) break; - if (tok->str() == ")" && tok->link() && Token::Match(tok->link()->previous(), "if|for|while")) - tok = old; } // Freeing memory (not considered "using" the pointer if it was also allocated in this function) if (Token::Match(tok, "free|g_free|kfree|vfree ( %var% )") || diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index fc6ddd487..b89494d00 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -84,6 +84,7 @@ private: TEST_CASE(localvar36); // ticket #2805 TEST_CASE(localvar37); // ticket #3078 TEST_CASE(localvar38); + TEST_CASE(localvar39); // ticket #3454 TEST_CASE(localvaralias1); TEST_CASE(localvaralias2); // ticket #1637 TEST_CASE(localvaralias3); // ticket #1639 @@ -1371,6 +1372,14 @@ private: ASSERT_EQUALS("", errout.str()); } + void localvar39() { + functionVariableUsage("void f() {\n" + " int a = 1;\n" + " foo(x*a);\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + void localvaralias1() { functionVariableUsage("void foo()\n" "{\n"