From 235404077ff478516874f65c02492695722476ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Tue, 16 Mar 2010 19:25:10 +0100 Subject: [PATCH] Fixed #1501 (false positive: Variable 'n' is assigned a value that is never used) --- lib/checkother.cpp | 4 ++++ test/testunusedvar.cpp | 13 +++++++++++++ 2 files changed, 17 insertions(+) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index bd1294a6e..ad72e0c08 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -535,6 +535,10 @@ void CheckOther::functionVariableUsage() else if (Token::Match(tok, "; %var% ;")) varUsage[ tok->strAt(1)].read = true; + + else if (Token::Match(tok, "++|-- %var%") || + Token::Match(tok, "%var% ++|--")) + varUsage[tok->strAt(1)].use(); } // Check usage of all variables in the current scope.. diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index eb616e88f..cd72bf21a 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -64,6 +64,7 @@ private: TEST_CASE(localvar3); TEST_CASE(localvar4); TEST_CASE(localvar5); + TEST_CASE(localvar6); // Don't give false positives for variables in structs/unions TEST_CASE(localvarStruct1); @@ -273,6 +274,18 @@ private: ASSERT_EQUALS(std::string(""), errout.str()); } + void localvar6() + { + functionVariableUsage("void foo()\n" + "{\n" + " int a = 0;\n" + " int b[10];\n" + " for (int i=0;i<10;++i)\n" + " b[i] = ++a;\n" + "}\n"); + ASSERT_EQUALS(std::string(""), errout.str()); + } + void localvarStruct1()