From ad42d4f0ff1886651f254ab833e06cb592be6fbe Mon Sep 17 00:00:00 2001 From: Richard Russon Date: Thu, 24 Oct 2019 01:34:30 +0100 Subject: [PATCH] tidy conditionals 3 - move true condition first - flatten 'else' clause when 'if' clause returns/breaks - compare int functions to zero --- complete.c | 8 +++++--- conn/sasl.c | 4 ++-- conn/ssl.c | 2 +- context.c | 8 +++++--- index.c | 26 ++++++++++++-------------- ncrypt/crypt_gpgme.c | 12 ++++++------ send.c | 15 ++++++--------- test/config/account.c | 12 ++++++------ test/config/enum.c | 18 +++++++++--------- test/config/long.c | 26 +++++++++++++------------- test/config/number.c | 26 +++++++++++++------------- test/config/synonym.c | 8 +++----- 12 files changed, 81 insertions(+), 84 deletions(-) diff --git a/complete.c b/complete.c index e9ec2f3c0..2ff98329a 100644 --- a/complete.c +++ b/complete.c @@ -203,13 +203,15 @@ int mutt_complete(char *buf, size_t buflen) mutt_buffer_strcpy(filepart, de->d_name); /* check to see if it is a directory */ - if (!mutt_buffer_is_empty(dirpart)) + if (mutt_buffer_is_empty(dirpart)) + { + mutt_buffer_reset(tmp); + } + else { mutt_buffer_strcpy(tmp, mutt_b2s(exp_dirpart)); mutt_buffer_addch(tmp, '/'); } - else - mutt_buffer_reset(tmp); mutt_buffer_addstr(tmp, mutt_b2s(filepart)); if ((stat(mutt_b2s(tmp), &st) != -1) && (st.st_mode & S_IFDIR)) mutt_buffer_addch(filepart, '/'); diff --git a/conn/sasl.c b/conn/sasl.c index 1314780f9..1f3606c72 100644 --- a/conn/sasl.c +++ b/conn/sasl.c @@ -567,7 +567,7 @@ int mutt_sasl_client_new(struct Connection *conn, sasl_conn_t **saslconn) } size = sizeof(local); - if (!getsockname(conn->fd, (struct sockaddr *) &local, &size)) + if (getsockname(conn->fd, (struct sockaddr *) &local, &size) == 0) { if (iptostring((struct sockaddr *) &local, size, iplocalport, IP_PORT_BUFLEN) == SASL_OK) plp = iplocalport; @@ -578,7 +578,7 @@ int mutt_sasl_client_new(struct Connection *conn, sasl_conn_t **saslconn) mutt_debug(LL_DEBUG2, "SASL failed to get local IP address\n"); size = sizeof(remote); - if (!getpeername(conn->fd, (struct sockaddr *) &remote, &size)) + if (getpeername(conn->fd, (struct sockaddr *) &remote, &size) == 0) { if (iptostring((struct sockaddr *) &remote, size, ipremoteport, IP_PORT_BUFLEN) == SASL_OK) prp = ipremoteport; diff --git a/conn/ssl.c b/conn/ssl.c index 4017fb295..f14473aef 100644 --- a/conn/ssl.c +++ b/conn/ssl.c @@ -417,7 +417,7 @@ static void x509_fingerprint(char *s, int l, X509 *cert, const EVP_MD *(*hashfun unsigned char md[EVP_MAX_MD_SIZE]; unsigned int n; - if (!X509_digest(cert, hashfunc(), md, &n)) + if (X509_digest(cert, hashfunc(), md, &n) == 0) // Failure { snprintf(s, l, "%s", _("[unable to calculate]")); } diff --git a/context.c b/context.c index 421e76eff..266af48f4 100644 --- a/context.c +++ b/context.c @@ -132,13 +132,15 @@ void ctx_update(struct Context *ctx) e->security = crypt_query(e->content); } - if (!ctx->pattern) + if (ctx->pattern) + { + e->vnum = -1; + } + else { m->v2r[m->vcount] = msgno; e->vnum = m->vcount++; } - else - e->vnum = -1; e->msgno = msgno; if (e->env->supersedes) diff --git a/index.c b/index.c index 04cea7162..9bef52e07 100644 --- a/index.c +++ b/index.c @@ -1998,21 +1998,19 @@ int mutt_index_menu(void) mutt_message(_("Failed to create query, aborting")); break; } - else - { - main_change_folder(menu, op, NULL, buf, sizeof(buf), &oldcount, &index_hint); - // If notmuch doesn't contain the message, we're left in an empty - // vfolder. No messages are found, but nm_read_entire_thread assumes - // a valid message-id and will throw a segfault. - // - // To prevent that, stay in the empty vfolder and print an error. - if (Context->mailbox->msg_count == 0) - { - mutt_error(_("failed to find message in notmuch database. try " - "running 'notmuch new'.")); - break; - } + main_change_folder(menu, op, NULL, buf, sizeof(buf), &oldcount, &index_hint); + + // If notmuch doesn't contain the message, we're left in an empty + // vfolder. No messages are found, but nm_read_entire_thread assumes + // a valid message-id and will throw a segfault. + // + // To prevent that, stay in the empty vfolder and print an error. + if (Context->mailbox->msg_count == 0) + { + mutt_error(_("failed to find message in notmuch database. try " + "running 'notmuch new'.")); + break; } } oldcount = Context->mailbox->msg_count; diff --git a/ncrypt/crypt_gpgme.c b/ncrypt/crypt_gpgme.c index eb5c1b4af..32b512618 100644 --- a/ncrypt/crypt_gpgme.c +++ b/ncrypt/crypt_gpgme.c @@ -2404,17 +2404,17 @@ int pgp_gpgme_decrypt_mime(FILE *fp_in, FILE **fp_out, struct Body *b, struct Bo } *cur = decrypt_part(b, &s, *fp_out, false, &is_signed); - if (!*cur) - { - rc = -1; - mutt_file_fclose(fp_out); - } - else + if (*cur) { rewind(*fp_out); if (is_signed > 0) first_part->goodsig = true; } + else + { + rc = -1; + mutt_file_fclose(fp_out); + } bail: if (need_decode) diff --git a/send.c b/send.c index c749776de..6a006650c 100644 --- a/send.c +++ b/send.c @@ -2434,26 +2434,23 @@ int ci_send_message(SendFlags flags, struct Email *e_templ, const char *tempfile (mutt_addrlist_count_recips(&e_templ->env->cc) == 0) && (mutt_addrlist_count_recips(&e_templ->env->bcc) == 0)) { - if (!(flags & SEND_BATCH)) - { - mutt_error(_("No recipients specified")); - goto main_loop; - } - else + if (flags & SEND_BATCH) { puts(_("No recipients specified")); goto cleanup; } + + mutt_error(_("No recipients specified")); + goto main_loop; } if (mutt_env_to_intl(e_templ->env, &tag, &err)) { mutt_error(_("Bad IDN in '%s': '%s'"), tag, err); FREE(&err); - if (!(flags & SEND_BATCH)) - goto main_loop; - else + if (flags & SEND_BATCH) goto cleanup; + goto main_loop; } if (!e_templ->env->subject && !(flags & SEND_BATCH) && diff --git a/test/config/account.c b/test/config/account.c index fc66b0986..88f466445 100644 --- a/test/config/account.c +++ b/test/config/account.c @@ -149,25 +149,25 @@ void config_account(void) mutt_buffer_reset(&err); rc = cs_subset_string_get(a->sub, he, &err); - if (!TEST_CHECK(CSR_RESULT(rc) == CSR_SUCCESS)) + if (TEST_CHECK(CSR_RESULT(rc) == CSR_SUCCESS)) { - TEST_MSG("%s\n", err.data); + TEST_MSG("%s = %s\n", he->key.strkey, err.data); } else { - TEST_MSG("%s = %s\n", he->key.strkey, err.data); + TEST_MSG("%s\n", err.data); } he = cs_subset_lookup(a->sub, "Cherry"); mutt_buffer_reset(&err); rc = cs_subset_string_get(a->sub, he, &err); - if (!TEST_CHECK(CSR_RESULT(rc) == CSR_SUCCESS)) + if (TEST_CHECK(CSR_RESULT(rc) == CSR_SUCCESS)) { - TEST_MSG("%s\n", err.data); + TEST_MSG("%s = %s\n", he->key.strkey, err.data); } else { - TEST_MSG("%s = %s\n", he->key.strkey, err.data); + TEST_MSG("%s\n", err.data); } const char *name = "fruit:Apple"; diff --git a/test/config/enum.c b/test/config/enum.c index 8638323fc..ba9f6f72b 100644 --- a/test/config/enum.c +++ b/test/config/enum.c @@ -232,15 +232,15 @@ static bool test_string_set(struct ConfigSet *cs, struct Buffer *err) TEST_MSG("Setting %s to %s\n", name, NONULL(invalid[i])); mutt_buffer_reset(err); rc = cs_str_string_set(cs, name, invalid[i], err); - if (!TEST_CHECK(CSR_RESULT(rc) != CSR_SUCCESS)) + if (TEST_CHECK(CSR_RESULT(rc) != CSR_SUCCESS)) { - TEST_MSG("%s = %d, set by '%s'\n", name, VarDamson, invalid[i]); - TEST_MSG("This test should have failed\n"); - return false; + TEST_MSG("Expected error: %s\n", err->data); } else { - TEST_MSG("Expected error: %s\n", err->data); + TEST_MSG("%s = %d, set by '%s'\n", name, VarDamson, invalid[i]); + TEST_MSG("This test should have failed\n"); + return false; } short_line(); } @@ -320,14 +320,14 @@ static bool test_native_set(struct ConfigSet *cs, struct Buffer *err) short_line(); TEST_MSG("Setting %s to %d\n", name, value); rc = cs_str_native_set(cs, name, value, err); - if (!TEST_CHECK((rc & CSR_SUC_NO_CHANGE) != 0)) + if (TEST_CHECK((rc & CSR_SUC_NO_CHANGE) != 0)) { - TEST_MSG("This test should have failed\n"); - return false; + TEST_MSG("Value of %s wasn't changed\n", name); } else { - TEST_MSG("Value of %s wasn't changed\n", name); + TEST_MSG("This test should have failed\n"); + return false; } name = "Hawthorn"; diff --git a/test/config/long.c b/test/config/long.c index 121f71a4f..ead67b59f 100644 --- a/test/config/long.c +++ b/test/config/long.c @@ -203,15 +203,15 @@ static bool test_string_set(struct ConfigSet *cs, struct Buffer *err) TEST_MSG("Setting %s to %s\n", name, NONULL(invalid[i])); mutt_buffer_reset(err); rc = cs_str_string_set(cs, name, invalid[i], err); - if (!TEST_CHECK(CSR_RESULT(rc) != CSR_SUCCESS)) + if (TEST_CHECK(CSR_RESULT(rc) != CSR_SUCCESS)) { - TEST_MSG("%s = %ld, set by '%s'\n", name, VarDamson, NONULL(invalid[i])); - TEST_MSG("This test should have failed\n"); - return false; + TEST_MSG("Expected error: %s\n", err->data); } else { - TEST_MSG("Expected error: %s\n", err->data); + TEST_MSG("%s = %ld, set by '%s'\n", name, VarDamson, NONULL(invalid[i])); + TEST_MSG("This test should have failed\n"); + return false; } short_line(); } @@ -220,14 +220,14 @@ static bool test_string_set(struct ConfigSet *cs, struct Buffer *err) mutt_buffer_reset(err); TEST_MSG("Setting %s to %s\n", name, "-42"); rc = cs_str_string_set(cs, name, "-42", err); - if (!TEST_CHECK(CSR_RESULT(rc) != CSR_SUCCESS)) + if (TEST_CHECK(CSR_RESULT(rc) != CSR_SUCCESS)) { - TEST_MSG("This test should have failed\n"); - return false; + TEST_MSG("Expected error: %s\n", err->data); } else { - TEST_MSG("Expected error: %s\n", err->data); + TEST_MSG("This test should have failed\n"); + return false; } log_line(__func__); @@ -290,14 +290,14 @@ static bool test_native_set(struct ConfigSet *cs, struct Buffer *err) short_line(); TEST_MSG("Setting %s to %ld\n", name, value); rc = cs_str_native_set(cs, name, value, err); - if (!TEST_CHECK((rc & CSR_SUC_NO_CHANGE) != 0)) + if (TEST_CHECK((rc & CSR_SUC_NO_CHANGE) != 0)) { - TEST_MSG("This test should have failed\n"); - return false; + TEST_MSG("Value of %s wasn't changed\n", name); } else { - TEST_MSG("Value of %s wasn't changed\n", name); + TEST_MSG("This test should have failed\n"); + return false; } name = "Hawthorn"; diff --git a/test/config/number.c b/test/config/number.c index 925bf77ab..1e36b03b1 100644 --- a/test/config/number.c +++ b/test/config/number.c @@ -202,15 +202,15 @@ static bool test_string_set(struct ConfigSet *cs, struct Buffer *err) TEST_MSG("Setting %s to %s\n", name, NONULL(invalid[i])); mutt_buffer_reset(err); rc = cs_str_string_set(cs, name, invalid[i], err); - if (!TEST_CHECK(CSR_RESULT(rc) != CSR_SUCCESS)) + if (TEST_CHECK(CSR_RESULT(rc) != CSR_SUCCESS)) { - TEST_MSG("%s = %d, set by '%s'\n", name, VarDamson, invalid[i]); - TEST_MSG("This test should have failed\n"); - return false; + TEST_MSG("Expected error: %s\n", err->data); } else { - TEST_MSG("Expected error: %s\n", err->data); + TEST_MSG("%s = %d, set by '%s'\n", name, VarDamson, invalid[i]); + TEST_MSG("This test should have failed\n"); + return false; } short_line(); } @@ -219,14 +219,14 @@ static bool test_string_set(struct ConfigSet *cs, struct Buffer *err) mutt_buffer_reset(err); TEST_MSG("Setting %s to %s\n", name, "-42"); rc = cs_str_string_set(cs, name, "-42", err); - if (!TEST_CHECK(CSR_RESULT(rc) != CSR_SUCCESS)) + if (TEST_CHECK(CSR_RESULT(rc) != CSR_SUCCESS)) { - TEST_MSG("This test should have failed\n"); - return false; + TEST_MSG("Expected error: %s\n", err->data); } else { - TEST_MSG("Expected error: %s\n", err->data); + TEST_MSG("This test should have failed\n"); + return false; } log_line(__func__); @@ -289,14 +289,14 @@ static bool test_native_set(struct ConfigSet *cs, struct Buffer *err) short_line(); TEST_MSG("Setting %s to %d\n", name, value); rc = cs_str_native_set(cs, name, value, err); - if (!TEST_CHECK((rc & CSR_SUC_NO_CHANGE) != 0)) + if (TEST_CHECK((rc & CSR_SUC_NO_CHANGE) != 0)) { - TEST_MSG("This test should have failed\n"); - return false; + TEST_MSG("Value of %s wasn't changed\n", name); } else { - TEST_MSG("Value of %s wasn't changed\n", name); + TEST_MSG("This test should have failed\n"); + return false; } name = "Hawthorn"; diff --git a/test/config/synonym.c b/test/config/synonym.c index 3347f9115..3b504a4ad 100644 --- a/test/config/synonym.c +++ b/test/config/synonym.c @@ -194,16 +194,14 @@ void config_synonym(void) if (!cs_register_variables(cs, Vars, 0)) return; - if (!cs_register_variables(cs, Vars2, 0)) - { - TEST_MSG("Expected error\n"); - } - else + if (cs_register_variables(cs, Vars2, 0)) { TEST_MSG("Test should have failed\n"); return; } + TEST_MSG("Expected error\n"); + notify_observer_add(cs->notify, NT_CONFIG, 0, log_observer, 0); set_list(cs); -- 2.40.0