From 909ca7b0cda09e3fed638129823679add79b59c4 Mon Sep 17 00:00:00 2001 From: firewave Date: Sat, 14 Dec 2024 18:22:46 +0100 Subject: [PATCH 1/2] avoid using a temporary object when inserting into a container --- lib/checkio.cpp | 8 ++++---- lib/checkother.cpp | 4 ++-- lib/checkstl.cpp | 2 +- lib/checkunusedvar.cpp | 2 +- lib/clangimport.cpp | 8 ++++---- lib/symboldatabase.cpp | 4 ++-- lib/symboldatabase.h | 2 +- lib/templatesimplifier.cpp | 4 ++-- lib/valueflow.cpp | 2 +- 9 files changed, 18 insertions(+), 18 deletions(-) diff --git a/lib/checkio.cpp b/lib/checkio.cpp index f0b6feebb4a..198a7d402cc 100644 --- a/lib/checkio.cpp +++ b/lib/checkio.cpp @@ -141,11 +141,11 @@ void CheckIO::checkFileUsage() if (var->isLocal()) { if (var->nameToken()->strAt(1) == "(") // initialize by calling "ctor" - filepointers.insert(std::make_pair(var->declarationId(), Filepointer(OpenMode::UNKNOWN_OM))); + filepointers.emplace(var->declarationId(), Filepointer(OpenMode::UNKNOWN_OM)); else - filepointers.insert(std::make_pair(var->declarationId(), Filepointer(OpenMode::CLOSED))); + filepointers.emplace(var->declarationId(), Filepointer(OpenMode::CLOSED)); } else { - filepointers.insert(std::make_pair(var->declarationId(), Filepointer(OpenMode::UNKNOWN_OM))); + filepointers.emplace(var->declarationId(), Filepointer(OpenMode::UNKNOWN_OM)); // TODO: If all fopen calls we find open the file in the same type, we can set Filepointer::mode } } @@ -282,7 +282,7 @@ void CheckIO::checkFileUsage() continue; if (filepointers.find(fileTok->varId()) == filepointers.end()) { // function call indicates: Its a File - filepointers.insert(std::make_pair(fileTok->varId(), Filepointer(OpenMode::UNKNOWN_OM))); + filepointers.emplace(fileTok->varId(), Filepointer(OpenMode::UNKNOWN_OM)); } Filepointer& f = filepointers[fileTok->varId()]; diff --git a/lib/checkother.cpp b/lib/checkother.cpp index 1b4cb0e1393..204be8d0319 100644 --- a/lib/checkother.cpp +++ b/lib/checkother.cpp @@ -2472,8 +2472,8 @@ void CheckOther::checkInvalidFree() // Keep track of which variables were assigned addresses to newly-allocated memory if ((tok->isCpp() && Token::Match(tok, "%var% = new")) || (Token::Match(tok, "%var% = %name% (") && mSettings->library.getAllocFuncInfo(tok->tokAt(2)))) { - allocation.insert(std::make_pair(tok->varId(), tok->strAt(2))); - inconclusive.insert(std::make_pair(tok->varId(), false)); + allocation.emplace(tok->varId(), tok->strAt(2)); + inconclusive.emplace(tok->varId(), false); } // If a previously-allocated pointer is incremented or decremented, any subsequent diff --git a/lib/checkstl.cpp b/lib/checkstl.cpp index 9bbb80d4f90..23d02e70ab5 100644 --- a/lib/checkstl.cpp +++ b/lib/checkstl.cpp @@ -984,7 +984,7 @@ namespace { void add(const Reference& r) { if (!r.tok) return; - expressions.insert(std::make_pair(r.tok->exprId(), r)); + expressions.emplace(r.tok->exprId(), r); } std::vector invalidTokens() const { diff --git a/lib/checkunusedvar.cpp b/lib/checkunusedvar.cpp index 3e175093a55..c52017a0951 100644 --- a/lib/checkunusedvar.cpp +++ b/lib/checkunusedvar.cpp @@ -263,7 +263,7 @@ void Variables::addVar(const Variable *var, bool write_) { if (var->declarationId() > 0) { - mVarUsage.insert(std::make_pair(var->declarationId(), VariableUsage(var, type, false, write_, false))); + mVarUsage.emplace(var->declarationId(), VariableUsage(var, type, false, write_, false)); } } diff --git a/lib/clangimport.cpp b/lib/clangimport.cpp index fb11991777f..98d162c3007 100644 --- a/lib/clangimport.cpp +++ b/lib/clangimport.cpp @@ -234,26 +234,26 @@ namespace clangimport { void enumDecl(const std::string &addr, Token *nameToken, Enumerator *enumerator) { Decl decl(nameToken, enumerator); - mDeclMap.insert(std::pair(addr, decl)); + mDeclMap.emplace(addr, decl); nameToken->enumerator(enumerator); notFound(addr); } void funcDecl(const std::string &addr, Token *nameToken, Function *function) { Decl decl(nameToken, function); - mDeclMap.insert(std::pair(addr, decl)); + mDeclMap.emplace(addr, decl); nameToken->function(function); notFound(addr); } void scopeDecl(const std::string &addr, Scope *scope) { Decl decl(scope); - mDeclMap.insert(std::pair(addr, decl)); + mDeclMap.emplace(addr, decl); } void varDecl(const std::string &addr, Token *def, Variable *var) { Decl decl(def, var); - mDeclMap.insert(std::pair(addr, decl)); + mDeclMap.emplace(addr, decl); def->varId(++mVarId); def->variable(var); if (def->valueType()) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index a062390926a..291b2a8ed0e 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -1250,13 +1250,13 @@ void SymbolDatabase::fixVarId(VarIdMap & varIds, const Token * vartok, Token * m mVariableList.push_back(membervar); } else mVariableList[membertok->varId()] = membervar; - varIds.insert(std::make_pair(vartok->varId(), memberId)); + varIds.emplace(vartok->varId(), memberId); varId = varIds.find(vartok->varId()); } MemberIdMap::const_iterator memberId = varId->second.find(membervar->nameToken()->varId()); if (memberId == varId->second.cend()) { if (membertok->varId() == 0) { - varId->second.insert(std::make_pair(membervar->nameToken()->varId(), mTokenizer.newVarId())); + varId->second.emplace(membervar->nameToken()->varId(), mTokenizer.newVarId()); mVariableList.push_back(membervar); memberId = varId->second.find(membervar->nameToken()->varId()); } else diff --git a/lib/symboldatabase.h b/lib/symboldatabase.h index 5827ab39295..1a7cda9593a 100644 --- a/lib/symboldatabase.h +++ b/lib/symboldatabase.h @@ -1162,7 +1162,7 @@ class CPPCHECKLIB Scope { const Function * back = &functionList.back(); - functionMap.insert(make_pair(back->tokenDef->str(), back)); + functionMap.emplace(back->tokenDef->str(), back); } AccessControl defaultAccess() const; diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index 4f76648ae92..f3bdb05cc14 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -780,12 +780,12 @@ void TemplateSimplifier::getTemplateInstantiations() for (const auto & decl : mTemplateDeclarations) { if (decl.isFunction()) - functionNameMap.insert(std::make_pair(decl.name(), &decl)); + functionNameMap.emplace(decl.name(), &decl); } for (const auto & decl : mTemplateForwardDeclarations) { if (decl.isFunction()) - functionNameMap.insert(std::make_pair(decl.name(), &decl)); + functionNameMap.emplace(decl.name(), &decl); } const Token *skip = nullptr; diff --git a/lib/valueflow.cpp b/lib/valueflow.cpp index 7d53a0d70b1..1c442d5f17e 100644 --- a/lib/valueflow.cpp +++ b/lib/valueflow.cpp @@ -5933,7 +5933,7 @@ static void valueFlowUninit(TokenList& tokenlist, ErrorLogger& errorLogger, cons Token* tok2 = p.first; const ValueFlow::Value& v = p.second; // Try to insert into map - auto pp = partialReads.insert(std::make_pair(tok2, v)); + auto pp = partialReads.emplace(tok2, v); ValueFlow::Value& v2 = pp.first->second; const bool inserted = pp.second; // Merge the two values if it is already in map From 3161d2a7bcd143f1dd20cfd647be9e7861d75243 Mon Sep 17 00:00:00 2001 From: firewave Date: Sun, 15 Dec 2024 16:31:40 +0100 Subject: [PATCH 2/2] checkio.cpp: fixed `stlFindInsert` selfcheck warning --- lib/checkio.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/checkio.cpp b/lib/checkio.cpp index 198a7d402cc..6e2267ef934 100644 --- a/lib/checkio.cpp +++ b/lib/checkio.cpp @@ -281,9 +281,8 @@ void CheckIO::checkFileUsage() if (!fileTok || !fileTok->varId() || fileTok->strAt(1) == "[") continue; - if (filepointers.find(fileTok->varId()) == filepointers.end()) { // function call indicates: Its a File - filepointers.emplace(fileTok->varId(), Filepointer(OpenMode::UNKNOWN_OM)); - } + // function call indicates: Its a File + filepointers.emplace(fileTok->varId(), Filepointer(OpenMode::UNKNOWN_OM)); Filepointer& f = filepointers[fileTok->varId()];