From 526c34c0b014489650d9b803113b38e49754ac8c Mon Sep 17 00:00:00 2001 From: Daksh Mamodiya Date: Fri, 5 Jun 2026 16:47:09 +0200 Subject: [PATCH] Source: Minor static-analysis cleanups No behavior change: * Avoid a redundant container lookup before insert by using the result of a single insert()/emplace() instead of find() followed by insert. * Replace self-assigning substr() with resize()/pop_back() to shorten strings in place without an extra allocation. * Remove a redundant nested condition identical to its enclosing if. --- Source/cmDependsC.cxx | 8 ++------ Source/cmDependsFortran.cxx | 2 +- Source/cmExtraEclipseCDT4Generator.cxx | 12 ++++-------- Source/cmGeneratorTarget.cxx | 2 +- Source/cmLinkItemGraphVisitor.cxx | 4 +--- Source/cmLocalUnixMakefileGenerator3.cxx | 4 ++-- Source/cmQtAutoGen.cxx | 2 +- Source/cmQtAutoGenInitializer.cxx | 6 ++---- Source/cmSystemTools.cxx | 4 ++-- 9 files changed, 16 insertions(+), 28 deletions(-) diff --git a/Source/cmDependsC.cxx b/Source/cmDependsC.cxx index 48725dc5e7..b7595bc137 100644 --- a/Source/cmDependsC.cxx +++ b/Source/cmDependsC.cxx @@ -176,9 +176,7 @@ bool cmDependsC::WriteDependencies(std::set const& sources, fileIt->second.Used = true; dependencies.insert(fullName); for (UnscannedEntry const& inc : fileIt->second.UnscannedEntries) { - if (this->Encountered.find(inc.FileName) == - this->Encountered.end()) { - this->Encountered.insert(inc.FileName); + if (this->Encountered.insert(inc.FileName).second) { this->Unscanned.push(inc); } } @@ -382,9 +380,7 @@ void cmDependsC::Scan(std::istream& is, std::string const& directory, // preprocessor-like implementation of this scanner is created. if (this->IncludeRegexScan.find(entry.FileName)) { newCacheEntry.UnscannedEntries.push_back(entry); - if (this->Encountered.find(entry.FileName) == - this->Encountered.end()) { - this->Encountered.insert(entry.FileName); + if (this->Encountered.insert(entry.FileName).second) { this->Unscanned.push(entry); } } diff --git a/Source/cmDependsFortran.cxx b/Source/cmDependsFortran.cxx index 2b85d2b01a..dd5779f436 100644 --- a/Source/cmDependsFortran.cxx +++ b/Source/cmDependsFortran.cxx @@ -87,7 +87,7 @@ cmDependsFortran::cmDependsFortran(cmLocalUnixMakefileGenerator3* lg) for (std::string def : definitions) { std::string::size_type assignment = def.find('='); if (assignment != std::string::npos) { - def = def.substr(0, assignment); + def.resize(assignment); } this->PPDefinitions.insert(def); } diff --git a/Source/cmExtraEclipseCDT4Generator.cxx b/Source/cmExtraEclipseCDT4Generator.cxx index 7427422ddd..c70f3ff9ff 100644 --- a/Source/cmExtraEclipseCDT4Generator.cxx +++ b/Source/cmExtraEclipseCDT4Generator.cxx @@ -591,8 +591,7 @@ void cmExtraEclipseCDT4Generator::AppendIncludeDirectories( dir = frameworkRx.match(1); } - if (emittedDirs.find(dir) == emittedDirs.end()) { - emittedDirs.insert(dir); + if (emittedDirs.insert(dir).second) { xml.StartElement("pathentry"); xml.Attribute("include", cmExtraEclipseCDT4Generator::GetEclipsePath(dir)); @@ -783,8 +782,7 @@ void cmExtraEclipseCDT4Generator::CreateCProjectFile() const } // insert the definition if not already added. - if (emitted.find(def) == emitted.end()) { - emitted.insert(def); + if (emitted.insert(def).second) { xml.StartElement("pathentry"); xml.Attribute("kind", "mac"); xml.Attribute("name", def); @@ -815,8 +813,7 @@ void cmExtraEclipseCDT4Generator::CreateCProjectFile() const } // insert the definition if not already added. - if (emitted.find(def) == emitted.end()) { - emitted.insert(def); + if (emitted.insert(def).second) { xml.StartElement("pathentry"); xml.Attribute("kind", "mac"); xml.Attribute("name", def); @@ -847,8 +844,7 @@ void cmExtraEclipseCDT4Generator::CreateCProjectFile() const } // insert the definition if not already added. - if (emitted.find(def) == emitted.end()) { - emitted.insert(def); + if (emitted.insert(def).second) { xml.StartElement("pathentry"); xml.Attribute("kind", "mac"); xml.Attribute("name", def); diff --git a/Source/cmGeneratorTarget.cxx b/Source/cmGeneratorTarget.cxx index 2db07a5f42..42430a73d1 100644 --- a/Source/cmGeneratorTarget.cxx +++ b/Source/cmGeneratorTarget.cxx @@ -5193,7 +5193,7 @@ std::string cmGeneratorTarget::CheckCMP0004(std::string const& item) const } pos = lib.find_last_not_of(" \t\r\n"); if (pos != std::string::npos) { - lib = lib.substr(0, pos + 1); + lib.resize(pos + 1); } if (lib != item) { cmake* cm = this->LocalGenerator->GetCMakeInstance(); diff --git a/Source/cmLinkItemGraphVisitor.cxx b/Source/cmLinkItemGraphVisitor.cxx index e8bf85ab72..d79b74b687 100644 --- a/Source/cmLinkItemGraphVisitor.cxx +++ b/Source/cmLinkItemGraphVisitor.cxx @@ -134,8 +134,6 @@ void cmLinkItemGraphVisitor::GetDependencies(cmGeneratorTarget const& target, auto const& utilityItems = target.GetUtilityItems(); for (auto const& item : utilityItems) { auto const& name = item.AsStr(); - if (dependencies.find(name) == dependencies.cend()) { - dependencies[name] = Dependency(DependencyType::Utility, item); - } + dependencies.emplace(name, Dependency(DependencyType::Utility, item)); } } diff --git a/Source/cmLocalUnixMakefileGenerator3.cxx b/Source/cmLocalUnixMakefileGenerator3.cxx index dca015b6df..ab6d34565c 100644 --- a/Source/cmLocalUnixMakefileGenerator3.cxx +++ b/Source/cmLocalUnixMakefileGenerator3.cxx @@ -1360,10 +1360,10 @@ std::string cmLocalUnixMakefileGenerator3::CreateMakeVariable( // we must shorten the combined string by 4 characters // keep no more than 24 characters from the second string if (static_cast(str2.size()) > keep) { - str2 = str2.substr(0, keep); + str2.resize(keep); } if (static_cast(str1.size()) + static_cast(str2.size()) > size) { - str1 = str1.substr(0, size - str2.size()); + str1.resize(size - str2.size()); } char buffer[12]; int ni = 0; diff --git a/Source/cmQtAutoGen.cxx b/Source/cmQtAutoGen.cxx index 71fd8de801..40e07a4388 100644 --- a/Source/cmQtAutoGen.cxx +++ b/Source/cmQtAutoGen.cxx @@ -252,7 +252,7 @@ static bool RccListParseOutput(std::string const& rccStdOut, auto StripCR = [](std::string& line) { std::string::size_type cr = line.find('\r'); if (cr != std::string::npos) { - line = line.substr(0, cr); + line.resize(cr); } }; diff --git a/Source/cmQtAutoGenInitializer.cxx b/Source/cmQtAutoGenInitializer.cxx index 19030f18f8..f507fdf5d5 100644 --- a/Source/cmQtAutoGenInitializer.cxx +++ b/Source/cmQtAutoGenInitializer.cxx @@ -2588,10 +2588,8 @@ bool cmQtAutoGenInitializer::GetQtExecutable(GenVarsT& genVars, if (this->MultiConfig && this->UseBetterGraph) { for (auto const& config : this->ConfigsList) { if (!genVars.ExecutableFeatures.Config[config]) { - if (!genVars.ExecutableFeatures.Config[config]) { - print_err(err); - return false; - } + print_err(err); + return false; } } } else { diff --git a/Source/cmSystemTools.cxx b/Source/cmSystemTools.cxx index 41e6c95557..11e534cb73 100644 --- a/Source/cmSystemTools.cxx +++ b/Source/cmSystemTools.cxx @@ -1824,7 +1824,7 @@ bool cmSystemTools::SimpleGlob(std::string const& glob, } std::string path = cmSystemTools::GetFilenamePath(glob); std::string ppath = cmSystemTools::GetFilenameName(glob); - ppath = ppath.substr(0, ppath.size() - 1); + ppath.pop_back(); if (path.empty()) { path = "/"; } @@ -2148,7 +2148,7 @@ cmSystemTools::SaveRestoreEnvironment::~SaveRestoreEnvironment() for (std::string var : currentEnv) { std::string::size_type pos = var.find('='); if (pos != std::string::npos) { - var = var.substr(0, pos); + var.resize(pos); } cmSystemTools::UnsetEnv(var.c_str());