Treat references like global variables in CheckOther::checkRedundantAssignment() as they might refer to such. (#4425)

This commit is contained in:
PKEuS 2013-02-15 08:09:31 -08:00
parent 107dd31e50
commit 017b4a8a7f
2 changed files with 10 additions and 1 deletions

View File

@ -665,7 +665,7 @@ static void eraseNotLocalArg(std::map<unsigned int, const Token*>& container, co
{
for (std::map<unsigned int, const Token*>::iterator i = container.begin(); i != container.end();) {
const Variable* var = symbolDatabase->getVariableFromVarId(i->first);
if (!var || (!var->isLocal() && !var->isArgument()) || var->isStatic()) {
if (!var || (!var->isLocal() && !var->isArgument()) || var->isStatic() || var->isReference()) {
container.erase(i++);
if (i == container.end())
break;

View File

@ -6818,6 +6818,7 @@ private:
" bar = x;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void f() {\n"
" Foo& bar = foo();\n"
" bar = x;\n"
@ -6825,6 +6826,14 @@ private:
"}");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:4]: (performance) Variable 'bar' is reassigned a value before the old one has been used.\n", errout.str());
check("void f() {\n"
" Foo& bar = foo();\n" // #4425. bar might refer to something global, etc.
" bar = y();\n"
" foo();\n"
" bar = y();\n"
"}");
ASSERT_EQUALS("", errout.str());
// Tests with function call between assignment
check("void f(int i) {\n"
" i = 1;\n"