[PATCH 5/5] virsh: refactor functions to not use 'ret' variable
Ján Tomko
jtomko at redhat.com
Thu Sep 23 16:22:26 UTC 2021
On a Thursday in 2021, Kristina Hanicova wrote:
>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);
Please put an emtpy line before the successful returns.
>+ return true;
> }
>
> /*
>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);
Rather than duplicating virInterfaceFree, you can create a
virshInterface type and define a cleanup function for it,
as we've done in tools/virsh-util.h for some other public structs.
>+ return false;
> }
>
>+ vshPrint(ctl, "%s", dump);
> virInterfaceFree(iface);
>- return ret;
>+ return true;
> }
>
> /*
Reviewed-by: Ján Tomko <jtomko at redhat.com>
Jano
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 488 bytes
Desc: not available
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20210923/c8416ddb/attachment-0001.sig>
More information about the libvir-list
mailing list