Refactoring: Use range for loops

This commit is contained in:
Daniel Marjamäki 2018-11-14 06:58:21 +01:00
parent f0c86b9d14
commit ac242b69d6
1 changed files with 22 additions and 25 deletions

View File

@ -1220,10 +1220,7 @@ void CheckUnusedVar::checkFunctionVariableUsage()
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase(); const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
// only check functions // only check functions
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];
// Bailout when there are lambdas or inline functions // Bailout when there are lambdas or inline functions
// TODO: Handle lambdas and inline functions properly // TODO: Handle lambdas and inline functions properly
if (scope->hasInlineOrLambdaFunction()) if (scope->hasInlineOrLambdaFunction())
@ -1311,27 +1308,27 @@ void CheckUnusedVar::checkStructMemberUsage()
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase(); const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
for (std::list<Scope>::const_iterator scope = symbolDatabase->scopeList.cbegin(); scope != symbolDatabase->scopeList.cend(); ++scope) { for (const Scope &scope : symbolDatabase->scopeList) {
if (scope->type != Scope::eStruct && scope->type != Scope::eUnion) if (scope.type != Scope::eStruct && scope.type != Scope::eUnion)
continue; continue;
if (scope->bodyStart->fileIndex() != 0 || scope->className.empty()) if (scope.bodyStart->fileIndex() != 0 || scope.className.empty())
continue; continue;
// Packed struct => possibly used by lowlevel code. Struct members might be required by hardware. // Packed struct => possibly used by lowlevel code. Struct members might be required by hardware.
if (scope->bodyEnd->isAttributePacked()) if (scope.bodyEnd->isAttributePacked())
continue; continue;
// Bail out if struct/union contains any functions // Bail out if struct/union contains any functions
if (!scope->functionList.empty()) if (!scope.functionList.empty())
continue; continue;
// bail out if struct is inherited // bail out if struct is inherited
bool bailout = false; bool bailout = false;
for (std::list<Scope>::const_iterator i = symbolDatabase->scopeList.cbegin(); i != symbolDatabase->scopeList.cend(); ++i) { for (const Scope &derivedScope : symbolDatabase->scopeList) {
if (i->definedType) { if (derivedScope.definedType) {
for (size_t j = 0; j < i->definedType->derivedFrom.size(); j++) { for (const Type::BaseInfo &derivedFrom : derivedScope.definedType->derivedFrom) {
if (i->definedType->derivedFrom[j].type == scope->definedType) { if (derivedFrom.type == scope.definedType) {
bailout = true; bailout = true;
break; break;
} }
@ -1343,7 +1340,7 @@ void CheckUnusedVar::checkStructMemberUsage()
// bail out for extern/global struct // bail out for extern/global struct
for (const Variable* var : symbolDatabase->variableList()) { for (const Variable* var : symbolDatabase->variableList()) {
if (var && (var->isExtern() || (var->isGlobal() && !var->isStatic())) && var->typeEndToken()->str() == scope->className) { if (var && (var->isExtern() || (var->isGlobal() && !var->isStatic())) && var->typeEndToken()->str() == scope.className) {
bailout = true; bailout = true;
break; break;
} }
@ -1352,19 +1349,19 @@ void CheckUnusedVar::checkStructMemberUsage()
continue; continue;
// Bail out if some data is casted to struct.. // Bail out if some data is casted to struct..
const std::string castPattern("( struct| " + scope->className + " * ) & %name% ["); const std::string castPattern("( struct| " + scope.className + " * ) & %name% [");
if (Token::findmatch(scope->bodyEnd, castPattern.c_str())) if (Token::findmatch(scope.bodyEnd, castPattern.c_str()))
continue; continue;
// (struct S){..} // (struct S){..}
const std::string initPattern("( struct| " + scope->className + " ) {"); const std::string initPattern("( struct| " + scope.className + " ) {");
if (Token::findmatch(scope->bodyEnd, initPattern.c_str())) if (Token::findmatch(scope.bodyEnd, initPattern.c_str()))
continue; continue;
// Bail out if struct is used in sizeof.. // Bail out if struct is used in sizeof..
for (const Token *tok = scope->bodyEnd; nullptr != (tok = Token::findsimplematch(tok, "sizeof ("));) { for (const Token *tok = scope.bodyEnd; nullptr != (tok = Token::findsimplematch(tok, "sizeof ("));) {
tok = tok->tokAt(2); tok = tok->tokAt(2);
if (Token::Match(tok, ("struct| " + scope->className).c_str())) { if (Token::Match(tok, ("struct| " + scope.className).c_str())) {
bailout = true; bailout = true;
break; break;
} }
@ -1373,19 +1370,19 @@ void CheckUnusedVar::checkStructMemberUsage()
continue; continue;
// Try to prevent false positives when struct members are not used directly. // Try to prevent false positives when struct members are not used directly.
if (Token::findmatch(scope->bodyEnd, (scope->className + " %type%| *").c_str())) if (Token::findmatch(scope.bodyEnd, (scope.className + " %type%| *").c_str()))
continue; continue;
for (std::list<Variable>::const_iterator var = scope->varlist.cbegin(); var != scope->varlist.cend(); ++var) { for (const Variable &var : scope.varlist) {
// declaring a POD member variable? // declaring a POD member variable?
if (!var->typeStartToken()->isStandardType() && !var->isPointer()) if (!var.typeStartToken()->isStandardType() && !var.isPointer())
continue; continue;
// Check if the struct member variable is used anywhere in the file // Check if the struct member variable is used anywhere in the file
if (Token::findsimplematch(mTokenizer->tokens(), (". " + var->name()).c_str())) if (Token::findsimplematch(mTokenizer->tokens(), (". " + var.name()).c_str()))
continue; continue;
unusedStructMemberError(var->nameToken(), scope->className, var->name(), scope->type == Scope::eUnion); unusedStructMemberError(var.nameToken(), scope.className, var.name(), scope.type == Scope::eUnion);
} }
} }
} }