diff --git a/lib/checkother.cpp b/lib/checkother.cpp index fb59cc0da..be8fb2f80 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -983,7 +983,20 @@ void CheckOther::functionVariableUsage() if (tok->tokAt(4)->varId() != 0) variables.read(tok->tokAt(4)->varId()); - tok = tok->tokAt(5); + // look at initializers + if (Token::Match(tok->tokAt(6), "= {")) + { + tok = tok->tokAt(8); + while (tok && tok->str() != "}") + { + if (Token::Match(tok, "%var%")) + variables.read(tok->varId()); + tok = tok->next(); + } + tok = tok->next(); + } + else + tok = tok->tokAt(5); } // pointer or reference declaration with possible initialization @@ -1310,11 +1323,13 @@ void CheckOther::functionVariableUsage() // function parameter else if (Token::Match(tok, "[(,] %var% [")) variables.use(tok->next()->varId()); // use = read + write - - // function parameter else if (Token::Match(tok, "[(,] %var% [,)]") && tok->previous()->str() != "*") variables.use(tok->next()->varId()); // use = read + write + // function + else if (Token::Match(tok, " %var% (")) + variables.read(tok->varId()); + else if (Token::Match(tok, " %var% .")) variables.use(tok->varId()); // use = read + write diff --git a/test/testunusedvar.cpp b/test/testunusedvar.cpp index 67fb19c9f..e20245349 100644 --- a/test/testunusedvar.cpp +++ b/test/testunusedvar.cpp @@ -65,6 +65,7 @@ private: TEST_CASE(localvar17); // ticket #1720 TEST_CASE(localvar18); // ticket #1723 TEST_CASE(localvar19); // ticket #1776 + TEST_CASE(localvar20); // ticket #1799 TEST_CASE(localvaralias1); TEST_CASE(localvaralias2); // ticket #1637 TEST_CASE(localvaralias3); // ticket #1639 @@ -89,6 +90,7 @@ private: TEST_CASE(localvarCast); TEST_CASE(localvarClass); TEST_CASE(localvarUnused); + TEST_CASE(localvarFunction); // ticket #1799 } void checkStructMemberUsage(const char code[]) @@ -1164,6 +1166,17 @@ private: "[test.cpp:3]: (style) Variable 'c' is assigned a value that is never used\n", errout.str()); } + void localvar20() // ticket #1799 + { + functionVariableUsage("void foo()\n" + "{\n" + " char c1 = 'c';\n" + " char c2[] = { c1 };\n" + " a(c2);\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } + void localvaralias1() { functionVariableUsage("void foo()\n" @@ -1921,6 +1934,18 @@ private: "}\n"); ASSERT_EQUALS("", errout.str()); } + + void localvarFunction() + { + functionVariableUsage("void check_dlsym(void*& h)\n" + "{\n" + " typedef void (*function_type) (void);\n" + " function_type fn;\n" + " fn = reinterpret_cast(dlsym(h, \"try_allocation\"));\n" + " fn();\n" + "}"); + ASSERT_EQUALS("", errout.str()); + } }; REGISTER_TEST(TestUnusedVar)