Auto variables: improved the check for 'return retval.c_str()'. Ticket: #2191
This commit is contained in:
parent
47d37e7a05
commit
3d195f2326
|
@ -442,13 +442,17 @@ void CheckAutoVariables::returncstr()
|
|||
}
|
||||
|
||||
// have we reached a function that returns a reference?
|
||||
if (Token::Match(tok, "const char * %var% ("))
|
||||
if (Token::Match(tok, "const char *"))
|
||||
{
|
||||
// go to the '('
|
||||
const Token *tok2 = tok->tokAt(4);
|
||||
const Token *tok2 = tok->tokAt(3);
|
||||
while (Token::Match(tok2, "%var% ::"))
|
||||
tok2 = tok2->tokAt(2);
|
||||
if (!Token::Match(tok2, "%var% ("))
|
||||
continue;
|
||||
|
||||
// go to the ')'
|
||||
tok2 = tok2->link();
|
||||
tok2 = tok2->next()->link();
|
||||
|
||||
// is this a function implementation?
|
||||
if (Token::Match(tok2, ") const| {"))
|
||||
|
@ -482,7 +486,7 @@ void CheckAutoVariables::returncstr()
|
|||
tok2 = tok2->tokAt(2);
|
||||
|
||||
// is it a variable declaration?
|
||||
if (Token::Match(tok2, "%type% %var% ;"))
|
||||
if (Token::Match(tok2, "%type% %var% [;=]"))
|
||||
localvar.insert(tok2->next()->varId());
|
||||
}
|
||||
|
||||
|
|
|
@ -48,6 +48,7 @@ public:
|
|||
checkAutoVariables.autoVariables();
|
||||
checkAutoVariables.returnPointerToLocalArray();
|
||||
checkAutoVariables.returnReference();
|
||||
checkAutoVariables.returncstr();
|
||||
}
|
||||
|
||||
/** Check auto variables */
|
||||
|
|
|
@ -224,6 +224,13 @@ private:
|
|||
"}\n");
|
||||
ASSERT_EQUALS("[test.cpp:4]: (error) Returning pointer to auto variable\n", errout.str());
|
||||
|
||||
check("const char *Foo::f()\n"
|
||||
"{\n"
|
||||
" std::string s;\n"
|
||||
" return s.c_str();\n"
|
||||
"}\n");
|
||||
ASSERT_EQUALS("[test.cpp:4]: (error) Returning pointer to auto variable\n", errout.str());
|
||||
|
||||
check("std::string hello()\n"
|
||||
"{\n"
|
||||
" return \"hello\";\n"
|
||||
|
|
Loading…
Reference in New Issue