cleanup: reduced scope of variables

Cppcheck CI tool identified many variables whose scope could be reduced.

Change-Id: I1478b5680adc560d08bc74ffc91929b1d19e24fa
Signed-off-by: Patrick Venture <venture@google.com>
diff --git a/mainloop.cpp b/mainloop.cpp
index 096e1d4..5fa6404 100644
--- a/mainloop.cpp
+++ b/mainloop.cpp
@@ -359,7 +359,6 @@
         if (attrs.find(hwmon::entry::input) != attrs.end())
         {
             // Read value from sensor.
-            int64_t value;
             std::string input = hwmon::entry::cinput;
             if (i.first.first == "pwm")
             {
@@ -368,6 +367,7 @@
 
             try
             {
+                int64_t value;
                 auto& objInfo = std::get<ObjectInfo>(i.second);
                 auto& obj = std::get<Object>(objInfo);
 
diff --git a/sensor.cpp b/sensor.cpp
index e68f7a7..21f3d87 100644
--- a/sensor.cpp
+++ b/sensor.cpp
@@ -170,8 +170,6 @@
     namespace fs = std::experimental::filesystem;
 
     std::shared_ptr<StatusObject> iface = nullptr;
-    static constexpr bool deferSignals = true;
-    auto& bus = *std::get<sdbusplus::bus::bus*>(info);
     auto& objPath = std::get<std::string>(info);
     auto& obj = std::get<Object>(info);
 
@@ -185,11 +183,10 @@
     if (fs::exists(sysfsFullPath))
     {
         bool functional = true;
-        uint32_t fault = 0;
         try
         {
-            fault = ioAccess.read(faultName, faultID, entry, hwmonio::retries,
-                                  hwmonio::delay);
+            uint32_t fault = ioAccess.read(faultName, faultID, entry,
+                                           hwmonio::retries, hwmonio::delay);
             if (fault != 0)
             {
                 functional = false;
@@ -209,6 +206,9 @@
                 phosphor::logging::entry("FILE=%s", sysfsFullPath.c_str()));
         }
 
+        static constexpr bool deferSignals = true;
+        auto& bus = *std::get<sdbusplus::bus::bus*>(info);
+
         iface =
             std::make_shared<StatusObject>(bus, objPath.c_str(), deferSignals);
         // Set functional property
diff --git a/targets.hpp b/targets.hpp
index 317eec5..9e63351 100644
--- a/targets.hpp
+++ b/targets.hpp
@@ -69,9 +69,7 @@
 {
     std::shared_ptr<T> target;
     namespace fs = std::experimental::filesystem;
-    static constexpr bool deferSignals = true;
 
-    auto& bus = *std::get<sdbusplus::bus::bus*>(info);
     auto& obj = std::get<Object>(info);
     auto& objPath = std::get<std::string>(info);
     auto type = Targets<T>::type;
@@ -159,6 +157,9 @@
                     phosphor::logging::entry("FILE=%s", sysfsFullPath.c_str()));
             }
 
+            static constexpr bool deferSignals = true;
+            auto& bus = *std::get<sdbusplus::bus::bus*>(info);
+
             // ioAccess.path() is a path like: /sys/class/hwmon/hwmon1
             target = std::make_shared<T>(
                 std::move(std::make_unique<hwmonio::HwmonIO>(ioAccess.path())),
diff --git a/thresholds.hpp b/thresholds.hpp
index c483665..8b5329c 100644
--- a/thresholds.hpp
+++ b/thresholds.hpp
@@ -82,9 +82,6 @@
 auto addThreshold(const std::string& sensorType, const std::string& sensorID,
                   int64_t value, ObjectInfo& info)
 {
-    static constexpr bool deferSignals = true;
-
-    auto& bus = *std::get<sdbusplus::bus::bus*>(info);
     auto& objPath = std::get<std::string>(info);
     auto& obj = std::get<Object>(info);
     std::shared_ptr<T> iface;
@@ -93,6 +90,9 @@
     auto tHi = env::getEnv(Thresholds<T>::envHi, sensorType, sensorID);
     if (!tLo.empty() && !tHi.empty())
     {
+        static constexpr bool deferSignals = true;
+        auto& bus = *std::get<sdbusplus::bus::bus*>(info);
+
         iface = std::make_shared<T>(bus, objPath.c_str(), deferSignals);
         auto lo = stoll(tLo);
         auto hi = stoll(tHi);