From 043b64467bf60d2d7e75fffac1cf21dfee42cc87 Mon Sep 17 00:00:00 2001 From: jrp2014 Date: Thu, 5 Apr 2018 15:48:23 +0200 Subject: [PATCH] Refactor lib/checkclass.cpp --- lib/checkclass.cpp | 46 +++++++++++++++------------------------------- 1 file changed, 15 insertions(+), 31 deletions(-) diff --git a/lib/checkclass.cpp b/lib/checkclass.cpp index 2e43354ef..1b7d0b37b 100644 --- a/lib/checkclass.cpp +++ b/lib/checkclass.cpp @@ -134,10 +134,9 @@ void CheckClass::constructors() } - std::list::const_iterator func; std::vector usage(scope->varlist.size()); - for (func = scope->functionList.begin(); func != scope->functionList.end(); ++func) { + for (std::list::const_iterator func = scope->functionList.begin(); func != scope->functionList.end(); ++func) { if (!func->hasBody() || !(func->isConstructor() || func->type == Function::eOperatorEqual)) continue; @@ -389,12 +388,11 @@ void CheckClass::noCopyConstructorError(const Token *tok, const std::string &cla bool CheckClass::canNotCopy(const Scope *scope) { - std::list::const_iterator func; bool constructor = false; bool publicAssign = false; bool publicCopy = false; - for (func = scope->functionList.begin(); func != scope->functionList.end(); ++func) { + for (std::list::const_iterator func = scope->functionList.begin(); func != scope->functionList.end(); ++func) { if (func->isConstructor()) constructor = true; if (func->access != Public) @@ -413,13 +411,12 @@ bool CheckClass::canNotCopy(const Scope *scope) bool CheckClass::canNotMove(const Scope *scope) { - std::list::const_iterator func; bool constructor = false; bool publicAssign = false; bool publicCopy = false; bool publicMove = false; - for (func = scope->functionList.begin(); func != scope->functionList.end(); ++func) { + for (std::list::const_iterator func = scope->functionList.begin(); func != scope->functionList.end(); ++func) { if (func->isConstructor()) constructor = true; if (func->access != Public) @@ -441,10 +438,9 @@ bool CheckClass::canNotMove(const Scope *scope) void CheckClass::assignVar(unsigned int varid, const Scope *scope, std::vector &usage) { - std::list::const_iterator var; unsigned int count = 0; - for (var = scope->varlist.begin(); var != scope->varlist.end(); ++var, ++count) { + for (std::list::const_iterator var = scope->varlist.begin(); var != scope->varlist.end(); ++var, ++count) { if (var->declarationId() == varid) { usage[count].assign = true; return; @@ -454,10 +450,9 @@ void CheckClass::assignVar(unsigned int varid, const Scope *scope, std::vector &usage) { - std::list::const_iterator var; unsigned int count = 0; - for (var = scope->varlist.begin(); var != scope->varlist.end(); ++var, ++count) { + for (std::list::const_iterator var = scope->varlist.begin(); var != scope->varlist.end(); ++var, ++count) { if (var->declarationId() == varid) { usage[count].init = true; return; @@ -488,9 +483,8 @@ bool CheckClass::isBaseClassFunc(const Token *tok, const Scope *scope) // Check if base class exists in database if (derivedFrom && derivedFrom->classScope) { const std::list& functionList = derivedFrom->classScope->functionList; - std::list::const_iterator func; - for (func = functionList.begin(); func != functionList.end(); ++func) { + for (std::list::const_iterator func = functionList.begin(); func != functionList.end(); ++func) { if (func->tokenDef->str() == tok->str()) return true; } @@ -589,8 +583,7 @@ void CheckClass::initializeVarList(const Function &func, std::listnext(), "%var% . %name% (")) { - std::list::const_iterator var; - for (var = scope->varlist.begin(); var != scope->varlist.end(); ++var) { + for (std::list::const_iterator var = scope->varlist.begin(); var != scope->varlist.end(); ++var) { if (var->declarationId() == ftok->next()->varId()) { /** @todo false negative: we assume function changes variable state */ assignVar(ftok->next()->varId(), scope, usage); @@ -1223,9 +1216,8 @@ void CheckClass::operatorEq() const std::size_t classes = symbolDatabase->classAndStructScopes.size(); for (std::size_t i = 0; i < classes; ++i) { const Scope * scope = symbolDatabase->classAndStructScopes[i]; - std::list::const_iterator func; - for (func = scope->functionList.begin(); func != scope->functionList.end(); ++func) { + for (std::list::const_iterator func = scope->functionList.begin(); func != scope->functionList.end(); ++func) { if (func->type == Function::eOperatorEqual && func->access == Public) { // skip "deleted" functions - cannot be called anyway if (func->isDelete()) @@ -1278,9 +1270,8 @@ void CheckClass::operatorEqRetRefThis() const std::size_t classes = symbolDatabase->classAndStructScopes.size(); for (std::size_t i = 0; i < classes; ++i) { const Scope * scope = symbolDatabase->classAndStructScopes[i]; - std::list::const_iterator func; - for (func = scope->functionList.begin(); func != scope->functionList.end(); ++func) { + for (std::list::const_iterator func = scope->functionList.begin(); func != scope->functionList.end(); ++func) { if (func->type == Function::eOperatorEqual && func->hasBody()) { // make sure return signature is correct if (Token::Match(func->retDef, "%type% &") && func->retDef->str() == scope->className) { @@ -1316,10 +1307,8 @@ void CheckClass::checkReturnPtrThis(const Scope *scope, const Function *func, co // check if a function is called if (tok->strAt(2) == "(" && tok->linkAt(2)->next()->str() == ";") { - std::list::const_iterator it; - // check if it is a member function - for (it = scope->functionList.begin(); it != scope->functionList.end(); ++it) { + for (std::list::const_iterator it = scope->functionList.begin(); it != scope->functionList.end(); ++it) { // check for a regular function with the same name and a body if (it->type == Function::eFunction && it->hasBody() && it->token->str() == tok->next()->str()) { @@ -1419,8 +1408,7 @@ void CheckClass::operatorEqToSelf() if (scope->definedType->derivedFrom.size() > 1) continue; - std::list::const_iterator func; - for (func = scope->functionList.begin(); func != scope->functionList.end(); ++func) { + for (std::list::const_iterator func = scope->functionList.begin(); func != scope->functionList.end(); ++func) { if (func->type == Function::eOperatorEqual && func->hasBody()) { // make sure that the operator takes an object of the same type as *this, otherwise we can't detect self-assignment checks if (func->argumentList.empty()) @@ -1548,8 +1536,7 @@ void CheckClass::virtualDestructor() if (printInconclusive) { const Function *destructor = scope->getDestructor(); if (destructor && !destructor->isVirtual()) { - std::list::const_iterator func; - for (func = scope->functionList.begin(); func != scope->functionList.end(); ++func) { + for (std::list::const_iterator func = scope->functionList.begin(); func != scope->functionList.end(); ++func) { if (func->isVirtual()) { inconclusiveErrors.push_back(destructor); break; @@ -1719,9 +1706,8 @@ void CheckClass::checkConst() const std::size_t classes = symbolDatabase->classAndStructScopes.size(); for (std::size_t i = 0; i < classes; ++i) { const Scope * scope = symbolDatabase->classAndStructScopes[i]; - std::list::const_iterator func; - for (func = scope->functionList.begin(); func != scope->functionList.end(); ++func) { + for (std::list::const_iterator func = scope->functionList.begin(); func != scope->functionList.end(); ++func) { // does the function have a body? if (func->type != Function::eFunction || !func->hasBody()) continue; @@ -1819,8 +1805,7 @@ bool CheckClass::isMemberVar(const Scope *scope, const Token *tok) const } } while (again); - std::list::const_iterator var; - for (var = scope->varlist.begin(); var != scope->varlist.end(); ++var) { + for (std::list::const_iterator var = scope->varlist.begin(); var != scope->varlist.end(); ++var) { if (var->name() == tok->str()) { if (tok->varId() == 0) symbolDatabase->debugMessage(tok, "CheckClass::isMemberVar found used member variable \'" + tok->str() + "\' with varid 0"); @@ -2105,10 +2090,9 @@ void CheckClass::initializerListOrder() const std::size_t classes = symbolDatabase->classAndStructScopes.size(); for (std::size_t i = 0; i < classes; ++i) { const Scope * scope = symbolDatabase->classAndStructScopes[i]; - std::list::const_iterator func; // iterate through all member functions looking for constructors - for (func = scope->functionList.begin(); func != scope->functionList.end(); ++func) { + for (std::list::const_iterator func = scope->functionList.begin(); func != scope->functionList.end(); ++func) { if (func->isConstructor() && func->hasBody()) { // check for initializer list const Token *tok = func->arg->link()->next();