]> www.infradead.org Git - users/sagi/libnvme.git/commitdiff
tree: Use early return instead of else statements
authorMartin Belanger <martin.belanger@dell.com>
Thu, 31 Aug 2023 15:01:51 +0000 (11:01 -0400)
committerDaniel Wagner <wagi@monom.org>
Fri, 1 Sep 2023 09:54:07 +0000 (11:54 +0200)
The code could be simplified and made more readable by using
early returns instead of else statements.

Signed-off-by: Martin Belanger <martin.belanger@dell.com>
src/nvme/tree.c

index a7cebe96629eb8b5c29d9902459733db07ea7561..989fb056401a8a543a40425194dc63e814bce5d3 100644 (file)
@@ -1256,33 +1256,28 @@ struct nvme_ctrl *nvme_create_ctrl(nvme_root_t r,
  */
 static bool _tcp_ctrl_match_host_traddr_no_src_addr(struct nvme_ctrl *c, struct candidate_args *candidate)
 {
-       if (c->cfg.host_traddr) {
-               if (!nvme_ipaddrs_eq(candidate->host_traddr, c->cfg.host_traddr))
-                       return false;
-       } else {
-               /* If c->cfg.host_traddr is NULL, then the controller (c)
-                * uses the interface's primary address as the source
-                * address. If c->cfg.host_iface is defined we can
-                * determine the primary address associated with that
-                * interface and compare that to the candidate->host_traddr.
-                */
-               if (c->cfg.host_iface) {
-                       if (!nvme_iface_primary_addr_matches(candidate->iface_list,
-                                                            c->cfg.host_iface,
-                                                            candidate->host_traddr))
-                               return false;
-               } else {
-                       /* If both c->cfg.host_traddr and c->cfg.host_iface are
-                        * NULL, we don't have enough information to make a
-                        * 100% positive match. Regardless, let's be optimistic
-                        * and assume that we have a match.
-                        */
-                       nvme_msg(root_from_ctrl(c), LOG_DEBUG,
-                                "Not enough data, but assume %s matches candidate's host_traddr: %s\n",
-                                nvme_ctrl_get_name(c),
-                                candidate->host_traddr);
-               }
-       }
+       if (c->cfg.host_traddr)
+               return nvme_ipaddrs_eq(candidate->host_traddr, c->cfg.host_traddr);
+
+       /* If c->cfg.host_traddr is NULL, then the controller (c)
+        * uses the interface's primary address as the source
+        * address. If c->cfg.host_iface is defined we can
+        * determine the primary address associated with that
+        * interface and compare that to the candidate->host_traddr.
+        */
+       if (c->cfg.host_iface)
+               return nvme_iface_primary_addr_matches(candidate->iface_list,
+                                                      c->cfg.host_iface,
+                                                      candidate->host_traddr);
+
+       /* If both c->cfg.host_traddr and c->cfg.host_iface are
+        * NULL, we don't have enough information to make a
+        * 100% positive match. Regardless, let's be optimistic
+        * and assume that we have a match.
+        */
+       nvme_msg(root_from_ctrl(c), LOG_DEBUG,
+                "Not enough data, but assume %s matches candidate's host_traddr: %s\n",
+                nvme_ctrl_get_name(c), candidate->host_traddr);
 
        return true;
 }
@@ -1302,30 +1297,28 @@ static bool _tcp_ctrl_match_host_traddr_no_src_addr(struct nvme_ctrl *c, struct
  */
 static bool _tcp_ctrl_match_host_iface_no_src_addr(struct nvme_ctrl *c, struct candidate_args *candidate)
 {
-       if (c->cfg.host_iface) {
-               if (!streq0(candidate->host_iface, c->cfg.host_iface))
-                       return false;
-       } else {
-               if (c->cfg.host_traddr) {
-                       const char *c_host_iface;
-
-                       c_host_iface = nvme_iface_matching_addr(candidate->iface_list,
-                                                               c->cfg.host_traddr);
-                       if (!streq0(candidate->host_iface, c_host_iface))
-                               return false;
-               } else {
-                       /* If both c->cfg.host_traddr and c->cfg.host_iface are
-                        * NULL, we don't have enough information to make a
-                        * 100% positive match. Regardless, let's be optimistic
-                        * and assume that we have a match.
-                        */
-                       nvme_msg(root_from_ctrl(c), LOG_DEBUG,
-                                "Not enough data, but assume %s matches candidate's host_iface: %s\n",
-                                nvme_ctrl_get_name(c),
-                                candidate->host_iface);
-               }
+       if (c->cfg.host_iface)
+               return streq0(candidate->host_iface, c->cfg.host_iface);
+
+       /* If c->cfg.host_traddr is not NULL we can infer the controller's (c)
+        * interface from it and compare it to the candidate->host_iface.
+        */
+       if (c->cfg.host_traddr) {
+               const char *c_host_iface;
+
+               c_host_iface = nvme_iface_matching_addr(candidate->iface_list, c->cfg.host_traddr);
+               return streq0(candidate->host_iface, c_host_iface);
        }
 
+       /* If both c->cfg.host_traddr and c->cfg.host_iface are
+        * NULL, we don't have enough information to make a
+        * 100% positive match. Regardless, let's be optimistic
+        * and assume that we have a match.
+        */
+       nvme_msg(root_from_ctrl(c), LOG_DEBUG,
+                "Not enough data, but assume %s matches candidate's host_iface: %s\n",
+                nvme_ctrl_get_name(c), candidate->host_iface);
+
        return true;
 }