[PATCH] domain_conf: rewrite if else condition

Kristina Hanicova posted 1 patch 1 year, 9 months ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/9291030ce4362808d702ee8fbde12a98c30ce8c2.1658320927.git.khanicov@redhat.com
src/conf/domain_conf.c | 50 +++++++++++++++++++++---------------------
1 file changed, 25 insertions(+), 25 deletions(-)
[PATCH] domain_conf: rewrite if else condition
Posted by Kristina Hanicova 1 year, 9 months ago
This patch prevents nesting of if conditions and makes the code
cleaner.

Signed-off-by: Kristina Hanicova <khanicov@redhat.com>
---
 src/conf/domain_conf.c | 50 +++++++++++++++++++++---------------------
 1 file changed, 25 insertions(+), 25 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 44a01ab628..6b81c61056 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -5309,18 +5309,18 @@ virDomainDeviceAddressParseXML(xmlNodePtr address,
 {
     g_autofree char *type = virXMLPropString(address, "type");
 
-    if (type) {
-        if ((info->type = virDomainDeviceAddressTypeFromString(type)) <= 0) {
-            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
-                           _("unknown address type '%s'"), type);
-            return -1;
-        }
-    } else {
+    if (!type) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        "%s", _("No type specified for device address"));
         return -1;
     }
 
+    if ((info->type = virDomainDeviceAddressTypeFromString(type)) <= 0) {
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+                       _("unknown address type '%s'"), type);
+        return -1;
+    }
+
     switch ((virDomainDeviceAddressType) info->type) {
     case VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI:
         if (virPCIDeviceAddressParseXML(address, &info->addr.pci) < 0)
@@ -5996,20 +5996,20 @@ virDomainHostdevDefParseXMLSubsys(xmlNodePtr node,
      * <hostdev>.  (the functions we're going to call expect address
      * type to already be known).
      */
-    if (type) {
-        if ((def->source.subsys.type
-             = virDomainHostdevSubsysTypeFromString(type)) < 0) {
-            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
-                           _("unknown host device source address type '%s'"),
-                           type);
-            return -1;
-        }
-    } else {
+    if (!type) {
         virReportError(VIR_ERR_XML_ERROR,
                        "%s", _("missing source address type"));
         return -1;
     }
 
+    if ((def->source.subsys.type
+         = virDomainHostdevSubsysTypeFromString(type)) < 0) {
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+                       _("unknown host device source address type '%s'"),
+                       type);
+        return -1;
+    }
+
     if (!(sourcenode = virXPathNode("./source", ctxt))) {
         virReportError(VIR_ERR_XML_ERROR, "%s",
                        _("Missing <source> element in hostdev device"));
@@ -6304,20 +6304,20 @@ virDomainHostdevDefParseXMLCaps(xmlNodePtr node G_GNUC_UNUSED,
      * <hostdev>.  (the functions we're going to call expect address
      * type to already be known).
      */
-    if (type) {
-        if ((def->source.caps.type
-             = virDomainHostdevCapsTypeFromString(type)) < 0) {
-            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
-                           _("unknown host device source address type '%s'"),
-                           type);
-            return -1;
-        }
-    } else {
+    if (!type) {
         virReportError(VIR_ERR_XML_ERROR,
                        "%s", _("missing source address type"));
         return -1;
     }
 
+    if ((def->source.caps.type
+         = virDomainHostdevCapsTypeFromString(type)) < 0) {
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+                       _("unknown host device source address type '%s'"),
+                       type);
+        return -1;
+    }
+
     if (!virXPathNode("./source", ctxt)) {
         virReportError(VIR_ERR_XML_ERROR, "%s",
                        _("Missing <source> element in hostdev device"));
-- 
2.35.3
Re: [PATCH] domain_conf: rewrite if else condition
Posted by Michal Prívozník 1 year, 9 months ago
On 7/20/22 14:42, Kristina Hanicova wrote:
> This patch prevents nesting of if conditions and makes the code
> cleaner.
> 
> Signed-off-by: Kristina Hanicova <khanicov@redhat.com>
> ---
>  src/conf/domain_conf.c | 50 +++++++++++++++++++++---------------------
>  1 file changed, 25 insertions(+), 25 deletions(-)
> 
> diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
> index 44a01ab628..6b81c61056 100644
> --- a/src/conf/domain_conf.c
> +++ b/src/conf/domain_conf.c
> @@ -5309,18 +5309,18 @@ virDomainDeviceAddressParseXML(xmlNodePtr address,
>  {
>      g_autofree char *type = virXMLPropString(address, "type");
>  
> -    if (type) {
> -        if ((info->type = virDomainDeviceAddressTypeFromString(type)) <= 0) {
> -            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
> -                           _("unknown address type '%s'"), type);
> -            return -1;
> -        }
> -    } else {
> +    if (!type) {
>          virReportError(VIR_ERR_INTERNAL_ERROR,
>                         "%s", _("No type specified for device address"));
>          return -1;
>      }
>  
> +    if ((info->type = virDomainDeviceAddressTypeFromString(type)) <= 0) {
> +        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
> +                       _("unknown address type '%s'"), type);
> +        return -1;
> +    }
> +
>      switch ((virDomainDeviceAddressType) info->type) {
>      case VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI:
>          if (virPCIDeviceAddressParseXML(address, &info->addr.pci) < 0)
> @@ -5996,20 +5996,20 @@ virDomainHostdevDefParseXMLSubsys(xmlNodePtr node,
>       * <hostdev>.  (the functions we're going to call expect address
>       * type to already be known).
>       */
> -    if (type) {
> -        if ((def->source.subsys.type
> -             = virDomainHostdevSubsysTypeFromString(type)) < 0) {
> -            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
> -                           _("unknown host device source address type '%s'"),
> -                           type);
> -            return -1;
> -        }
> -    } else {
> +    if (!type) {
>          virReportError(VIR_ERR_XML_ERROR,
>                         "%s", _("missing source address type"));
>          return -1;
>      }
>  
> +    if ((def->source.subsys.type
> +         = virDomainHostdevSubsysTypeFromString(type)) < 0) {

Here, and ....

> +        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
> +                       _("unknown host device source address type '%s'"),
> +                       type);
> +        return -1;
> +    }
> +
>      if (!(sourcenode = virXPathNode("./source", ctxt))) {
>          virReportError(VIR_ERR_XML_ERROR, "%s",
>                         _("Missing <source> element in hostdev device"));
> @@ -6304,20 +6304,20 @@ virDomainHostdevDefParseXMLCaps(xmlNodePtr node G_GNUC_UNUSED,
>       * <hostdev>.  (the functions we're going to call expect address
>       * type to already be known).
>       */
> -    if (type) {
> -        if ((def->source.caps.type
> -             = virDomainHostdevCapsTypeFromString(type)) < 0) {
> -            virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
> -                           _("unknown host device source address type '%s'"),
> -                           type);
> -            return -1;
> -        }
> -    } else {
> +    if (!type) {
>          virReportError(VIR_ERR_XML_ERROR,
>                         "%s", _("missing source address type"));
>          return -1;
>      }
>  
> +    if ((def->source.caps.type
> +         = virDomainHostdevCapsTypeFromString(type)) < 0) {

.. here I'd rather have a slightly longer line than this. I'll fix that
before pushing.

Reviewed-by: Michal Privoznik <mprivozn@redhat.com>

Michal