From 61b5a21ac900038c101356dc10049c0b299682e4 Mon Sep 17 00:00:00 2001 From: Robert Reif Date: Fri, 16 Apr 2010 16:25:57 +0200 Subject: [PATCH] Fixed #1605 (false positive: uninitialized variable) --- lib/checkother.cpp | 2 +- test/testunusedvar.cpp | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 4f50b774b..5575a5bc6 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -660,7 +660,7 @@ void CheckOther::functionVariableUsage() else if (Token::Match(tok, "%var% =")) varUsage[tok->str()].write = true; - else if (Token::Match(tok, "%var% [ %any% ] =")) + else if (Token::Match(tok, "%var% [") && Token::Match(tok->next()->link(), "] =")) varUsage[tok->str()].write = true; else if (Token::Match(tok, "else %var% =")) diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index 812f53224..6927ded00 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -69,6 +69,7 @@ private: TEST_CASE(localvar6); TEST_CASE(localvar7); TEST_CASE(localvar8); + TEST_CASE(localvar9); // ticket #1605 TEST_CASE(localvarasm); // Don't give false positives for variables in structs/unions @@ -525,6 +526,18 @@ private: ASSERT_EQUALS("", errout.str()); } + void localvar9() + { + // ticket #1605 + functionVariableUsage("void foo()\n" + "{\n" + " int a[10];\n" + " for (int i = 0; i < 10; )\n" + " a[i++] = 0;\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:2]: (style) Variable 'a' is assigned a value that is never used\n", errout.str()); + } + void localvarasm() { functionVariableUsage("void foo(int &b)\n"