From 121c0ee58f9c2bcbc44060c69f78937aca94909f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Sat, 17 Jan 2009 11:12:15 +0000 Subject: [PATCH] unused variable: reading the value of variable in a for loop.. "for(;a;)" see ticket #18 --- src/checkother.cpp | 3 +++ test/testunusedvar.cpp | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/src/checkother.cpp b/src/checkother.cpp index 66ddc2ca7..98c84d204 100644 --- a/src/checkother.cpp +++ b/src/checkother.cpp @@ -927,6 +927,9 @@ void CheckOther::functionVariableUsage() if (Token::Match(tok, "[(,] %var% [,)]")) varUsage[ tok->strAt(1)] |= USAGE_WRITE; + + if (Token::Match(tok, "; %var% ;")) + varUsage[ tok->strAt(1)] |= USAGE_READ; } // Check usage of all variables in the current scope.. diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index 87946d389..1b531ba91 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -75,6 +75,7 @@ private: TEST_CASE(localvarIf); // Usage in if TEST_CASE(localvarIfElse); // return tmp1 ? tmp2 : tmp3; TEST_CASE(localvarOpAssign); // a |= b; + TEST_CASE(localvarFor); // for ( ; var; ) } void structmember1() @@ -335,6 +336,15 @@ private: ASSERT_EQUALS(std::string("[test.cpp:2]: Variable 'a' is assigned a value that is never used\n"), errout.str()); } + void localvarFor() + { + functionVariableUsage("void foo()\n" + "{\n" + " int a = 1;\n" + " for (;a;);\n" + "}\n"); + ASSERT_EQUALS(std::string(""), errout.str()); + } };