From 304980848f71f9a75f64aa5340ee1efa40ff6cd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sun, 8 Jan 2012 08:44:18 +0100 Subject: [PATCH] Fixed #3473 (False positive: variable is assigned value that is never used (used in return statement)) --- lib/checkunusedvar.cpp | 10 ++++++++-- test/testunusedvar.cpp | 9 +++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index e6f900d92..f5c89d65e 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -696,8 +696,14 @@ void CheckUnusedVar::checkFunctionVariableUsage_iterateScopes(const Scope* const } } - else if (Token::Match(tok, "return|throw %var%")) - variables.readAll(tok->next()->varId()); + else if (Token::Match(tok, "return|throw %var%")) { + for (const Token *tok2 = tok->next(); tok2; tok2 = tok2->next()) { + if (tok2->varId()) + variables.readAll(tok2->varId()); + else if (tok2->str() == ";") + break; + } + } // assignment else if (!Token::Match(tok->tokAt(-2), "[;{}.] %var% (") && diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index 590c46924..a9c92b26b 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -85,6 +85,7 @@ private: TEST_CASE(localvar37); // ticket #3078 TEST_CASE(localvar38); TEST_CASE(localvar39); // ticket #3454 + TEST_CASE(localvar40); // ticket #3473 TEST_CASE(localvaralias1); TEST_CASE(localvaralias2); // ticket #1637 TEST_CASE(localvaralias3); // ticket #1639 @@ -1381,6 +1382,14 @@ private: ASSERT_EQUALS("", errout.str()); } + void localvar40() { + functionVariableUsage("int f() {\n" + " int a = 1;\n" + " return x & a;\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + } + void localvaralias1() { functionVariableUsage("void foo()\n" "{\n"