Moved duplicate code into function in CheckOther::checkRedundantAssignment()

This commit is contained in:
PKEuS 2012-09-03 10:46:51 +02:00
parent 318d80be08
commit 2be2738c6e
1 changed files with 15 additions and 17 deletions

View File

@ -614,6 +614,19 @@ void CheckOther::sizeofForPointerError(const Token *tok, const std::string &varn
// Detect redundant assignments: x = 0; x = 4;
//---------------------------------------------------------------------------
static void eraseNotLocalArg(std::map<unsigned int, const Token*>& container, const SymbolDatabase* symbolDatabase)
{
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())) {
container.erase(i++);
if (i == container.end())
break;
} else
++i;
}
}
void CheckOther::checkRedundantAssignment()
{
if (!_settings->isEnabled("performance"))
@ -702,23 +715,8 @@ void CheckOther::checkRedundantAssignment()
const Token* funcEnd = func->functionScope->classEnd;
bool noreturn;
if (!_tokenizer->IsScopeNoReturn(funcEnd, &noreturn) && !noreturn) {
for (std::map<unsigned int, const Token*>::iterator i = varAssignments.begin(); i != varAssignments.end(); ++i) {
const Variable* var = symbolDatabase->getVariableFromVarId(i->first);
if (!var || (!var->isLocal() && !var->isArgument())) {
varAssignments.erase(i++);
if (i == varAssignments.end())
break;
}
}
for (std::map<unsigned int, const Token*>::iterator i = memAssignments.begin(); i != memAssignments.end(); ++i) {
const Variable* var = symbolDatabase->getVariableFromVarId(i->first);
if (!var || (!var->isLocal() && !var->isArgument())) {
memAssignments.erase(i++);
if (i == memAssignments.end())
break;
}
}
eraseNotLocalArg(varAssignments, symbolDatabase);
eraseNotLocalArg(memAssignments, symbolDatabase);
} else {
varAssignments.clear();
memAssignments.clear();