Fixed #3791 (False positive: returnTempReference)

This commit is contained in:
Daniel Marjamäki 2012-07-18 07:22:29 +02:00
parent 826d5bb00b
commit 17c27f51ff
2 changed files with 12 additions and 1 deletions

View File

@ -232,7 +232,8 @@ bool CheckAutoVariables::returnTemporary(const Token *tok) const
if (!Token::Match(tok, "return %var% ("))
return false;
// TODO: Find all functions that return objects by value
return bool(NULL != Token::findmatch(_tokenizer->tokens(), ("std :: string " + tok->next()->str() + " (").c_str()));
return bool(NULL != Token::findmatch(_tokenizer->tokens(), ("std :: string " + tok->next()->str() + " (").c_str()) &&
NULL == Token::findmatch(_tokenizer->tokens(), ("std :: string & " + tok->next()->str() + " (").c_str()));
}
//---------------------------------------------------------------------------

View File

@ -88,6 +88,7 @@ private:
TEST_CASE(returnReference4);
TEST_CASE(returnReference5);
TEST_CASE(returnReference6);
TEST_CASE(returnReference7);
// return c_str()..
TEST_CASE(returncstr);
@ -552,6 +553,15 @@ private:
ASSERT_EQUALS("", errout.str());
}
void returnReference7() { // 3791 - false positive for overloaded function
check("std::string a();\n"
"std::string &a(int);\n"
"std::string &b() {\n"
" return a(12);\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void returncstr() {
check("std::string hello()\n"
"{\n"