[PATCH 5/5] virsh: refactor functions to not use 'ret' variable

Kristina Hanicova khanicov at redhat.com
Thu Sep 23 15:08:04 UTC 2021


This patch targets smaller functions and rewrites them into the
pattern using an early return without needing redundant 'ret'
variable (if possible).
Patch also includes removal of 'else' branch after 'return' and
other small alterations.

Signed-off-by: Kristina Hanicova <khanicov at redhat.com>
---
 tools/virsh-host.c      | 26 ++++++-----------
 tools/virsh-interface.c | 65 ++++++++++++++++++-----------------------
 tools/virsh-network.c   | 47 +++++++++++++----------------
 tools/virsh-nodedev.c   | 32 ++++++++------------
 tools/virsh-nwfilter.c  | 15 +++++-----
 tools/virsh-util.c      |  3 +-
 6 files changed, 77 insertions(+), 111 deletions(-)

diff --git a/tools/virsh-host.c b/tools/virsh-host.c
index 591746655b..66cd844263 100644
--- a/tools/virsh-host.c
+++ b/tools/virsh-host.c
@@ -1243,7 +1243,6 @@ static bool
 cmdCPUBaseline(vshControl *ctl, const vshCmd *cmd)
 {
     const char *from = NULL;
-    bool ret = false;
     g_autofree char *result = NULL;
     g_auto(GStrv) list = NULL;
     unsigned int flags = 0;
@@ -1260,16 +1259,13 @@ cmdCPUBaseline(vshControl *ctl, const vshCmd *cmd)
     if (!(list = vshExtractCPUDefXMLs(ctl, from)))
         return false;
 
-    result = virConnectBaselineCPU(priv->conn, (const char **)list,
-                                   g_strv_length(list),
-                                   flags);
-
-    if (result) {
-        vshPrint(ctl, "%s", result);
-        ret = true;
-    }
+    if (!(result = virConnectBaselineCPU(priv->conn, (const char **)list,
+                                         g_strv_length(list),
+                                         flags)))
+        return false;
 
-    return ret;
+    vshPrint(ctl, "%s", result);
+    return true;
 }
 
 /*
@@ -1355,7 +1351,6 @@ cmdVersion(vshControl *ctl, const vshCmd *cmd G_GNUC_UNUSED)
     unsigned long includeVersion;
     unsigned long apiVersion;
     unsigned long daemonVersion;
-    int ret;
     unsigned int major;
     unsigned int minor;
     unsigned int rel;
@@ -1375,8 +1370,7 @@ cmdVersion(vshControl *ctl, const vshCmd *cmd G_GNUC_UNUSED)
     vshPrint(ctl, _("Compiled against library: libvirt %d.%d.%d\n"),
              major, minor, rel);
 
-    ret = virGetVersion(&libVersion, hvType, &apiVersion);
-    if (ret < 0) {
+    if (virGetVersion(&libVersion, hvType, &apiVersion) < 0) {
         vshError(ctl, "%s", _("failed to get the library version"));
         return false;
     }
@@ -1394,8 +1388,7 @@ cmdVersion(vshControl *ctl, const vshCmd *cmd G_GNUC_UNUSED)
     vshPrint(ctl, _("Using API: %s %d.%d.%d\n"), hvType,
              major, minor, rel);
 
-    ret = virConnectGetVersion(priv->conn, &hvVersion);
-    if (ret < 0) {
+    if (virConnectGetVersion(priv->conn, &hvVersion) < 0) {
         vshError(ctl, "%s", _("failed to get the hypervisor version"));
         return false;
     }
@@ -1413,8 +1406,7 @@ cmdVersion(vshControl *ctl, const vshCmd *cmd G_GNUC_UNUSED)
     }
 
     if (vshCommandOptBool(cmd, "daemon")) {
-        ret = virConnectGetLibVersion(priv->conn, &daemonVersion);
-        if (ret < 0) {
+        if (virConnectGetLibVersion(priv->conn, &daemonVersion) < 0) {
             vshError(ctl, "%s", _("failed to get the daemon version"));
         } else {
             major = daemonVersion / 1000000;
diff --git a/tools/virsh-interface.c b/tools/virsh-interface.c
index 46af45c97b..f402119b68 100644
--- a/tools/virsh-interface.c
+++ b/tools/virsh-interface.c
@@ -485,26 +485,23 @@ static bool
 cmdInterfaceDumpXML(vshControl *ctl, const vshCmd *cmd)
 {
     virInterfacePtr iface;
-    bool ret = true;
     g_autofree char *dump = NULL;
     unsigned int flags = 0;
-    bool inactive = vshCommandOptBool(cmd, "inactive");
 
-    if (inactive)
+    if (vshCommandOptBool(cmd, "inactive"))
         flags |= VIR_INTERFACE_XML_INACTIVE;
 
     if (!(iface = virshCommandOptInterface(ctl, cmd, NULL)))
         return false;
 
-    dump = virInterfaceGetXMLDesc(iface, flags);
-    if (dump != NULL) {
-        vshPrint(ctl, "%s", dump);
-    } else {
-        ret = false;
+    if (!(dump = virInterfaceGetXMLDesc(iface, flags))) {
+        virInterfaceFree(iface);
+        return false;
     }
 
+    vshPrint(ctl, "%s", dump);
     virInterfaceFree(iface);
-    return ret;
+    return true;
 }
 
 /*
@@ -535,7 +532,6 @@ cmdInterfaceDefine(vshControl *ctl, const vshCmd *cmd)
 {
     virInterfacePtr iface;
     const char *from = NULL;
-    bool ret = true;
     g_autofree char *buffer = NULL;
     unsigned int flags = 0;
     virshControl *priv = ctl->privData;
@@ -549,17 +545,15 @@ cmdInterfaceDefine(vshControl *ctl, const vshCmd *cmd)
     if (virFileReadAll(from, VSH_MAX_XML_FILE, &buffer) < 0)
         return false;
 
-    iface = virInterfaceDefineXML(priv->conn, buffer, flags);
-
-    if (iface != NULL) {
-        vshPrintExtra(ctl, _("Interface %s defined from %s\n"),
-                      virInterfaceGetName(iface), from);
-        virInterfaceFree(iface);
-    } else {
+    if (!(iface = virInterfaceDefineXML(priv->conn, buffer, flags))) {
         vshError(ctl, _("Failed to define interface from %s"), from);
-        ret = false;
+        return false;
     }
-    return ret;
+
+    vshPrintExtra(ctl, _("Interface %s defined from %s\n"),
+                  virInterfaceGetName(iface), from);
+    virInterfaceFree(iface);
+    return true;
 }
 
 /*
@@ -584,21 +578,20 @@ static bool
 cmdInterfaceUndefine(vshControl *ctl, const vshCmd *cmd)
 {
     virInterfacePtr iface;
-    bool ret = true;
     const char *name;
 
     if (!(iface = virshCommandOptInterface(ctl, cmd, &name)))
         return false;
 
-    if (virInterfaceUndefine(iface) == 0) {
-        vshPrintExtra(ctl, _("Interface %s undefined\n"), name);
-    } else {
+    if (virInterfaceUndefine(iface) < 0) {
         vshError(ctl, _("Failed to undefine interface %s"), name);
-        ret = false;
+        virInterfaceFree(iface);
+        return false;
     }
 
+    vshPrintExtra(ctl, _("Interface %s undefined\n"), name);
     virInterfaceFree(iface);
-    return ret;
+    return true;
 }
 
 /*
@@ -623,21 +616,20 @@ static bool
 cmdInterfaceStart(vshControl *ctl, const vshCmd *cmd)
 {
     virInterfacePtr iface;
-    bool ret = true;
     const char *name;
 
     if (!(iface = virshCommandOptInterface(ctl, cmd, &name)))
         return false;
 
-    if (virInterfaceCreate(iface, 0) == 0) {
-        vshPrintExtra(ctl, _("Interface %s started\n"), name);
-    } else {
+    if (virInterfaceCreate(iface, 0) < 0) {
         vshError(ctl, _("Failed to start interface %s"), name);
-        ret = false;
+        virInterfaceFree(iface);
+        return false;
     }
 
+    vshPrintExtra(ctl, _("Interface %s started\n"), name);
     virInterfaceFree(iface);
-    return ret;
+    return true;
 }
 
 /*
@@ -662,21 +654,20 @@ static bool
 cmdInterfaceDestroy(vshControl *ctl, const vshCmd *cmd)
 {
     virInterfacePtr iface;
-    bool ret = true;
     const char *name;
 
     if (!(iface = virshCommandOptInterface(ctl, cmd, &name)))
         return false;
 
-    if (virInterfaceDestroy(iface, 0) == 0) {
-        vshPrintExtra(ctl, _("Interface %s destroyed\n"), name);
-    } else {
+    if (virInterfaceDestroy(iface, 0) < 0) {
         vshError(ctl, _("Failed to destroy interface %s"), name);
-        ret = false;
+        virInterfaceFree(iface);
+        return false;
     }
 
+    vshPrintExtra(ctl, _("Interface %s destroyed\n"), name);
     virInterfaceFree(iface);
-    return ret;
+    return false;
 }
 
 /*
diff --git a/tools/virsh-network.c b/tools/virsh-network.c
index 8651265909..1442210278 100644
--- a/tools/virsh-network.c
+++ b/tools/virsh-network.c
@@ -209,7 +209,6 @@ cmdNetworkCreate(vshControl *ctl, const vshCmd *cmd)
 {
     virNetworkPtr network;
     const char *from = NULL;
-    bool ret = true;
     g_autofree char *buffer = NULL;
     unsigned int flags = 0;
     virshControl *priv = ctl->privData;
@@ -228,15 +227,15 @@ cmdNetworkCreate(vshControl *ctl, const vshCmd *cmd)
     else
         network = virNetworkCreateXML(priv->conn, buffer);
 
-    if (network != NULL) {
-        vshPrintExtra(ctl, _("Network %s created from %s\n"),
-                      virNetworkGetName(network), from);
-        virNetworkFree(network);
-    } else {
+    if (!network) {
         vshError(ctl, _("Failed to create network from %s"), from);
-        ret = false;
+        return false;
     }
-    return ret;
+
+    vshPrintExtra(ctl, _("Network %s created from %s\n"),
+                  virNetworkGetName(network), from);
+    virNetworkFree(network);
+    return true;
 }
 
 /*
@@ -267,7 +266,6 @@ cmdNetworkDefine(vshControl *ctl, const vshCmd *cmd)
 {
     virNetworkPtr network;
     const char *from = NULL;
-    bool ret = true;
     g_autofree char *buffer = NULL;
     unsigned int flags = 0;
     virshControl *priv = ctl->privData;
@@ -286,15 +284,15 @@ cmdNetworkDefine(vshControl *ctl, const vshCmd *cmd)
     else
         network = virNetworkDefineXML(priv->conn, buffer);
 
-    if (network != NULL) {
-        vshPrintExtra(ctl, _("Network %s defined from %s\n"),
-                      virNetworkGetName(network), from);
-        virNetworkFree(network);
-    } else {
+    if (!network) {
         vshError(ctl, _("Failed to define network from %s"), from);
-        ret = false;
+        return false;
     }
-    return ret;
+
+    vshPrintExtra(ctl, _("Network %s defined from %s\n"),
+                  virNetworkGetName(network), from);
+    virNetworkFree(network);
+    return true;
 }
 
 /*
@@ -362,28 +360,23 @@ static bool
 cmdNetworkDumpXML(vshControl *ctl, const vshCmd *cmd)
 {
     virNetworkPtr network;
-    bool ret = true;
     g_autofree char *dump = NULL;
     unsigned int flags = 0;
-    int inactive;
 
     if (!(network = virshCommandOptNetwork(ctl, cmd, NULL)))
         return false;
 
-    inactive = vshCommandOptBool(cmd, "inactive");
-    if (inactive)
+    if (vshCommandOptBool(cmd, "inactive"))
         flags |= VIR_NETWORK_XML_INACTIVE;
 
-    dump = virNetworkGetXMLDesc(network, flags);
-
-    if (dump != NULL) {
-        vshPrint(ctl, "%s", dump);
-    } else {
-        ret = false;
+    if (!(dump = virNetworkGetXMLDesc(network, flags))) {
+        virNetworkFree(network);
+        return false;
     }
 
+    vshPrint(ctl, "%s", dump);
     virNetworkFree(network);
-    return ret;
+    return true;
 }
 
 /*
diff --git a/tools/virsh-nodedev.c b/tools/virsh-nodedev.c
index f1b4eb94bf..f72359121f 100644
--- a/tools/virsh-nodedev.c
+++ b/tools/virsh-nodedev.c
@@ -57,7 +57,6 @@ cmdNodeDeviceCreate(vshControl *ctl, const vshCmd *cmd)
 {
     virNodeDevicePtr dev = NULL;
     const char *from = NULL;
-    bool ret = true;
     g_autofree char *buffer = NULL;
     virshControl *priv = ctl->privData;
 
@@ -67,18 +66,15 @@ cmdNodeDeviceCreate(vshControl *ctl, const vshCmd *cmd)
     if (virFileReadAll(from, VSH_MAX_XML_FILE, &buffer) < 0)
         return false;
 
-    dev = virNodeDeviceCreateXML(priv->conn, buffer, 0);
-
-    if (dev != NULL) {
-        vshPrintExtra(ctl, _("Node device %s created from %s\n"),
-                      virNodeDeviceGetName(dev), from);
-        virNodeDeviceFree(dev);
-    } else {
+    if (!(dev = virNodeDeviceCreateXML(priv->conn, buffer, 0))) {
         vshError(ctl, _("Failed to create node device from %s"), from);
-        ret = false;
+        return false;
     }
 
-    return ret;
+    vshPrintExtra(ctl, _("Node device %s created from %s\n"),
+                  virNodeDeviceGetName(dev), from);
+    virNodeDeviceFree(dev);
+    return true;
 }
 
 
@@ -1077,7 +1073,6 @@ cmdNodeDeviceDefine(vshControl *ctl, const vshCmd *cmd G_GNUC_UNUSED)
 {
     virNodeDevice *dev = NULL;
     const char *from = NULL;
-    bool ret = true;
     g_autofree char *buffer = NULL;
     virshControl *priv = ctl->privData;
 
@@ -1087,18 +1082,15 @@ cmdNodeDeviceDefine(vshControl *ctl, const vshCmd *cmd G_GNUC_UNUSED)
     if (virFileReadAll(from, VSH_MAX_XML_FILE, &buffer) < 0)
         return false;
 
-    dev = virNodeDeviceDefineXML(priv->conn, buffer, 0);
-
-    if (dev != NULL) {
-        vshPrintExtra(ctl, _("Node device '%s' defined from '%s'\n"),
-                      virNodeDeviceGetName(dev), from);
-        virNodeDeviceFree(dev);
-    } else {
+    if (!(dev = virNodeDeviceDefineXML(priv->conn, buffer, 0))) {
         vshError(ctl, _("Failed to define node device from '%s'"), from);
-        ret = false;
+        return false;
     }
 
-    return ret;
+    vshPrintExtra(ctl, _("Node device '%s' defined from '%s'\n"),
+                  virNodeDeviceGetName(dev), from);
+    virNodeDeviceFree(dev);
+    return true;
 }
 
 
diff --git a/tools/virsh-nwfilter.c b/tools/virsh-nwfilter.c
index 77f211d031..33164f623f 100644
--- a/tools/virsh-nwfilter.c
+++ b/tools/virsh-nwfilter.c
@@ -515,7 +515,6 @@ cmdNWFilterBindingCreate(vshControl *ctl, const vshCmd *cmd)
 {
     virNWFilterBindingPtr binding;
     const char *from = NULL;
-    bool ret = true;
     char *buffer;
     unsigned int flags = 0;
     virshControl *priv = ctl->privData;
@@ -532,15 +531,15 @@ cmdNWFilterBindingCreate(vshControl *ctl, const vshCmd *cmd)
     binding = virNWFilterBindingCreateXML(priv->conn, buffer, flags);
     VIR_FREE(buffer);
 
-    if (binding != NULL) {
-        vshPrintExtra(ctl, _("Network filter binding on %s created from %s\n"),
-                      virNWFilterBindingGetPortDev(binding), from);
-        virNWFilterBindingFree(binding);
-    } else {
+    if (!binding) {
         vshError(ctl, _("Failed to create network filter from %s"), from);
-        ret = false;
+        return false;
     }
-    return ret;
+
+    vshPrintExtra(ctl, _("Network filter binding on %s created from %s\n"),
+                  virNWFilterBindingGetPortDev(binding), from);
+    virNWFilterBindingFree(binding);
+    return true;
 }
 
 
diff --git a/tools/virsh-util.c b/tools/virsh-util.c
index 19cd0bcb99..fb74514b3c 100644
--- a/tools/virsh-util.c
+++ b/tools/virsh-util.c
@@ -137,8 +137,7 @@ virshDomainState(vshControl *ctl,
     /* fall back to virDomainGetInfo if virDomainGetState is not supported */
     if (virDomainGetInfo(dom, &info) < 0)
         return -1;
-    else
-        return info.state;
+    return info.state;
 }
 
 
-- 
2.31.1




More information about the libvir-list mailing list