[PATCH] qemu: Remove unnecessary variables and labels

Kristina Hanicova posted 1 patch 2 weeks ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/8499aac58d960b3806a2a647eb78ca8b7b7b6967.1637685247.git.khanicov@redhat.com
src/driver.c                   |  4 +--
src/qemu/qemu_agent.c          |  4 +--
src/qemu/qemu_alias.c          | 14 +++-----
src/qemu/qemu_capabilities.c   | 21 +++++------
src/qemu/qemu_cgroup.c         |  7 ++--
src/qemu/qemu_conf.c           | 15 ++++----
src/qemu/qemu_domain_address.c |  9 ++---
src/qemu/qemu_driver.c         | 64 ++++++++++++----------------------
8 files changed, 48 insertions(+), 90 deletions(-)

[PATCH] qemu: Remove unnecessary variables and labels

Posted by Kristina Hanicova 2 weeks ago
This patch removes variables such as 'ret', 'rc' and others which
are easily replaced. Therefore, making the code look cleaner and
easier to understand.

Signed-off-by: Kristina Hanicova <khanicov@redhat.com>
---
 src/driver.c                   |  4 +--
 src/qemu/qemu_agent.c          |  4 +--
 src/qemu/qemu_alias.c          | 14 +++-----
 src/qemu/qemu_capabilities.c   | 21 +++++------
 src/qemu/qemu_cgroup.c         |  7 ++--
 src/qemu/qemu_conf.c           | 15 ++++----
 src/qemu/qemu_domain_address.c |  9 ++---
 src/qemu/qemu_driver.c         | 64 ++++++++++++----------------------
 8 files changed, 48 insertions(+), 90 deletions(-)

diff --git a/src/driver.c b/src/driver.c
index 329d493a50..9ae95cb4c3 100644
--- a/src/driver.c
+++ b/src/driver.c
@@ -52,7 +52,6 @@ virDriverLoadModule(const char *name,
                     bool required)
 {
     g_autofree char *modfile = NULL;
-    int ret;
 
     VIR_DEBUG("Module load %s", name);
 
@@ -64,8 +63,7 @@ virDriverLoadModule(const char *name,
                                             "LIBVIRT_DRIVER_DIR")))
         return -1;
 
-    ret = virModuleLoad(modfile, regfunc, required);
-    return ret;
+    return virModuleLoad(modfile, regfunc, required);
 }
 
 
diff --git a/src/qemu/qemu_agent.c b/src/qemu/qemu_agent.c
index c3b02569cd..08436a9705 100644
--- a/src/qemu/qemu_agent.c
+++ b/src/qemu/qemu_agent.c
@@ -176,7 +176,6 @@ qemuAgentOpenUnix(const char *socketpath)
 {
     struct sockaddr_un addr;
     int agentfd;
-    int ret = -1;
 
     if ((agentfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
         virReportSystemError(errno,
@@ -199,8 +198,7 @@ qemuAgentOpenUnix(const char *socketpath)
         goto error;
     }
 
-    ret = connect(agentfd, (struct sockaddr *)&addr, sizeof(addr));
-    if (ret < 0) {
+    if (connect(agentfd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
         virReportSystemError(errno, "%s",
                              _("failed to connect to agent socket"));
         goto error;
diff --git a/src/qemu/qemu_alias.c b/src/qemu/qemu_alias.c
index a36f346592..5e35f43614 100644
--- a/src/qemu/qemu_alias.c
+++ b/src/qemu/qemu_alias.c
@@ -744,16 +744,13 @@ qemuAssignDeviceAliases(virDomainDef *def, virQEMUCaps *qemuCaps)
 char *
 qemuAliasDiskDriveFromDisk(const virDomainDiskDef *disk)
 {
-    char *ret;
-
     if (!disk->info.alias) {
         virReportError(VIR_ERR_INVALID_ARG, "%s",
                        _("disk does not have an alias"));
         return NULL;
     }
 
-    ret = g_strdup_printf("%s%s", QEMU_DRIVE_HOST_PREFIX, disk->info.alias);
-    return ret;
+    return g_strdup_printf("%s%s", QEMU_DRIVE_HOST_PREFIX, disk->info.alias);
 }
 
 
@@ -780,18 +777,15 @@ qemuAliasDiskDriveSkipPrefix(const char *dev_name)
 char *
 qemuAliasFromHostdev(const virDomainHostdevDef *hostdev)
 {
-    char *ret;
-
     if (!hostdev->info->alias) {
         virReportError(VIR_ERR_INVALID_ARG, "%s",
                        _("hostdev does not have an alias"));
         return NULL;
     }
 
-    ret = g_strdup_printf("%s-%s",
-                          virDomainDeviceAddressTypeToString(hostdev->info->type),
-                          hostdev->info->alias);
-    return ret;
+    return g_strdup_printf("%s-%s",
+                           virDomainDeviceAddressTypeToString(hostdev->info->type),
+                           hostdev->info->alias);
 }
 
 
diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index a4c492dde2..33797469a6 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -3070,7 +3070,6 @@ virQEMUCapsGetCPUFeatures(virQEMUCaps *qemuCaps,
     g_auto(GStrv) list = NULL;
     size_t i;
     size_t n;
-    int ret = -1;
 
     *features = NULL;
     modelInfo = virQEMUCapsGetCPUModelInfo(qemuCaps, virtType);
@@ -3091,12 +3090,10 @@ virQEMUCapsGetCPUFeatures(virQEMUCaps *qemuCaps,
     }
 
     *features = g_steal_pointer(&list);
-    if (migratable && !modelInfo->migratability)
-        ret = 1;
-    else
-        ret = 0;
 
-    return ret;
+    if (migratable && !modelInfo->migratability)
+        return 1;
+    return 0;
 }
 
 
@@ -5237,15 +5234,13 @@ virQEMUCapsProbeQMPSchemaCapabilities(virQEMUCaps *qemuCaps,
 virDomainVirtType
 virQEMUCapsGetVirtType(virQEMUCaps *qemuCaps)
 {
-    virDomainVirtType type;
     if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_KVM))
-        type = VIR_DOMAIN_VIRT_KVM;
-    else if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_TCG))
-        type = VIR_DOMAIN_VIRT_QEMU;
-    else
-        type = VIR_DOMAIN_VIRT_NONE;
+        return VIR_DOMAIN_VIRT_KVM;
+
+    if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_TCG))
+        return VIR_DOMAIN_VIRT_QEMU;
 
-    return type;
+    return VIR_DOMAIN_VIRT_NONE;
 }
 
 int
diff --git a/src/qemu/qemu_cgroup.c b/src/qemu/qemu_cgroup.c
index 1e7b562b33..c07058d2f8 100644
--- a/src/qemu/qemu_cgroup.c
+++ b/src/qemu/qemu_cgroup.c
@@ -336,18 +336,15 @@ static int
 qemuSetupTPMCgroup(virDomainObj *vm,
                    virDomainTPMDef *dev)
 {
-    int ret = 0;
-
     switch (dev->type) {
     case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH:
-        ret = qemuSetupChrSourceCgroup(vm, dev->data.passthrough.source);
-        break;
+        return qemuSetupChrSourceCgroup(vm, dev->data.passthrough.source);
     case VIR_DOMAIN_TPM_TYPE_EMULATOR:
     case VIR_DOMAIN_TPM_TYPE_LAST:
         break;
     }
 
-    return ret;
+    return 0;
 }
 
 
diff --git a/src/qemu/qemu_conf.c b/src/qemu/qemu_conf.c
index 0451bc70ac..d31cd05ab4 100644
--- a/src/qemu/qemu_conf.c
+++ b/src/qemu/qemu_conf.c
@@ -1915,16 +1915,13 @@ qemuGetBaseHugepagePath(virQEMUDriver *driver,
                         virHugeTLBFS *hugepage)
 {
     const char *root = driver->embeddedRoot;
-    char *ret;
 
     if (root && !STRPREFIX(hugepage->mnt_dir, root)) {
         g_autofree char * hash = virDomainDriverGenerateRootHash("qemu", root);
-        ret = g_strdup_printf("%s/libvirt/%s", hugepage->mnt_dir, hash);
-    } else {
-        ret = g_strdup_printf("%s/libvirt/qemu", hugepage->mnt_dir);
+        return g_strdup_printf("%s/libvirt/%s", hugepage->mnt_dir, hash);
     }
 
-    return ret;
+    return g_strdup_printf("%s/libvirt/qemu", hugepage->mnt_dir);
 }
 
 
@@ -1935,11 +1932,11 @@ qemuGetDomainHugepagePath(virQEMUDriver *driver,
 {
     g_autofree char *base = qemuGetBaseHugepagePath(driver, hugepage);
     g_autofree char *domPath = virDomainDefGetShortName(def);
-    char *ret = NULL;
 
-    if (base && domPath)
-        ret = g_strdup_printf("%s/%s", base, domPath);
-    return ret;
+    if (!base || !domPath)
+        return NULL;
+
+    return g_strdup_printf("%s/%s", base, domPath);
 }
 
 
diff --git a/src/qemu/qemu_domain_address.c b/src/qemu/qemu_domain_address.c
index 9abe2b84c8..22bf5d3b54 100644
--- a/src/qemu/qemu_domain_address.c
+++ b/src/qemu/qemu_domain_address.c
@@ -2465,7 +2465,6 @@ static int
 qemuDomainAddressFindNewTargetIndex(virDomainDef *def)
 {
     int targetIndex;
-    int ret = -1;
 
     /* Try all indexes between 1 and 31 - QEMU only supports 32
      * PHBs, and 0 is reserved for the default, implicit one */
@@ -2490,13 +2489,11 @@ qemuDomainAddressFindNewTargetIndex(virDomainDef *def)
 
         /* If no existing PCI controller uses this index, great,
          * it means it's free and we can return it to the caller */
-        if (!found) {
-            ret = targetIndex;
-            break;
-        }
+        if (!found)
+            return targetIndex;
     }
 
-    return ret;
+    return -1;
 }
 
 
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index d954635dde..860c5b70a8 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -1332,7 +1332,6 @@ qemuGetSchedInfo(unsigned long long *cpuWait,
     g_autofree char *data = NULL;
     g_auto(GStrv) lines = NULL;
     size_t i;
-    int ret = -1;
     double val;
 
     *cpuWait = 0;
@@ -1344,21 +1343,19 @@ qemuGetSchedInfo(unsigned long long *cpuWait,
     else
         proc = g_strdup_printf("/proc/%d/sched", (int)pid);
     if (!proc)
-        goto cleanup;
-    ret = -1;
+        return -1;
 
     /* The file is not guaranteed to exist (needs CONFIG_SCHED_DEBUG) */
     if (access(proc, R_OK) < 0) {
-        ret = 0;
-        goto cleanup;
+        return 0;
     }
 
     if (virFileReadAll(proc, (1<<16), &data) < 0)
-        goto cleanup;
+        return -1;
 
     lines = g_strsplit(data, "\n", 0);
     if (!lines)
-        goto cleanup;
+        return -1;
 
     for (i = 0; lines[i] != NULL; i++) {
         const char *line = lines[i];
@@ -1372,7 +1369,7 @@ qemuGetSchedInfo(unsigned long long *cpuWait,
                 virReportError(VIR_ERR_INTERNAL_ERROR,
                                _("Missing separator in sched info '%s'"),
                                lines[i]);
-                goto cleanup;
+                return -1;
             }
             line++;
             while (*line == ' ')
@@ -1382,7 +1379,7 @@ qemuGetSchedInfo(unsigned long long *cpuWait,
                 virReportError(VIR_ERR_INTERNAL_ERROR,
                                _("Unable to parse sched info value '%s'"),
                                line);
-                goto cleanup;
+                return -1;
             }
 
             *cpuWait = (unsigned long long)(val * 1000000);
@@ -1390,10 +1387,7 @@ qemuGetSchedInfo(unsigned long long *cpuWait,
         }
     }
 
-    ret = 0;
-
- cleanup:
-    return ret;
+    return 0;
 }
 
 
@@ -3178,7 +3172,6 @@ doCoreDump(virQEMUDriver *driver,
 {
     int fd = -1;
     int ret = -1;
-    int rc = -1;
     virFileWrapperFd *wrapperFd = NULL;
     int directFlag = 0;
     bool needUnlink = false;
@@ -3224,8 +3217,9 @@ doCoreDump(virQEMUDriver *driver,
         if (STREQ(memory_dump_format, "elf"))
             memory_dump_format = NULL;
 
-        rc = qemuDumpToFd(driver, vm, fd, QEMU_ASYNC_JOB_DUMP,
-                          memory_dump_format);
+        if (qemuDumpToFd(driver, vm, fd, QEMU_ASYNC_JOB_DUMP,
+                         memory_dump_format) < 0)
+            goto cleanup;
     } else {
         if (dumpformat != VIR_DOMAIN_CORE_DUMP_FORMAT_RAW) {
             virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
@@ -3237,13 +3231,11 @@ doCoreDump(virQEMUDriver *driver,
         if (!qemuMigrationSrcIsAllowed(driver, vm, false, 0))
             goto cleanup;
 
-        rc = qemuMigrationSrcToFile(driver, vm, fd, compressor,
-                                    QEMU_ASYNC_JOB_DUMP);
+        if (qemuMigrationSrcToFile(driver, vm, fd, compressor,
+                                   QEMU_ASYNC_JOB_DUMP) < 0)
+            goto cleanup;
     }
 
-    if (rc < 0)
-        goto cleanup;
-
     if (VIR_CLOSE(fd) < 0) {
         virReportSystemError(errno,
                              _("unable to close file %s"),
@@ -7225,14 +7217,12 @@ qemuDomainUpdateDeviceLive(virDomainObj *vm,
 {
     virQEMUDriver *driver = dom->conn->privateData;
     virDomainDeviceDef oldDev = { .type = dev->type };
-    int ret = -1;
     int idx;
 
     switch ((virDomainDeviceType)dev->type) {
     case VIR_DOMAIN_DEVICE_DISK:
         qemuDomainObjCheckDiskTaint(driver, vm, dev->data.disk, NULL);
-        ret = qemuDomainChangeDiskLive(vm, dev, driver, force);
-        break;
+        return qemuDomainChangeDiskLive(vm, dev, driver, force);
 
     case VIR_DOMAIN_DEVICE_GRAPHICS:
         if ((idx = qemuDomainFindGraphicsIndex(vm->def, dev->data.graphics)) >= 0) {
@@ -7243,8 +7233,7 @@ qemuDomainUpdateDeviceLive(virDomainObj *vm,
                 return -1;
         }
 
-        ret = qemuDomainChangeGraphics(driver, vm, dev->data.graphics);
-        break;
+        return qemuDomainChangeGraphics(driver, vm, dev->data.graphics);
 
     case VIR_DOMAIN_DEVICE_NET:
         if ((idx = virDomainNetFindIdx(vm->def, dev->data.net)) >= 0) {
@@ -7255,12 +7244,10 @@ qemuDomainUpdateDeviceLive(virDomainObj *vm,
                 return -1;
         }
 
-        ret = qemuDomainChangeNet(driver, vm, dev);
-        break;
+        return qemuDomainChangeNet(driver, vm, dev);
 
     case VIR_DOMAIN_DEVICE_MEMORY:
-        ret = qemuDomainChangeMemoryLive(driver, vm, dev);
-        break;
+        return qemuDomainChangeMemoryLive(driver, vm, dev);
 
     case VIR_DOMAIN_DEVICE_FS:
     case VIR_DOMAIN_DEVICE_INPUT:
@@ -7288,10 +7275,10 @@ qemuDomainUpdateDeviceLive(virDomainObj *vm,
         virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
                        _("live update of device '%s' is not supported"),
                        virDomainDeviceTypeToString(dev->type));
-        break;
+        return -1;
     }
 
-    return ret;
+    return -1;
 }
 
 
@@ -9681,13 +9668,11 @@ qemuGetVcpusBWLive(virDomainObj *vm,
 {
     g_autoptr(virCgroup) cgroup_vcpu = NULL;
     qemuDomainObjPrivate *priv = NULL;
-    int rc;
 
     priv = vm->privateData;
     if (!qemuDomainHasVcpuPids(vm)) {
         /* We do not create sub dir for each vcpu */
-        rc = qemuGetVcpuBWLive(priv->cgroup, period, quota);
-        if (rc < 0)
+        if (qemuGetVcpuBWLive(priv->cgroup, period, quota) < 0)
             return -1;
 
         if (*quota > 0)
@@ -9700,8 +9685,7 @@ qemuGetVcpusBWLive(virDomainObj *vm,
                            false, &cgroup_vcpu) < 0)
         return -1;
 
-    rc = qemuGetVcpuBWLive(cgroup_vcpu, period, quota);
-    if (rc < 0)
+    if (qemuGetVcpuBWLive(cgroup_vcpu, period, quota) < 0)
         return -1;
 
     return 0;
@@ -9731,12 +9715,11 @@ qemuGetIOThreadsBWLive(virDomainObj *vm,
 {
     g_autoptr(virCgroup) cgroup_iothread = NULL;
     qemuDomainObjPrivate *priv = NULL;
-    int rc;
 
     priv = vm->privateData;
     if (!vm->def->niothreadids) {
         /* We do not create sub dir for each iothread */
-        if ((rc = qemuGetVcpuBWLive(priv->cgroup, period, quota)) < 0)
+        if (qemuGetVcpuBWLive(priv->cgroup, period, quota) < 0)
             return -1;
 
         return 0;
@@ -9748,8 +9731,7 @@ qemuGetIOThreadsBWLive(virDomainObj *vm,
                            false, &cgroup_iothread) < 0)
         return -1;
 
-    rc = qemuGetVcpuBWLive(cgroup_iothread, period, quota);
-    if (rc < 0)
+    if (qemuGetVcpuBWLive(cgroup_iothread, period, quota) < 0)
         return -1;
 
     return 0;
-- 
2.31.1

Re: [PATCH] qemu: Remove unnecessary variables and labels

Posted by Michal Prívozník 2 weeks ago
On 11/23/21 17:34, Kristina Hanicova wrote:
> This patch removes variables such as 'ret', 'rc' and others which
> are easily replaced. Therefore, making the code look cleaner and
> easier to understand.
> 
> Signed-off-by: Kristina Hanicova <khanicov@redhat.com>
> ---
>  src/driver.c                   |  4 +--
>  src/qemu/qemu_agent.c          |  4 +--
>  src/qemu/qemu_alias.c          | 14 +++-----
>  src/qemu/qemu_capabilities.c   | 21 +++++------
>  src/qemu/qemu_cgroup.c         |  7 ++--
>  src/qemu/qemu_conf.c           | 15 ++++----
>  src/qemu/qemu_domain_address.c |  9 ++---
>  src/qemu/qemu_driver.c         | 64 ++++++++++++----------------------
>  8 files changed, 48 insertions(+), 90 deletions(-)

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

Michal