Skip to content

Commit de9f489

Browse files
firewaveamai2012
authored andcommitted
use range loops / constness (#2181)
* use range loops / constness * platform.cpp: avoid shadowed variable
1 parent 5f0f8af commit de9f489

21 files changed

Lines changed: 115 additions & 122 deletions

cli/cmdlineparser.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -736,9 +736,9 @@ bool CmdLineParser::parseFromArgs(int argc, const char* const argv[])
736736
else if (std::strcmp(argv[i], "--doc") == 0) {
737737
std::ostringstream doc;
738738
// Get documentation..
739-
for (std::list<Check *>::iterator it = Check::instances().begin(); it != Check::instances().end(); ++it) {
740-
const std::string& name((*it)->name());
741-
const std::string info((*it)->classInfo());
739+
for (const Check * it : Check::instances()) {
740+
const std::string& name(it->name());
741+
const std::string info(it->classInfo());
742742
if (!name.empty() && !info.empty())
743743
doc << "## " << name << " ##\n"
744744
<< info << "\n";

lib/checkbufferoverrun.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,8 @@ void CheckBufferOverrun::arrayIndex()
313313
// Negative index
314314
bool neg = false;
315315
std::vector<const ValueFlow::Value *> negativeIndexes;
316-
for (int i = 0; i < indexTokens.size(); ++i) {
317-
const ValueFlow::Value *negativeValue = indexTokens[i]->getValueLE(-1, mSettings);
316+
for (const Token * indexToken : indexTokens) {
317+
const ValueFlow::Value *negativeValue = indexToken->getValueLE(-1, mSettings);
318318
negativeIndexes.emplace_back(negativeValue);
319319
if (negativeValue)
320320
neg = true;

lib/checkclass.cpp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -563,23 +563,23 @@ void CheckClass::initVar(nonneg int varid, const Scope *scope, std::vector<Usage
563563

564564
void CheckClass::assignAllVar(std::vector<Usage> &usage)
565565
{
566-
for (int i = 0; i < usage.size(); ++i)
567-
usage[i].assign = true;
566+
for (Usage & i : usage)
567+
i.assign = true;
568568
}
569569

570570
void CheckClass::clearAllVar(std::vector<Usage> &usage)
571571
{
572-
for (int i = 0; i < usage.size(); ++i) {
573-
usage[i].assign = false;
574-
usage[i].init = false;
572+
for (Usage & i : usage) {
573+
i.assign = false;
574+
i.init = false;
575575
}
576576
}
577577

578578
bool CheckClass::isBaseClassFunc(const Token *tok, const Scope *scope)
579579
{
580580
// Iterate through each base class...
581-
for (int i = 0; i < scope->definedType->derivedFrom.size(); ++i) {
582-
const Type *derivedFrom = scope->definedType->derivedFrom[i].type;
581+
for (const Type::BaseInfo & i : scope->definedType->derivedFrom) {
582+
const Type *derivedFrom = i.type;
583583

584584
// Check if base class exists in database
585585
if (derivedFrom && derivedFrom->classScope) {
@@ -1222,8 +1222,8 @@ void CheckClass::checkMemsetType(const Scope *start, const Token *tok, const Sco
12221222
const bool printPortability = mSettings->isEnabled(Settings::PORTABILITY);
12231223

12241224
// recursively check all parent classes
1225-
for (int i = 0; i < type->definedType->derivedFrom.size(); i++) {
1226-
const Type* derivedFrom = type->definedType->derivedFrom[i].type;
1225+
for (const Type::BaseInfo & i : type->definedType->derivedFrom) {
1226+
const Type* derivedFrom = i.type;
12271227
if (derivedFrom && derivedFrom->classScope)
12281228
checkMemsetType(start, tok, derivedFrom->classScope, allocation, parsedTypes);
12291229
}
@@ -1680,10 +1680,10 @@ void CheckClass::virtualDestructor()
16801680
const Token *derivedClass = derived->next();
16811681

16821682
// Iterate through each base class...
1683-
for (int j = 0; j < scope->definedType->derivedFrom.size(); ++j) {
1683+
for (const Type::BaseInfo & j : scope->definedType->derivedFrom) {
16841684
// Check if base class is public and exists in database
1685-
if (scope->definedType->derivedFrom[j].access != AccessControl::Private && scope->definedType->derivedFrom[j].type) {
1686-
const Type *derivedFrom = scope->definedType->derivedFrom[j].type;
1685+
if (j.access != AccessControl::Private && j.type) {
1686+
const Type *derivedFrom = j.type;
16871687
const Scope *derivedFromScope = derivedFrom->classScope;
16881688
if (!derivedFromScope)
16891689
continue;
@@ -1937,9 +1937,9 @@ bool CheckClass::isMemberVar(const Scope *scope, const Token *tok) const
19371937
// not found in this class
19381938
if (!scope->definedType->derivedFrom.empty()) {
19391939
// check each base class
1940-
for (int i = 0; i < scope->definedType->derivedFrom.size(); ++i) {
1940+
for (const Type::BaseInfo & i : scope->definedType->derivedFrom) {
19411941
// find the base class
1942-
const Type *derivedFrom = scope->definedType->derivedFrom[i].type;
1942+
const Type *derivedFrom = i.type;
19431943

19441944
// find the function in the base class
19451945
if (derivedFrom && derivedFrom->classScope) {
@@ -1976,9 +1976,9 @@ bool CheckClass::isMemberFunc(const Scope *scope, const Token *tok) const
19761976
// not found in this class
19771977
if (!scope->definedType->derivedFrom.empty()) {
19781978
// check each base class
1979-
for (int i = 0; i < scope->definedType->derivedFrom.size(); ++i) {
1979+
for (const Type::BaseInfo & i : scope->definedType->derivedFrom) {
19801980
// find the base class
1981-
const Type *derivedFrom = scope->definedType->derivedFrom[i].type;
1981+
const Type *derivedFrom = i.type;
19821982

19831983
// find the function in the base class
19841984
if (derivedFrom && derivedFrom->classScope) {
@@ -2001,9 +2001,9 @@ bool CheckClass::isConstMemberFunc(const Scope *scope, const Token *tok) const
20012001
// not found in this class
20022002
if (!scope->definedType->derivedFrom.empty()) {
20032003
// check each base class
2004-
for (int i = 0; i < scope->definedType->derivedFrom.size(); ++i) {
2004+
for (const Type::BaseInfo & i : scope->definedType->derivedFrom) {
20052005
// find the base class
2006-
const Type *derivedFrom = scope->definedType->derivedFrom[i].type;
2006+
const Type *derivedFrom = i.type;
20072007

20082008
// find the function in the base class
20092009
if (derivedFrom && derivedFrom->classScope) {

lib/checkio.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,8 +1561,8 @@ bool CheckIO::ArgumentInfo::isStdVectorOrString()
15611561
return true;
15621562
} else if (variableInfo->type() && !variableInfo->type()->derivedFrom.empty()) {
15631563
const std::vector<Type::BaseInfo>& derivedFrom = variableInfo->type()->derivedFrom;
1564-
for (std::size_t i = 0, size = derivedFrom.size(); i < size; ++i) {
1565-
const Token* nameTok = derivedFrom[i].nameTok;
1564+
for (const Type::BaseInfo & i : derivedFrom) {
1565+
const Token* nameTok = i.nameTok;
15661566
if (Token::Match(nameTok, "std :: vector|array <")) {
15671567
typeToken = nameTok->tokAt(4);
15681568
_template = true;

lib/checkstring.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,7 @@ void CheckString::checkSuspiciousStringCompare()
184184
// Pointer addition?
185185
if (varTok->str() == "+" && mTokenizer->isC()) {
186186
const Token * const tokens[2] = { varTok->astOperand1(), varTok->astOperand2() };
187-
for (int nr = 0; nr < 2; nr++) {
188-
const Token *t = tokens[nr];
187+
for (const Token * t : tokens) {
189188
while (t && (t->str() == "." || t->str() == "::"))
190189
t = t->astOperand2();
191190
if (t && t->variable() && t->variable()->isPointer())

lib/errorlogger.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -638,8 +638,7 @@ std::string ErrorLogger::ErrorMessage::FileLocation::stringify() const
638638
std::string ErrorLogger::toxml(const std::string &str)
639639
{
640640
std::ostringstream xml;
641-
for (std::size_t i = 0U; i < str.length(); i++) {
642-
const unsigned char c = str[i];
641+
for (unsigned char c : str) {
643642
switch (c) {
644643
case '<':
645644
xml << "&lt;";
@@ -678,8 +677,8 @@ std::string ErrorLogger::plistHeader(const std::string &version, const std::vect
678677
<< "<string>cppcheck version " << version << "</string>\r\n"
679678
<< " <key>files</key>\r\n"
680679
<< " <array>\r\n";
681-
for (unsigned int i = 0; i < files.size(); ++i)
682-
ostr << " <string>" << ErrorLogger::toxml(files[i]) << "</string>\r\n";
680+
for (const std::string & file : files)
681+
ostr << " <string>" << ErrorLogger::toxml(file) << "</string>\r\n";
683682
ostr << " </array>\r\n"
684683
<< " <key>diagnostics</key>\r\n"
685684
<< " <array>\r\n";

lib/importproject.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -736,13 +736,13 @@ void ImportProject::importBcb6Prj(const std::string &projectFilename)
736736
{
737737
std::string arg;
738738

739-
for (int i = 0; i < cflag1.size(); ++i) {
740-
if (cflag1.at(i) == ' ' && !arg.empty()) {
739+
for (char i : cflag1) {
740+
if (i == ' ' && !arg.empty()) {
741741
cflags.insert(arg);
742742
arg.clear();
743743
continue;
744744
}
745-
arg += cflag1.at(i);
745+
arg += i;
746746
}
747747

748748
if (!arg.empty()) {

lib/library.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -869,8 +869,7 @@ std::string Library::getFunctionName(const Token *ftok, bool *error) const
869869
if (!scope->isClassOrStruct())
870870
continue;
871871
const std::vector<Type::BaseInfo> &derivedFrom = scope->definedType->derivedFrom;
872-
for (unsigned int i = 0; i < derivedFrom.size(); ++i) {
873-
const Type::BaseInfo &baseInfo = derivedFrom[i];
872+
for (const Type::BaseInfo & baseInfo : derivedFrom) {
874873
const std::string name(baseInfo.name + "::" + ftok->str());
875874
if (functions.find(name) != functions.end() && matchArguments(ftok, name))
876875
return name;

lib/mathlib.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,8 @@ MathLib::biguint MathLib::toULongNumber(const std::string & str)
347347
static unsigned int encodeMultiChar(const std::string& str)
348348
{
349349
unsigned int retval = 0;
350-
for (std::string::const_iterator it=str.begin(); it!=str.end(); ++it) {
351-
retval = (retval << 8) | *it;
350+
for (char it : str) {
351+
retval = (retval << 8) | it;
352352
}
353353
return retval;
354354
}
@@ -1338,10 +1338,10 @@ bool MathLib::isNullValue(const std::string &str)
13381338
if (str.empty() || (!std::isdigit(static_cast<unsigned char>(str[0])) && (str[0] != '.' && str[0] != '-' && str[0] != '+')))
13391339
return false; // Has to be a number
13401340

1341-
for (size_t i = 0; i < str.size(); i++) {
1342-
if (std::isdigit(static_cast<unsigned char>(str[i])) && str[i] != '0') // May not contain digits other than 0
1341+
for (char i : str) {
1342+
if (std::isdigit(static_cast<unsigned char>(i)) && i != '0') // May not contain digits other than 0
13431343
return false;
1344-
if (str[i] == 'E' || str[i] == 'e')
1344+
if (i == 'E' || i == 'e')
13451345
return true;
13461346
}
13471347
return true;

lib/platform.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,8 @@ bool cppcheck::Platform::loadPlatformFile(const char exename[], const std::strin
173173
filenames.push_back(filesdir + ("platforms/" + filename + ".xml"));
174174
#endif
175175
bool success = false;
176-
for (int i = 0; i < filenames.size(); ++i) {
177-
if (doc.LoadFile(filenames[i].c_str()) == tinyxml2::XML_SUCCESS) {
176+
for (const std::string & f : filenames) {
177+
if (doc.LoadFile(f.c_str()) == tinyxml2::XML_SUCCESS) {
178178
success = true;
179179
break;
180180
}

0 commit comments

Comments
 (0)