[PATCH] qemu: Rewrite of code pattern

Kristina Hanicova posted 1 patch 2 weeks ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/4177db960c2cd642df0317084ba170caaa3f0d06.1637691165.git.khanicov@redhat.com
src/qemu/qemu_alias.c | 41 ++++++++++++++++-------------------------
1 file changed, 16 insertions(+), 25 deletions(-)

[PATCH] qemu: Rewrite of code pattern

Posted by Kristina Hanicova 2 weeks ago
This patch rewrites the pattern using early return where it is
not needed.

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

diff --git a/src/qemu/qemu_alias.c b/src/qemu/qemu_alias.c
index 5e35f43614..6f664fcd84 100644
--- a/src/qemu/qemu_alias.c
+++ b/src/qemu/qemu_alias.c
@@ -361,10 +361,9 @@ static int
 qemuAssignDeviceSoundAlias(virDomainSoundDef *sound,
                            int idx)
 {
-    if (sound->info.alias)
-        return 0;
+    if (!sound->info.alias)
+        sound->info.alias = g_strdup_printf("sound%d", idx);
 
-    sound->info.alias = g_strdup_printf("sound%d", idx);
     return 0;
 }
 
@@ -373,10 +372,9 @@ static int
 qemuAssignDeviceVideoAlias(virDomainVideoDef *video,
                            int idx)
 {
-    if (video->info.alias)
-        return 0;
+    if (!video->info.alias)
+        video->info.alias = g_strdup_printf("video%d", idx);
 
-    video->info.alias = g_strdup_printf("video%d", idx);
     return 0;
 }
 
@@ -385,10 +383,9 @@ static int
 qemuAssignDeviceHubAlias(virDomainHubDef *hub,
                          int idx)
 {
-    if (hub->info.alias)
-        return 0;
+    if (!hub->info.alias)
+        hub->info.alias = g_strdup_printf("hub%d", idx);
 
-    hub->info.alias = g_strdup_printf("hub%d", idx);
     return 0;
 }
 
@@ -397,10 +394,9 @@ static int
 qemuAssignDeviceSmartcardAlias(virDomainSmartcardDef *smartcard,
                                int idx)
 {
-    if (smartcard->info.alias)
-        return 0;
+    if (!smartcard->info.alias)
+        smartcard->info.alias = g_strdup_printf("smartcard%d", idx);
 
-    smartcard->info.alias = g_strdup_printf("smartcard%d", idx);
     return 0;
 }
 
@@ -409,10 +405,9 @@ static int
 qemuAssignDeviceMemballoonAlias(virDomainMemballoonDef *memballoon,
                                 int idx)
 {
-    if (memballoon->info.alias)
-        return 0;
+    if (!memballoon->info.alias)
+        memballoon->info.alias = g_strdup_printf("balloon%d", idx);
 
-    memballoon->info.alias = g_strdup_printf("balloon%d", idx);
     return 0;
 }
 
@@ -421,10 +416,9 @@ static int
 qemuAssignDeviceTPMAlias(virDomainTPMDef *tpm,
                          int idx)
 {
-    if (tpm->info.alias)
-        return 0;
+    if (!tpm->info.alias)
+        tpm->info.alias = g_strdup_printf("tpm%d", idx);
 
-    tpm->info.alias = g_strdup_printf("tpm%d", idx);
     return 0;
 }
 
@@ -586,10 +580,8 @@ qemuAssignDeviceWatchdogAlias(virDomainWatchdogDef *watchdog)
 {
     /* Currently, there's just one watchdog per domain */
 
-    if (watchdog->info.alias)
-        return 0;
-
-    watchdog->info.alias = g_strdup("watchdog0");
+    if (!watchdog->info.alias)
+        watchdog->info.alias = g_strdup("watchdog0");
 
     return 0;
 }
@@ -621,9 +613,8 @@ qemuAssignDeviceInputAlias(virDomainDef *def,
 int
 qemuAssignDeviceVsockAlias(virDomainVsockDef *vsock)
 {
-    if (vsock->info.alias)
-        return 0;
-    vsock->info.alias = g_strdup("vsock0");
+    if (!vsock->info.alias)
+        vsock->info.alias = g_strdup("vsock0");
 
     return 0;
 }
-- 
2.31.1

Re: [PATCH] qemu: Rewrite of code pattern

Posted by Martin Kletzander 1 week, 6 days ago
On Tue, Nov 23, 2021 at 07:35:08PM +0100, Kristina Hanicova wrote:
>This patch rewrites the pattern using early return where it is
>not needed.
>

At which point there is probably no need for the functions to return an
integer.  Changing that to void could remove even more code.