Repository: microsoft/PowerToys
File: src/common/utils/gpo.h (C/C++)

Code before:
                {
                    // return value from machine list
                    return string_value;
                }
                else
                {
                    // machine list exists; but value doesn't; therefore return empty string.
                    return "";
                }
            }
            else
            {
                // some error; therefore return empty string.
                return "";
            }
        }

        // If list not exist on machine, we need to check user
        bool user_list_found = false;
        if (auto res = RegOpenKeyExW(POLICIES_SCOPE_USER, POLICIES_PATH.c_str(), 0, KEY_READ, &key); res == ERROR_SUCCESS)
        {
            // If the path was found in the user, we need to check if the value for the policy exists.
            auto resValue = RegQueryValueExW(key, registry_list_enabled_value.c_str(), nullptr, nullptr, reinterpret_cast<LPBYTE>(&value), &valueSize);
            RegCloseKey(key);

            if (resValue != ERROR_SUCCESS)
            {
                // Value not found on the path.
                user_list_found = false;
            }
            else if (value != 1)
            {
                // Value not found on the path.
                user_list_found = false;
            }
            else
            {
                // value found and equals to 1
                user_list_found = true;
            }
        }

        // read value from machine list
        if (user_list_found)
        {
            if (auto res = RegOpenKeyExW(POLICIES_SCOPE_USER, registry_list_path.c_str(), 0, KEY_READ, &key); res == ERROR_SUCCESS)
            {
                // If the path exists in the user, we try to read the value
                DWORD dwType = REG_SZ;
                char string_value[1024];
                DWORD string_value_length = 1024;

Review comment (refactor):
Simplify: 

```
if (resValue == ERROR_SUCCESS) {
if (value == 1) {
user_list_found = true
}
}

otherwise user_list_found is set to false on init.


Code after:

            // If the path exists in the machine registry, we try to read the value.
            auto regValueData = readRegistryStringValue(POLICIES_SCOPE_MACHINE, registry_list_path, registry_list_value_name);

            if (regValueData.has_value())
            {
                // Return the value from the machine list.
                return *regValueData;
            }
        }

        // If no list exists for machine, we try to read from the user list.
        if (!machine_list_found)
        {
            if (RegOpenKeyExW(POLICIES_SCOPE_USER, registry_list_path.c_str(), 0, KEY_READ, &key) == ERROR_SUCCESS)
            {
                RegCloseKey(key);

                // If the path exists in the user registry, we try to read the value.
                auto regValueData = readRegistryStringValue(POLICIES_SCOPE_USER, registry_list_path, registry_list_value_name);

                if (regValueData.has_value())
                {
                    // Return the value from the user list.
                    return *regValueData;
                }
            }
        }

        // No list exists for machine and user, or no value was found in the list, or an error ocurred while reading the value.
        return std::nullopt;
    }

    inline gpo_rule_configured_t getUtilityEnabledValue(const std::wstring& utility_name)
    {
        auto individual_value = getConfiguredValue(utility_name);

        if (individual_value == gpo_rule_configured_disabled || individual_value == gpo_rule_configured_enabled)
        {
            return individual_value;
        }
        else
        {
            return getConfiguredValue(POLICY_CONFIGURE_ENABLED_GLOBAL_ALL_UTILITIES);
        }
    }

    inline gpo_rule_configured_t getConfiguredAlwaysOnTopEnabledValue()
    {
        return getUtilityEnabledValue(POLICY_CONFIGURE_ENABLED_ALWAYS_ON_TOP);
    }