Fix #1071 ((style) The function 'throw' is never used)

http://sourceforge.net/apps/trac/cppcheck/ticket/1071
This commit is contained in:
Reijo Tomperi 2009-12-08 23:41:47 +02:00
parent d08d1deab1
commit 87325799c5
2 changed files with 35 additions and 1 deletions

View File

@ -69,12 +69,19 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer)
else if (Token::Match(tok, "%type% :: %var% (") && !Token::Match(tok, tok->strAt(2)))
funcname = tok->tokAt(2);
// Don't assume throw as a function name: void foo() throw () {}
if (Token::Match(tok->previous(), ")|const"))
funcname = 0;
// Check that ") {" is found..
for (const Token *tok2 = funcname; tok2; tok2 = tok2->next())
{
if (tok2->str() == ")")
{
if (! Token::simpleMatch(tok2, ") {") && ! Token::simpleMatch(tok2, ") const {"))
if (! Token::simpleMatch(tok2, ") {") &&
! Token::simpleMatch(tok2, ") const {") &&
! Token::simpleMatch(tok2, ") const throw ( ) {") &&
! Token::simpleMatch(tok2, ") throw ( ) {"))
funcname = NULL;
break;
}

View File

@ -42,6 +42,8 @@ private:
TEST_CASE(else1);
TEST_CASE(functionpointer);
TEST_CASE(template1);
TEST_CASE(throwIsNotAFunction);
TEST_CASE(unusedError);
}
void check(const char code[])
@ -143,6 +145,31 @@ private:
"}\n");
ASSERT_EQUALS("", errout.str());
}
void throwIsNotAFunction()
{
check("struct A {void f() const throw () {}}; int main() {A a; a.f();}\n");
ASSERT_EQUALS("", errout.str());
}
void unusedError()
{
check("void foo() {}\n"
"int main()\n");
ASSERT_EQUALS("[test.cpp:1]: (style) The function 'foo' is never used\n", errout.str());
check("void foo() const {}\n"
"int main()\n");
ASSERT_EQUALS("[test.cpp:1]: (style) The function 'foo' is never used\n", errout.str());
check("void foo() const throw() {}\n"
"int main()\n");
ASSERT_EQUALS("[test.cpp:1]: (style) The function 'foo' is never used\n", errout.str());
check("void foo() throw() {}\n"
"int main()\n");
ASSERT_EQUALS("[test.cpp:1]: (style) The function 'foo' is never used\n", errout.str());
}
};
REGISTER_TEST(TestUnusedFunctions)