Fixed #2558 (false positive: (error) Returning reference to auto variable)

This commit is contained in:
Daniel Marjamäki 2011-02-12 15:39:26 +01:00
parent 2657d36d03
commit 7507f64ee7
2 changed files with 22 additions and 3 deletions

View File

@ -42,12 +42,18 @@ public:
: Check(myName(), tokenizer, settings, errorLogger)
{ }
/** @brief Run checks against the normal token list */
void runChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
{
CheckAutoVariables checkAutoVariables(tokenizer, settings, errorLogger);
checkAutoVariables.returnReference();
}
void runSimplifiedChecks(const Tokenizer *tokenizer, const Settings *settings, ErrorLogger *errorLogger)
{
CheckAutoVariables checkAutoVariables(tokenizer, settings, errorLogger);
checkAutoVariables.autoVariables();
checkAutoVariables.returnPointerToLocalArray();
checkAutoVariables.returnReference();
checkAutoVariables.returncstr();
}

View File

@ -47,6 +47,10 @@ private:
Tokenizer tokenizer(&settings, this);
std::istringstream istr(code);
tokenizer.tokenize(istr, "test.cpp");
CheckAutoVariables checkAutoVariables(&tokenizer, &settings, this);
checkAutoVariables.runChecks(&tokenizer, &settings, this);
tokenizer.simplifyTokenList();
// Assign variable ids
@ -56,10 +60,8 @@ private:
tokenizer.fillFunctionList();
// Check auto variables
CheckAutoVariables checkAutoVariables(&tokenizer, &settings, this);
checkAutoVariables.autoVariables();
checkAutoVariables.returnPointerToLocalArray();
checkAutoVariables.returnReference();
checkAutoVariables.returncstr();
}
@ -81,6 +83,7 @@ private:
// return reference..
TEST_CASE(returnReference1);
TEST_CASE(returnReference2);
TEST_CASE(returnReference3);
// return c_str()..
TEST_CASE(returncstr1);
@ -351,6 +354,16 @@ private:
ASSERT_EQUALS("[test.cpp:11]: (error) Returning reference to temporary\n", errout.str());
}
void returnReference3()
{
check("double & f(double & rd) {\n"
" double ret = getValue();\n"
" rd = ret;\n"
" return rd;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void returncstr1()
{
check("const char *foo()\n"