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()); + } };