Fixed #3594 (false positive: returning reference to auto variable)

This commit is contained in:
Daniel Marjamäki 2012-03-28 18:21:06 +02:00
parent 4f1f6e1824
commit abcbe2e49f
2 changed files with 11 additions and 2 deletions

View File

@ -260,9 +260,9 @@ void CheckAutoVariables::returnReference()
if (var1 && var1->isLocal() && !var1->isStatic()) {
// If reference variable is used, check what it references
if (Token::Match(var1->nameToken(), "%var% =")) {
if (Token::Match(var1->nameToken(), "%var% [=(]")) {
const Token *tok3 = var1->nameToken()->tokAt(2);
if (!Token::Match(tok3, "%var% [;.]"))
if (!Token::Match(tok3, "%var% [);.]"))
continue;
// Only report error if variable that is referenced is

View File

@ -87,6 +87,7 @@ private:
TEST_CASE(returnReference3);
TEST_CASE(returnReference4);
TEST_CASE(returnReference5);
TEST_CASE(returnReference6);
// return c_str()..
TEST_CASE(returncstr);
@ -520,6 +521,14 @@ private:
ASSERT_EQUALS("", errout.str());
}
void returnReference6() {
check("Fred & create() {\n"
" Fred &fred(*new Fred);\n"
" return fred;\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void returncstr() {
check("std::string hello()\n"
"{\n"