diff --git a/lib/checkunusedfunctions.cpp b/lib/checkunusedfunctions.cpp index 40217ba31..6f4f440cd 100644 --- a/lib/checkunusedfunctions.cpp +++ b/lib/checkunusedfunctions.cpp @@ -60,26 +60,23 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer) if (Token::Match(tok, "%type% %var% (")) funcname = tok->next(); - else if (Token::Match(tok, "%type% * %var% (")) + else if (Token::Match(tok, "%type% *|& %var% (")) funcname = tok->tokAt(2); else if (Token::Match(tok, "%type% :: %var% (") && !Token::Match(tok, tok->strAt(2).c_str())) funcname = tok->tokAt(2); // Don't assume throw as a function name: void foo() throw () {} - if (Token::Match(tok->previous(), ")|const")) - funcname = 0; + if (Token::Match(tok->previous(), ")|const") || funcname == 0) + continue; + + tok = funcname->linkAt(1); // Check that ") {" is found.. - for (const Token *tok2 = funcname; tok2; tok2 = tok2->next()) { - if (tok2->str() == ")") { - if (! Token::simpleMatch(tok2, ") {") && - ! Token::simpleMatch(tok2, ") const {") && - ! Token::simpleMatch(tok2, ") const throw ( ) {") && - ! Token::simpleMatch(tok2, ") throw ( ) {")) - funcname = 0; - break; - } - } + if (! Token::simpleMatch(tok, ") {") && + ! Token::simpleMatch(tok, ") const {") && + ! Token::simpleMatch(tok, ") const throw ( ) {") && + ! Token::simpleMatch(tok, ") throw ( ) {")) + funcname = 0; if (funcname) { FunctionUsage &func = _functions[ funcname->str()]; @@ -118,19 +115,8 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer) // funcname ( => Assert that the end parenthesis isn't followed by { if (Token::Match(funcname, "%var% (")) { - int parlevel = 0; - for (const Token *tok2 = funcname; tok2; tok2 = tok2->next()) { - if (tok2->str() == "(") - ++parlevel; - - else if (tok2->str() == ")") { - --parlevel; - if (parlevel == 0 && (Token::Match(tok2, ") const|{"))) - funcname = NULL; - if (parlevel <= 0) - break; - } - } + if (Token::Match(funcname->linkAt(1), ") const|{")) + funcname = NULL; } if (funcname) { diff --git a/test/testunusedfunctions.cpp b/test/testunusedfunctions.cpp index 6b9752831..fc367689c 100644 --- a/test/testunusedfunctions.cpp +++ b/test/testunusedfunctions.cpp @@ -46,6 +46,7 @@ private: TEST_CASE(unusedMain); TEST_CASE(initializationIsNotAFunction); TEST_CASE(operator1); // #3195 + TEST_CASE(returnRef); TEST_CASE(multipleFiles); // same function name in multiple files @@ -203,6 +204,11 @@ private: ASSERT_EQUALS("", errout.str()); } + void returnRef() { + check("int& foo() {return x;}"); + ASSERT_EQUALS("[test.cpp:1]: (style) The function 'foo' is never used\n", errout.str()); + } + void multipleFiles() { CheckUnusedFunctions c;