Refactoring; Use range for loop

This commit is contained in:
Daniel Marjamäki 2018-07-14 08:18:10 +02:00
parent 5b1d96b346
commit c60763bc14
1 changed files with 22 additions and 36 deletions

View File

@ -311,8 +311,7 @@ void CheckCondition::comparison()
continue; continue;
std::list<MathLib::bigint> numbers; std::list<MathLib::bigint> numbers;
getnumchildren(expr1, numbers); getnumchildren(expr1, numbers);
for (std::list<MathLib::bigint>::const_iterator num = numbers.begin(); num != numbers.end(); ++num) { for (const MathLib::bigint num1 : numbers) {
const MathLib::bigint num1 = *num;
if (num1 < 0) if (num1 < 0)
continue; continue;
if (Token::Match(tok, "==|!=")) { if (Token::Match(tok, "==|!=")) {
@ -412,13 +411,13 @@ void CheckCondition::multiCondition()
const SymbolDatabase* const symbolDatabase = mTokenizer->getSymbolDatabase(); const SymbolDatabase* const symbolDatabase = mTokenizer->getSymbolDatabase();
for (std::list<Scope>::const_iterator i = symbolDatabase->scopeList.begin(); i != symbolDatabase->scopeList.end(); ++i) { for (const Scope &scope : symbolDatabase->scopeList) {
if (i->type != Scope::eIf) if (scope.type != Scope::eIf)
continue; continue;
const Token * const cond1 = i->classDef->next()->astOperand2(); const Token * const cond1 = scope.classDef->next()->astOperand2();
const Token * tok2 = i->classDef->next(); const Token * tok2 = scope.classDef->next();
for (;;) { for (;;) {
tok2 = tok2->link(); tok2 = tok2->link();
if (!Token::simpleMatch(tok2, ") {")) if (!Token::simpleMatch(tok2, ") {"))
@ -472,12 +471,12 @@ void CheckCondition::multiCondition2()
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase(); const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
for (std::list<Scope>::const_iterator scope = symbolDatabase->scopeList.begin(); scope != symbolDatabase->scopeList.end(); ++scope) { for (const Scope &scope : symbolDatabase->scopeList) {
const Token *condTok = nullptr; const Token *condTok = nullptr;
if (scope->type == Scope::eIf || scope->type == Scope::eWhile) if (scope.type == Scope::eIf || scope.type == Scope::eWhile)
condTok = scope->classDef->next()->astOperand2(); condTok = scope.classDef->next()->astOperand2();
else if (scope->type == Scope::eFor) { else if (scope.type == Scope::eFor) {
condTok = scope->classDef->next()->astOperand2(); condTok = scope.classDef->next()->astOperand2();
if (!condTok || condTok->str() != ";") if (!condTok || condTok->str() != ";")
continue; continue;
condTok = condTok->astOperand2(); condTok = condTok->astOperand2();
@ -489,7 +488,7 @@ void CheckCondition::multiCondition2()
continue; continue;
const Token * const cond1 = condTok; const Token * const cond1 = condTok;
if (!Token::simpleMatch(scope->classDef->linkAt(1), ") {")) if (!Token::simpleMatch(scope.classDef->linkAt(1), ") {"))
continue; continue;
bool nonConstFunctionCall = false; bool nonConstFunctionCall = false;
@ -534,11 +533,11 @@ void CheckCondition::multiCondition2()
// parse until second condition is reached.. // parse until second condition is reached..
enum MULTICONDITIONTYPE { INNER, AFTER } type; enum MULTICONDITIONTYPE { INNER, AFTER } type;
const Token *tok; const Token *tok;
if (Token::Match(scope->bodyStart, "{ return|throw|continue|break")) { if (Token::Match(scope.bodyStart, "{ return|throw|continue|break")) {
tok = scope->bodyEnd->next(); tok = scope.bodyEnd->next();
type = MULTICONDITIONTYPE::AFTER; type = MULTICONDITIONTYPE::AFTER;
} else { } else {
tok = scope->bodyStart; tok = scope.bodyStart;
type = MULTICONDITIONTYPE::INNER; type = MULTICONDITIONTYPE::INNER;
} }
const Token * const endToken = tok->scope()->bodyEnd; const Token * const endToken = tok->scope()->bodyEnd;
@ -628,8 +627,8 @@ void CheckCondition::multiCondition2()
break; break;
} }
bool changed = false; bool changed = false;
for (std::set<unsigned int>::const_iterator it = vars.begin(); it != vars.end(); ++it) { for (unsigned int varid : vars) {
if (isVariableChanged(tok1, tok2, *it, nonlocal, mSettings, mTokenizer->isCPP())) { if (isVariableChanged(tok1, tok2, varid, nonlocal, mSettings, mTokenizer->isCPP())) {
changed = true; changed = true;
break; break;
} }
@ -871,9 +870,7 @@ void CheckCondition::checkIncorrectLogicOperator()
const bool printInconclusive = mSettings->inconclusive; const bool printInconclusive = mSettings->inconclusive;
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase(); const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
const std::size_t functions = symbolDatabase->functionScopes.size(); for (const Scope * scope : symbolDatabase->functionScopes) {
for (std::size_t ii = 0; ii < functions; ++ii) {
const Scope * scope = symbolDatabase->functionScopes[ii];
for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) { for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) {
if (!Token::Match(tok, "%oror%|&&") || !tok->astOperand1() || !tok->astOperand2()) if (!Token::Match(tok, "%oror%|&&") || !tok->astOperand1() || !tok->astOperand2())
@ -1063,9 +1060,7 @@ void CheckCondition::checkModuloAlwaysTrueFalse()
return; return;
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase(); const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
const std::size_t functions = symbolDatabase->functionScopes.size(); for (const Scope * scope : symbolDatabase->functionScopes) {
for (std::size_t i = 0; i < functions; ++i) {
const Scope * scope = symbolDatabase->functionScopes[i];
for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) { for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) {
if (!tok->isComparisonOp()) if (!tok->isComparisonOp())
continue; continue;
@ -1119,9 +1114,7 @@ void CheckCondition::clarifyCondition()
const bool isC = mTokenizer->isC(); const bool isC = mTokenizer->isC();
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase(); const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
const std::size_t functions = symbolDatabase->functionScopes.size(); for (const Scope * scope : symbolDatabase->functionScopes) {
for (std::size_t i = 0; i < functions; ++i) {
const Scope * scope = symbolDatabase->functionScopes[i];
for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) { for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) {
if (Token::Match(tok, "( %name% [=&|^]")) { if (Token::Match(tok, "( %name% [=&|^]")) {
for (const Token *tok2 = tok->tokAt(3); tok2; tok2 = tok2->next()) { for (const Token *tok2 = tok->tokAt(3); tok2; tok2 = tok2->next()) {
@ -1184,10 +1177,7 @@ void CheckCondition::alwaysTrueFalse()
return; return;
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase(); const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
const std::size_t functions = symbolDatabase->functionScopes.size(); for (const Scope * scope : symbolDatabase->functionScopes) {
for (std::size_t i = 0; i < functions; ++i) {
const Scope * scope = symbolDatabase->functionScopes[i];
for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) { for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) {
if (tok->link()) // don't write false positives when templates are used if (tok->link()) // don't write false positives when templates are used
@ -1297,9 +1287,7 @@ void CheckCondition::checkInvalidTestForOverflow()
return; return;
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase(); const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
const std::size_t functions = symbolDatabase->functionScopes.size(); for (const Scope * scope : symbolDatabase->functionScopes) {
for (std::size_t i = 0; i < functions; ++i) {
const Scope * scope = symbolDatabase->functionScopes[i];
for (const Token* tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next()) { for (const Token* tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next()) {
if (!tok->isComparisonOp() || !tok->astOperand1() || !tok->astOperand2()) if (!tok->isComparisonOp() || !tok->astOperand1() || !tok->astOperand2())
@ -1361,9 +1349,7 @@ void CheckCondition::checkPointerAdditionResultNotNull()
return; return;
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase(); const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
const std::size_t functions = symbolDatabase->functionScopes.size(); for (const Scope * scope : symbolDatabase->functionScopes) {
for (std::size_t i = 0; i < functions; ++i) {
const Scope * scope = symbolDatabase->functionScopes[i];
for (const Token* tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next()) { for (const Token* tok = scope->bodyStart; tok != scope->bodyEnd; tok = tok->next()) {
if (!tok->isComparisonOp() || !tok->astOperand1() || !tok->astOperand2()) if (!tok->isComparisonOp() || !tok->astOperand1() || !tok->astOperand2())