Skip to content

Commit df9b243

Browse files
committed
Modernize: use ranged for loops in CheckNullPointer
1 parent 3f4aae7 commit df9b243

2 files changed

Lines changed: 12 additions & 20 deletions

File tree

lib/checknullpointer.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,7 @@ void CheckNullPointer::nullPointerLinkedList()
287287

288288
// Check usage of dereferenced variable in the loop..
289289
// TODO: Move this to ValueFlow
290-
for (std::list<Scope*>::const_iterator j = i->nestedList.begin(); j != i->nestedList.end(); ++j) {
291-
const Scope* const scope = *j;
290+
for (const Scope *scope : i->nestedList) {
292291
if (scope->type != Scope::eWhile)
293292
continue;
294293

@@ -387,9 +386,7 @@ void CheckNullPointer::nullConstantDereference()
387386
{
388387
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
389388

390-
const std::size_t functions = symbolDatabase->functionScopes.size();
391-
for (std::size_t i = 0; i < functions; ++i) {
392-
const Scope * scope = symbolDatabase->functionScopes[i];
389+
for (const Scope * scope : symbolDatabase->functionScopes) {
393390
if (scope->function == nullptr || !scope->function->hasBody()) // We only look for functions with a body
394391
continue;
395392

@@ -521,9 +518,7 @@ void CheckNullPointer::nullPointerError(const Token *tok, const std::string &var
521518
void CheckNullPointer::arithmetic()
522519
{
523520
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
524-
const std::size_t functions = symbolDatabase->functionScopes.size();
525-
for (std::size_t i = 0; i < functions; ++i) {
526-
const Scope * scope = symbolDatabase->functionScopes[i];
521+
for (const Scope * scope : symbolDatabase->functionScopes) {
527522
for (const Token* tok = scope->bodyStart->next(); tok != scope->bodyEnd; tok = tok->next()) {
528523
if (!Token::Match(tok, "-|+|+=|-=|++|--"))
529524
continue;

lib/symboldatabase.cpp

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,6 @@ static const Token* skipScopeIdentifiers(const Token* tok)
8282

8383
void SymbolDatabase::createSymbolDatabaseFindAllScopes()
8484
{
85-
const bool isCPP = _tokenizer->isCPP();
86-
const bool isC = _tokenizer->isC();
87-
8885
// create global scope
8986
scopeList.emplace_back(this, nullptr, nullptr);
9087

@@ -102,16 +99,16 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
10299
"SymbolDatabase",
103100
tok->progressValue());
104101
// Locate next class
105-
if ((isCPP && ((Token::Match(tok, "class|struct|union|namespace ::| %name% {|:|::|<") &&
102+
if ((_tokenizer->isCPP() && ((Token::Match(tok, "class|struct|union|namespace ::| %name% {|:|::|<") &&
106103
!Token::Match(tok->previous(), "new|friend|const|enum|typedef|mutable|volatile|)|(|<")) ||
107104
(Token::Match(tok, "enum class| %name% {") ||
108105
Token::Match(tok, "enum class| %name% : %name% {"))))
109-
|| (isC && Token::Match(tok, "struct|union|enum %name% {"))) {
106+
|| (_tokenizer->isC() && Token::Match(tok, "struct|union|enum %name% {"))) {
110107
const Token *tok2 = tok->tokAt(2);
111108

112109
if (tok->strAt(1) == "::")
113110
tok2 = tok2->next();
114-
else if (isCPP && tok->strAt(1) == "class")
111+
else if (_tokenizer->isCPP() && tok->strAt(1) == "class")
115112
tok2 = tok2->next();
116113

117114
while (Token::Match(tok2, ":: %name%"))
@@ -127,7 +124,7 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
127124
}
128125

129126
// skip over final
130-
if (isCPP && Token::simpleMatch(tok2, "final"))
127+
if (_tokenizer->isCPP() && Token::simpleMatch(tok2, "final"))
131128
tok2 = tok2->next();
132129

133130
// make sure we have valid code
@@ -195,7 +192,7 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
195192
}
196193

197194
// definition may be different than declaration
198-
if (isCPP && tok->str() == "class") {
195+
if (_tokenizer->isCPP() && tok->str() == "class") {
199196
access[new_scope] = Private;
200197
new_scope->type = Scope::eClass;
201198
} else if (tok->str() == "struct") {
@@ -272,7 +269,7 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
272269
}
273270

274271
// Namespace and unknown macro (#3854)
275-
else if (isCPP &&
272+
else if (_tokenizer->isCPP() &&
276273
Token::Match(tok, "namespace %name% %type% (") &&
277274
tok->tokAt(2)->isUpperCaseName() &&
278275
Token::simpleMatch(tok->linkAt(3), ") {")) {
@@ -312,7 +309,7 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
312309
}
313310

314311
// using namespace
315-
else if (isCPP && Token::Match(tok, "using namespace ::| %type% ;|::")) {
312+
else if (_tokenizer->isCPP() && Token::Match(tok, "using namespace ::| %type% ;|::")) {
316313
Scope::UsingInfo using_info;
317314

318315
using_info.start = tok; // save location
@@ -332,7 +329,7 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
332329
}
333330

334331
// using type alias
335-
else if (isCPP && Token::Match(tok, "using %name% =")) {
332+
else if (_tokenizer->isCPP() && Token::Match(tok, "using %name% =")) {
336333
if (tok->strAt(-1) != ">" && !findType(tok->next(), scope)) {
337334
// fill typeList..
338335
typeList.emplace_back(tok, nullptr, scope);
@@ -536,7 +533,7 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
536533
}
537534

538535
// friend class declaration?
539-
else if (isCPP && Token::Match(tok, "friend class| ::| %any% ;|::")) {
536+
else if (_tokenizer->isCPP() && Token::Match(tok, "friend class| ::| %any% ;|::")) {
540537
Type::FriendInfo friendInfo;
541538

542539
// save the name start

0 commit comments

Comments
 (0)