From e67fdc7e5b83a842a086d06ad98be78f115bc3f7 Mon Sep 17 00:00:00 2001
From: sss <sss@karma>
Date: Sun, 5 Jan 2025 11:49:36 -0500
Subject: [PATCH] SampleHandler: Fix cppcheck warnings.

Use resize() rather than assigning from substr().
Use starts_with rather than find().
Redundant map lookups.
Classes that manage memory should not have default ctor/assignment.
---
 .../D3PDTools/SampleHandler/Root/DiskListSRM.cxx     |  4 ++--
 .../D3PDTools/SampleHandler/Root/GridTools.cxx       |  4 ++--
 .../D3PDTools/SampleHandler/Root/SampleHandler.cxx   | 11 ++---------
 .../SampleHandler/SampleHandler/DiskWriterLocal.h    |  5 ++++-
 .../D3PDTools/SampleHandler/test/it_GridTools.cxx    | 12 +++---------
 5 files changed, 13 insertions(+), 23 deletions(-)

diff --git a/PhysicsAnalysis/D3PDTools/SampleHandler/Root/DiskListSRM.cxx b/PhysicsAnalysis/D3PDTools/SampleHandler/Root/DiskListSRM.cxx
index 441e6e64b222..a1fc64527ea6 100644
--- a/PhysicsAnalysis/D3PDTools/SampleHandler/Root/DiskListSRM.cxx
+++ b/PhysicsAnalysis/D3PDTools/SampleHandler/Root/DiskListSRM.cxx
@@ -1,5 +1,5 @@
 /*
-  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
+  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
 */
 
 //          
@@ -84,7 +84,7 @@ namespace SH
 	  if (m_file[m_file.size()-1] == '/')
 	  {
 	    m_isDir = true;
-	    m_file = m_file.substr (0, m_file.size()-1);
+            m_file.resize (m_file.size()-1);
 	  } else
 	    m_isDir = false;
 	  return true;
diff --git a/PhysicsAnalysis/D3PDTools/SampleHandler/Root/GridTools.cxx b/PhysicsAnalysis/D3PDTools/SampleHandler/Root/GridTools.cxx
index 75a94c5889d2..e5d3d100a5f4 100644
--- a/PhysicsAnalysis/D3PDTools/SampleHandler/Root/GridTools.cxx
+++ b/PhysicsAnalysis/D3PDTools/SampleHandler/Root/GridTools.cxx
@@ -1,5 +1,5 @@
 /*
-  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
+  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
 */
 
 /// @author Nils Krumnack
@@ -255,7 +255,7 @@ namespace SH
     {
       if (!line.empty())
       {
-	if (line.find ("root:") != 0)
+	if (!line.starts_with ("root:"))
 	  RCU_THROW_MSG ("couldn't parse line: " + line);
 
 	std::string::size_type split1 = line.rfind (":");
diff --git a/PhysicsAnalysis/D3PDTools/SampleHandler/Root/SampleHandler.cxx b/PhysicsAnalysis/D3PDTools/SampleHandler/Root/SampleHandler.cxx
index f777f3bdb70a..35e440e6bab5 100644
--- a/PhysicsAnalysis/D3PDTools/SampleHandler/Root/SampleHandler.cxx
+++ b/PhysicsAnalysis/D3PDTools/SampleHandler/Root/SampleHandler.cxx
@@ -1,5 +1,5 @@
 /*
-  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
+  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
 */
 
 //          
@@ -238,14 +238,7 @@ namespace SH
       RCU_THROW_MSG ("sample " + sample->name() + " not found in SampleHandler");
     if (nameIter->second.get() != sample)
       RCU_THROW_MSG ("different sample of name " + sample->name() + " found in SampleHandler");
-    SamplesMIter sampleIter = m_samples.end();
-    for (SamplesMIter iter = m_samples.begin(), end2 = m_samples.end();
-	 iter != end2; ++ iter)
-    {
-      if (*iter == sample)
-	sampleIter = iter;
-    };
-    m_samples.erase (sampleIter);
+    std::erase (m_samples, sample);
     m_named.erase (nameIter);
   }
 
diff --git a/PhysicsAnalysis/D3PDTools/SampleHandler/SampleHandler/DiskWriterLocal.h b/PhysicsAnalysis/D3PDTools/SampleHandler/SampleHandler/DiskWriterLocal.h
index 426a958b921a..4a9d984ed375 100644
--- a/PhysicsAnalysis/D3PDTools/SampleHandler/SampleHandler/DiskWriterLocal.h
+++ b/PhysicsAnalysis/D3PDTools/SampleHandler/SampleHandler/DiskWriterLocal.h
@@ -1,5 +1,5 @@
 /*
-  Copyright (C) 2002-2019 CERN for the benefit of the ATLAS collaboration
+  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
 */
 
 /// @author Nils Krumnack
@@ -41,6 +41,9 @@ namespace SH
   public:
     DiskWriterLocal (const std::string& val_path);
 
+    DiskWriterLocal (const DiskWriterLocal&) = delete;
+    DiskWriterLocal& operator= (const DiskWriterLocal&) = delete;
+
 
     /// \brief standard destructor
     ///
diff --git a/PhysicsAnalysis/D3PDTools/SampleHandler/test/it_GridTools.cxx b/PhysicsAnalysis/D3PDTools/SampleHandler/test/it_GridTools.cxx
index 29d50567adf7..587e01d250e0 100644
--- a/PhysicsAnalysis/D3PDTools/SampleHandler/test/it_GridTools.cxx
+++ b/PhysicsAnalysis/D3PDTools/SampleHandler/test/it_GridTools.cxx
@@ -1,5 +1,5 @@
 /*
-  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
+  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
 */
 
 /// @author Nils Krumnack
@@ -64,12 +64,8 @@ TEST (GridToolsTest, rucioListDids)
   std::map<std::string,unsigned> types;
   for (auto& entry : entries)
   {
-    if (scopes.find (entry.scope) == scopes.end())
-      scopes[entry.scope] = 0;
-    ++ scopes[entry.scope];
-    if (types.find (entry.type) == types.end())
-      types[entry.type] = 0;
-    ++ types[entry.type];
+    ++scopes[entry.scope];
+    ++types[entry.type];
   }
   EXPECT_EQ (1u, scopes.size());
   EXPECT_TRUE (scopes.find ("user.ivukotic") != scopes.end());
@@ -86,8 +82,6 @@ TEST (GridToolsTest, rucioListFileReplicas)
   std::map<std::string,unsigned> types;
   for (auto& entry : entries)
   {
-    if (scopes.find (entry.scope) == scopes.end())
-      scopes[entry.scope] = 0;
     ++ scopes[entry.scope];
   }
   EXPECT_EQ (1u, scopes.size());
-- 
GitLab