From 87325799c5ba0d8208df91b9c33e85d609f93cf0 Mon Sep 17 00:00:00 2001 From: Reijo Tomperi Date: Tue, 8 Dec 2009 23:41:47 +0200 Subject: [PATCH] Fix #1071 ((style) The function 'throw' is never used) http://sourceforge.net/apps/trac/cppcheck/ticket/1071 --- lib/checkunusedfunctions.cpp | 9 ++++++++- test/testunusedfunctions.cpp | 27 +++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/lib/checkunusedfunctions.cpp b/lib/checkunusedfunctions.cpp index 8e6ee9e2e..33b4b4370 100644 --- a/lib/checkunusedfunctions.cpp +++ b/lib/checkunusedfunctions.cpp @@ -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; } diff --git a/test/testunusedfunctions.cpp b/test/testunusedfunctions.cpp index 1f8c51966..6cf409ea2 100644 --- a/test/testunusedfunctions.cpp +++ b/test/testunusedfunctions.cpp @@ -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)