Skip to contentSkip to conversation

Convert cc_check_return to switch/case

PATCH In review Arne Schwabe 3 participants 3 messages Jul 17, 2020 – Jul 19, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Convert cc_check_return to switch/case

The return false/return true is the result of
running uncrustify.

---
 src/openvpn/multi.c | 24 +++++++++---------------
 1 file changed, 9 insertions(+), 15 deletions(-)
src/openvpn/multi.c +9 -15
index 97b7df16..1fdf6ce5 100644
@@ -2229,22 +2229,16 @@ static inline bool
 cc_check_return(int *cc_succeeded_count,
                 enum client_connect_return ret)
 {
-    if (ret == CC_RET_SUCCEEDED)
+    switch (ret)
     {
-        (*cc_succeeded_count)++;
-        return true;
-    }
-    else if (ret == CC_RET_FAILED)
-    {
-        return false;
-    }
-    else if (ret == CC_RET_SKIPPED)
-    {
-        return true;
-    }
-    else
-    {
-        ASSERT(0);
+        case CC_RET_SUCCEEDED: (*cc_succeeded_count)++;
+            return true;
+
+        case CC_RET_FAILED: return false;
+
+        case CC_RET_SKIPPED: return true;
+
+        default: ASSERT(0);
     }
 }
 
Signature · 1 line
2.26.2
#1 lore raw
Quoted 31 lines · Arne Schwabe

The return false/return true is the result of
running uncrustify.

---
 src/openvpn/multi.c | 24 +++++++++---------------
 1 file changed, 9 insertions(+), 15 deletions(-)
src/openvpn/multi.c +1 -13
index 97b7df16..1fdf6ce5 100644
@@ -2229,22 +2229,16 @@ static inline bool
 cc_check_return(int *cc_succeeded_count,
                 enum client_connect_return ret)
 {
-    if (ret == CC_RET_SUCCEEDED)
+    switch (ret)
     {
-        (*cc_succeeded_count)++;
-        return true;
-    }
-    else if (ret == CC_RET_FAILED)
-    {
-        return false;
-    }
-    else if (ret == CC_RET_SKIPPED)
-    {
-        return true;
-    }
-    else

src/openvpn/multi.c static inline bool · line 2241

Quoted 10 lines
src/openvpn/multi.c +8 -2
-    {
-        ASSERT(0);
+        case CC_RET_SUCCEEDED: (*cc_succeeded_count)++;
+            return true;
+
+        case CC_RET_FAILED: return false;
+
+        case CC_RET_SKIPPED: return true;
+
+        default: ASSERT(0);

Code style police .... Even though it is not clearly defined, but based on the example here
<https://community.openvpn.net/openvpn/wiki/CodeStyle#Casesinaswitchstatementshouldbreakorexplicitlyfallthrough>
...

... it should be more like:

   switch (ret)
   {
        case CC_RET_SUCCEEDED:
            (*cc_succeeded_count)++;
            return true;
        case CC_RET_FAILED:
            return false;

        case CC_RET_SKIPPED:
            return true;

        default:
            ASSERT(0);

}

I generally find this approach more readable.

Signature · 4 lines
kind regards,

David Sommerseth
OpenVPN Inc
#2 lore raw

Hi,

Quoted 3 lines · David Sommerseth
Quoted 2 lines · Arne Schwabe

The return false/return true is the result of
running uncrustify.

[..]

Quoted 5 lines

src/openvpn/multi.c static inline bool · line 2241

Quoted 3 lines
src/openvpn/multi.c +3 -0
+        case CC_RET_SKIPPED: return true;
+
+        default: ASSERT(0);

Code style police ....

Not needed :-) - that code escaped, and the change-to-switch-case has long been merged in proper form.

(For the record, this was discovered and discussed on IRC like "5 minutes after the mail was sent").

gert

Signature · 6 lines
"If was one thing all people took for granted, was conviction that if you 
 feed honest figures into a computer, honest figures come out. Never doubted 
 it myself till I met a computer with a sense of humor."
                             Robert A. Heinlein, The Moon is a Harsh Mistress

Gert Doering - Munich, Germany                             [email protected]...