Fixed #1799 (false positive: Variable is assigned a value that is never used)
This commit is contained in:
parent
26e167fd5b
commit
bdd28b47aa
|
@ -983,7 +983,20 @@ void CheckOther::functionVariableUsage()
|
||||||
if (tok->tokAt(4)->varId() != 0)
|
if (tok->tokAt(4)->varId() != 0)
|
||||||
variables.read(tok->tokAt(4)->varId());
|
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
|
// pointer or reference declaration with possible initialization
|
||||||
|
@ -1310,11 +1323,13 @@ void CheckOther::functionVariableUsage()
|
||||||
// function parameter
|
// function parameter
|
||||||
else if (Token::Match(tok, "[(,] %var% ["))
|
else if (Token::Match(tok, "[(,] %var% ["))
|
||||||
variables.use(tok->next()->varId()); // use = read + write
|
variables.use(tok->next()->varId()); // use = read + write
|
||||||
|
|
||||||
// function parameter
|
|
||||||
else if (Token::Match(tok, "[(,] %var% [,)]") && tok->previous()->str() != "*")
|
else if (Token::Match(tok, "[(,] %var% [,)]") && tok->previous()->str() != "*")
|
||||||
variables.use(tok->next()->varId()); // use = read + write
|
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% ."))
|
else if (Token::Match(tok, " %var% ."))
|
||||||
variables.use(tok->varId()); // use = read + write
|
variables.use(tok->varId()); // use = read + write
|
||||||
|
|
||||||
|
|
|
@ -65,6 +65,7 @@ private:
|
||||||
TEST_CASE(localvar17); // ticket #1720
|
TEST_CASE(localvar17); // ticket #1720
|
||||||
TEST_CASE(localvar18); // ticket #1723
|
TEST_CASE(localvar18); // ticket #1723
|
||||||
TEST_CASE(localvar19); // ticket #1776
|
TEST_CASE(localvar19); // ticket #1776
|
||||||
|
TEST_CASE(localvar20); // ticket #1799
|
||||||
TEST_CASE(localvaralias1);
|
TEST_CASE(localvaralias1);
|
||||||
TEST_CASE(localvaralias2); // ticket #1637
|
TEST_CASE(localvaralias2); // ticket #1637
|
||||||
TEST_CASE(localvaralias3); // ticket #1639
|
TEST_CASE(localvaralias3); // ticket #1639
|
||||||
|
@ -89,6 +90,7 @@ private:
|
||||||
TEST_CASE(localvarCast);
|
TEST_CASE(localvarCast);
|
||||||
TEST_CASE(localvarClass);
|
TEST_CASE(localvarClass);
|
||||||
TEST_CASE(localvarUnused);
|
TEST_CASE(localvarUnused);
|
||||||
|
TEST_CASE(localvarFunction); // ticket #1799
|
||||||
}
|
}
|
||||||
|
|
||||||
void checkStructMemberUsage(const char code[])
|
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());
|
"[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()
|
void localvaralias1()
|
||||||
{
|
{
|
||||||
functionVariableUsage("void foo()\n"
|
functionVariableUsage("void foo()\n"
|
||||||
|
@ -1921,6 +1934,18 @@ private:
|
||||||
"}\n");
|
"}\n");
|
||||||
ASSERT_EQUALS("", errout.str());
|
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<function_type>(dlsym(h, \"try_allocation\"));\n"
|
||||||
|
" fn();\n"
|
||||||
|
"}");
|
||||||
|
ASSERT_EQUALS("", errout.str());
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
REGISTER_TEST(TestUnusedVar)
|
REGISTER_TEST(TestUnusedVar)
|
||||||
|
|
Loading…
Reference in New Issue