]> granicus.if.org Git - neomutt/commitdiff
notmuch: use guard-clauses pre-condition check
authorAustin Ray <austin@austinray.io>
Sat, 10 Nov 2018 04:24:49 +0000 (23:24 -0500)
committerRichard Russon <rich@flatcap.org>
Tue, 4 Dec 2018 13:19:26 +0000 (13:19 +0000)
Several functions used an if statement with the body of the function
inside the if block. Instead, negate the if statement and return early.
This makes it easier to keep track of invariants.

notmuch/mutt_notmuch.c
notmuch/nm_db.c

index d9f4614efdf0558ae94ffffee59a1d5c8ce4d6fe..cd28eb89ced73b8df22754d19afd3aa8ca5eca7b 100644 (file)
@@ -1699,27 +1699,28 @@ char *nm_uri_from_query(struct Mailbox *m, char *buf, size_t buflen)
   if (!mdata)
   {
     mdata = nm_get_default_data();
+
+    // Failed to get default data.
+    if (!mdata)
+      return NULL;
+
     using_default_data = true;
   }
 
-  if (mdata)
-  {
-    nm_parse_type_from_query(mdata, buf);
+  nm_parse_type_from_query(mdata, buf);
 
-    if (get_limit(mdata) != NmDbLimit)
-    {
-      added = snprintf(uri, sizeof(uri), "%s%s?type=%s&limit=%d&query=", NmUriProtocol,
-                       nm_db_get_filename(m),
-                       query_type_to_string(mdata->query_type), get_limit(mdata));
-    }
-    else
-    {
-      added = snprintf(uri, sizeof(uri), "%s%s?type=%s&query=", NmUriProtocol,
-                       nm_db_get_filename(m), query_type_to_string(mdata->query_type));
-    }
+  if (get_limit(mdata) != NmDbLimit)
+  {
+    added = snprintf(uri, sizeof(uri),
+        "%s%s?type=%s&limit=%d&query=", NmUriProtocol, nm_db_get_filename(m),
+        query_type_to_string(mdata->query_type), get_limit(mdata));
   }
   else
-    return NULL;
+  {
+    added = snprintf(uri, sizeof(uri),
+        "%s%s?type=%s&query=", NmUriProtocol, nm_db_get_filename(m),
+        query_type_to_string(mdata->query_type));
+  }
 
   if (added >= sizeof(uri))
   {
@@ -2100,10 +2101,7 @@ struct Account *nm_ac_find(struct Account *a, const char *path)
  */
 int nm_ac_add(struct Account *a, struct Mailbox *m)
 {
-  if (!a || !m)
-    return -1;
-
-  if (m->magic != MUTT_NOTMUCH)
+  if (!a || !m || m->magic != MUTT_NOTMUCH)
     return -1;
 
   if (!a->adata)
@@ -2518,13 +2516,10 @@ done:
  */
 enum MailboxType nm_path_probe(const char *path, const struct stat *st)
 {
-  if (!path)
+  if (!path || !mutt_str_startswith(path, NmUriProtocol, CASE_IGNORE))
     return MUTT_UNKNOWN;
 
-  if (mutt_str_startswith(path, NmUriProtocol, CASE_IGNORE))
-    return MUTT_NOTMUCH;
-
-  return MUTT_UNKNOWN;
+  return MUTT_NOTMUCH;
 }
 
 /**
index 2ad7fa5432903d8965280a3c061ae9c746664ea8..f5748ec6706b50139fd24e91c7849ea644a5fe9d 100644 (file)
@@ -281,11 +281,11 @@ void nm_db_longrun_init(struct Mailbox *m, bool writable)
 {
   struct NmAccountData *adata = nm_adata_get(m);
 
-  if (adata && nm_db_get(m, writable))
-  {
-    adata->longrun = true;
-    mutt_debug(2, "nm: long run initialized\n");
-  }
+  if (!(adata && nm_db_get(m, writable)))
+    return;
+
+  adata->longrun = true;
+  mutt_debug(2, "nm: long run initialized\n");
 }
 
 /**
@@ -310,9 +310,6 @@ void nm_db_debug_check(struct Mailbox *m)
   if (!adata || !adata->db)
     return;
 
-  if (adata->db)
-  {
-    mutt_debug(1, "nm: ERROR: db is open, closing\n");
-    nm_db_release(m);
-  }
+  mutt_debug(1, "nm: ERROR: db is open, closing\n");
+  nm_db_release(m);
 }