Change variable scopes

cppcheck correctly notes that a lot of our variables can be declared at
more specific scopes, and in every case, it seems to be correct.

Tested: Redfish service validator passes.  Unit test coverage on others.

Signed-off-by: Ed Tanous <edtanous@google.com>
Change-Id: Ia4414410d0e8f74a3bd40fdc0e0232450d1a6416
diff --git a/redfish-core/include/utils/chassis_utils.hpp b/redfish-core/include/utils/chassis_utils.hpp
index 0acbf33..b7f16d8 100644
--- a/redfish-core/include/utils/chassis_utils.hpp
+++ b/redfish-core/include/utils/chassis_utils.hpp
@@ -35,11 +35,10 @@
         }
 
         std::optional<std::string> chassisPath;
-        std::string chassisName;
         for (const std::string& chassis : chassisPaths)
         {
             sdbusplus::message::object_path path(chassis);
-            chassisName = path.filename();
+            std::string chassisName = path.filename();
             if (chassisName.empty())
             {
                 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;
diff --git a/redfish-core/lib/health.hpp b/redfish-core/lib/health.hpp
index 2f0f156..7955fc0 100644
--- a/redfish-core/lib/health.hpp
+++ b/redfish-core/lib/health.hpp
@@ -67,7 +67,6 @@
 
         for (const auto& [path, interfaces] : statuses)
         {
-            bool isChild = false;
             bool isSelf = false;
             if (selfPath)
             {
@@ -86,6 +85,7 @@
                 // of this association is an inventory item, or one of the
                 // endpoints in this association is a child
 
+                bool isChild = false;
                 for (const std::string& child : inventory)
                 {
                     if (path.str.starts_with(child))
diff --git a/redfish-core/lib/hypervisor_system.hpp b/redfish-core/lib/hypervisor_system.hpp
index 78bf626..2b27fb4 100644
--- a/redfish-core/lib/hypervisor_system.hpp
+++ b/redfish-core/lib/hypervisor_system.hpp
@@ -585,11 +585,10 @@
     // address
     const nlohmann::json& thisJson = input[0];
 
-    // For the error string
-    std::string pathString = "IPv4StaticAddresses/1";
-
     if (!thisJson.is_null() && !thisJson.empty())
     {
+        // For the error string
+        std::string pathString = "IPv4StaticAddresses/1";
         std::optional<std::string> address;
         std::optional<std::string> subnetMask;
         std::optional<std::string> gateway;
diff --git a/redfish-core/lib/power.hpp b/redfish-core/lib/power.hpp
index 47f0c5d..33aefa3 100644
--- a/redfish-core/lib/power.hpp
+++ b/redfish-core/lib/power.hpp
@@ -273,9 +273,6 @@
                     }
                 }
 
-                nlohmann::json& value =
-                    sensorJson["PowerLimit"]["LimitInWatts"];
-
                 // LimitException is Mandatory attribute as per OCP
                 // Baseline Profile – v1.0.0, so currently making it
                 // "NoAction" as default value to make it OCP Compliant.
@@ -285,7 +282,12 @@
                 {
                     // Redfish specification indicates PowerLimit should
                     // be null if the limit is not enabled.
-                    value = powerCap * std::pow(10, scale);
+                    sensorJson["PowerLimit"]["LimitInWatts"] =
+                        powerCap * std::pow(10, scale);
+                }
+                else
+                {
+                    sensorJson["PowerLimit"]["LimitInWatts"] = nullptr;
                 }
             };
 
diff --git a/redfish-core/lib/processor.hpp b/redfish-core/lib/processor.hpp
index b6c500e..d9c26f3 100644
--- a/redfish-core/lib/processor.hpp
+++ b/redfish-core/lib/processor.hpp
@@ -1294,9 +1294,9 @@
             return;
         }
 
-        std::string appliedConfigUri;
         if (appliedConfigJson)
         {
+            std::string appliedConfigUri;
             if (!json_util::readJson(*appliedConfigJson, asyncResp->res,
                                      "@odata.id", appliedConfigUri))
             {
diff --git a/redfish-core/lib/sensors.hpp b/redfish-core/lib/sensors.hpp
index 3cd4379..627ba7e 100644
--- a/redfish-core/lib/sensors.hpp
+++ b/redfish-core/lib/sensors.hpp
@@ -531,11 +531,10 @@
             return;
         }
         const std::string* chassisPath = nullptr;
-        std::string chassisName;
         for (const std::string& chassis : chassisPaths)
         {
             sdbusplus::message::object_path path(chassis);
-            chassisName = path.filename();
+            std::string chassisName = path.filename();
             if (chassisName.empty())
             {
                 BMCWEB_LOG_ERROR << "Failed to find '/' in " << chassis;