From f4fb95c827a01e32838b59d8230d91a100d8d884 Mon Sep 17 00:00:00 2001 From: Benedikt Heine Date: Sat, 7 Jul 2018 11:43:44 +0200 Subject: [PATCH] Do not make explicit NULL checks on elements Most of the NULL checks are actually doubly false logic. Turning the logic around and removing the NULL check makes the program easier to read. --- src/markup.c | 12 +++++------- src/menu.c | 2 +- src/notification.c | 8 ++++---- src/option_parser.c | 42 ++++++++++++++++++++---------------------- src/settings.c | 12 ++++++------ src/utils.c | 7 +++---- src/x11/screen.c | 2 +- src/x11/x.c | 2 +- test/option_parser.c | 2 +- 9 files changed, 42 insertions(+), 47 deletions(-) diff --git a/src/markup.c b/src/markup.c index 4ed6585..19d97ae 100644 --- a/src/markup.c +++ b/src/markup.c @@ -13,7 +13,7 @@ static char *markup_quote(char *str) { - assert(str != NULL); + assert(str); str = string_replace_all("&", "&", str); str = string_replace_all("\"", """, str); @@ -26,7 +26,7 @@ static char *markup_quote(char *str) static char *markup_unquote(char *str) { - assert(str != NULL); + assert(str); str = string_replace_all(""", "\"", str); str = string_replace_all("'", "'", str); @@ -39,7 +39,7 @@ static char *markup_unquote(char *str) static char *markup_br2nl(char *str) { - assert(str != NULL); + assert(str); str = string_replace_all("
", "\n", str); str = string_replace_all("
", "\n", str); @@ -230,9 +230,8 @@ void markup_strip_img(char **str, char **urls) */ char *markup_strip(char *str) { - if (str == NULL) { + if (!str) return NULL; - } /* strip all tags */ string_strip_delimited(str, '<', '>'); @@ -249,9 +248,8 @@ char *markup_strip(char *str) */ char *markup_transform(char *str, enum markup_mode markup_mode) { - if (str == NULL) { + if (!str) return NULL; - } switch (markup_mode) { case MARKUP_NULL: diff --git a/src/menu.c b/src/menu.c index ee90ed9..a32d55c 100644 --- a/src/menu.c +++ b/src/menu.c @@ -193,7 +193,7 @@ void dispatch_menu_result(const char *input) */ void context_menu(void) { - if (settings.dmenu_cmd == NULL) { + if (!settings.dmenu_cmd) { LOG_C("Unable to open dmenu: No dmenu command set."); return; } diff --git a/src/notification.c b/src/notification.c index 44730ee..7ec03bc 100644 --- a/src/notification.c +++ b/src/notification.c @@ -182,7 +182,7 @@ int notification_is_duplicate(const notification *a, const notification *b) { //Comparing raw icons is not supported, assume they are not identical if (settings.icon_position != icons_off - && (a->raw_icon != NULL || b->raw_icon != NULL)) + && (a->raw_icon || b->raw_icon)) return false; return strcmp(a->appname, b->appname) == 0 @@ -579,10 +579,10 @@ void notification_do_action(notification *n) context_menu(); } else if (n->urls) { - if (strstr(n->urls, "\n") == NULL) - open_browser(n->urls); - else + if (strstr(n->urls, "\n")) context_menu(); + else + open_browser(n->urls); } } diff --git a/src/option_parser.c b/src/option_parser.c index 465754b..df59030 100644 --- a/src/option_parser.c +++ b/src/option_parser.c @@ -83,9 +83,8 @@ section_t *get_section(const char *name) void add_entry(const char *section_name, const char *key, const char *value) { section_t *s = get_section(section_name); - if (s == NULL) { + if (!s) s = new_section(section_name); - } s->entry_count++; int len = s->entry_count; @@ -138,19 +137,19 @@ gint64 ini_get_time(const char *section, const char *key, gint64 def) int ini_get_int(const char *section, const char *key, int def) { const char *value = get_value(section, key); - if (value == NULL) - return def; - else + if (value) return atoi(value); + else + return def; } double ini_get_double(const char *section, const char *key, double def) { const char *value = get_value(section, key); - if (value == NULL) - return def; - else + if (value) return atof(value); + else + return def; } bool ini_is_set(const char *ini_section, const char *ini_key) @@ -163,9 +162,8 @@ const char *next_section(const char *section) if (section_count == 0) return NULL; - if (section == NULL) { + if (!section) return sections[0].name; - } for (int i = 0; i < section_count; i++) { if (strcmp(section, sections[i].name) == 0) { @@ -181,9 +179,7 @@ const char *next_section(const char *section) int ini_get_bool(const char *section, const char *key, int def) { const char *value = get_value(section, key); - if (value == NULL) - return def; - else { + if (value) { switch (value[0]) { case 'y': case 'Y': @@ -200,6 +196,8 @@ int ini_get_bool(const char *section, const char *key, int def) default: return def; } + } else { + return def; } } @@ -350,10 +348,10 @@ char *cmdline_get_string(const char *key, const char *def, const char *descripti if (str) return g_strdup(str); - if (def == NULL) - return NULL; - else + if (def) return g_strdup(def); + else + return NULL; } char *cmdline_get_path(const char *key, const char *def, const char *description) @@ -385,10 +383,10 @@ int cmdline_get_int(const char *key, int def, const char *description) cmdline_usage_append(key, "int", description); const char *str = cmdline_get_value(key); - if (str == NULL) - return def; - else + if (str) return atoi(str); + else + return def; } double cmdline_get_double(const char *key, double def, const char *description) @@ -396,10 +394,10 @@ double cmdline_get_double(const char *key, double def, const char *description) cmdline_usage_append(key, "double", description); const char *str = cmdline_get_value(key); - if (str == NULL) - return def; - else + if (str) return atof(str); + else + return def; } int cmdline_get_bool(const char *key, int def, const char *description) diff --git a/src/settings.c b/src/settings.c index 7f4e584..f278ac6 100644 --- a/src/settings.c +++ b/src/settings.c @@ -77,7 +77,7 @@ void load_settings(char *cmdline_config_path) xdgInitHandle(&xdg); - if (cmdline_config_path != NULL) { + if (cmdline_config_path) { if (0 == strcmp(cmdline_config_path, "-")) { config_file = stdin; } else { @@ -88,14 +88,14 @@ void load_settings(char *cmdline_config_path) DIE("Cannot find config file: '%s'", cmdline_config_path); } } - if (config_file == NULL) { + if (!config_file) { config_file = xdgConfigOpen("dunst/dunstrc", "r", &xdg); } - if (config_file == NULL) { + if (!config_file) { /* Fall back to just "dunstrc", which was used before 2013-06-23 * (before v0.2). */ config_file = xdgConfigOpen("dunstrc", "r", &xdg); - if (config_file == NULL) { + if (!config_file) { LOG_W("No dunstrc found."); xdgWipeHandle(&xdg); } @@ -669,7 +669,7 @@ void load_settings(char *cmdline_config_path) r = match; } - if (r == NULL) { + if (!r) { r = g_malloc(sizeof(rule_t)); rule_init(r); rules = g_slist_insert(rules, r, -1); @@ -689,7 +689,7 @@ void load_settings(char *cmdline_config_path) "markup", NULL ); - if (c != NULL) { + if (c) { r->markup = parse_markup_mode(c); g_free(c); } diff --git a/src/utils.c b/src/utils.c index 6eda3bc..8f06141 100644 --- a/src/utils.c +++ b/src/utils.c @@ -14,7 +14,7 @@ char *string_replace_char(char needle, char replacement, char *haystack) { char *current = haystack; - while ((current = strchr(current, needle)) != NULL) + while ((current = strchr(current, needle))) *current++ = replacement; return haystack; } @@ -49,9 +49,8 @@ char *string_replace(const char *needle, const char *replacement, char *haystack { char *start; start = strstr(haystack, needle); - if (start == NULL) { + if (!start) return haystack; - } return string_replace_at(haystack, (start - haystack), strlen(needle), replacement); } @@ -70,7 +69,7 @@ char *string_replace_all(const char *needle, const char *replacement, char *hays start = strstr(haystack, needle); repl_len = strlen(replacement); - while (start != NULL) { + while (start) { needle_pos = start - haystack; haystack = string_replace_at(haystack, needle_pos, needle_len, replacement); start = strstr(haystack + needle_pos + repl_len, needle); diff --git a/src/x11/screen.c b/src/x11/screen.c index 35df29c..1dfb4d2 100644 --- a/src/x11/screen.c +++ b/src/x11/screen.c @@ -46,7 +46,7 @@ static double get_xft_dpi_value(void) XrmInitialize(); char *xRMS = XResourceManagerString(xctx.dpy); - if (xRMS == NULL) { + if (!xRMS) { dpi = 0; return 0; } diff --git a/src/x11/x.c b/src/x11/x.c index 9311c10..60649d0 100644 --- a/src/x11/x.c +++ b/src/x11/x.c @@ -815,7 +815,7 @@ static void x_shortcut_ungrab(keyboard_shortcut *ks) */ static void x_shortcut_init(keyboard_shortcut *ks) { - if (ks == NULL || ks->str == NULL) + if (!ks|| !ks->str) return; if (!strcmp(ks->str, "none") || (!strcmp(ks->str, ""))) { diff --git a/test/option_parser.c b/test/option_parser.c index d5e283a..f2d0960 100644 --- a/test/option_parser.c +++ b/test/option_parser.c @@ -279,7 +279,7 @@ TEST test_option_get_bool(void) SUITE(suite_option_parser) { FILE *config_file = fopen("data/test-ini", "r"); - if (config_file == NULL) { + if (!config_file) { fputs("\nTest config file 'data/test-ini' couldn't be opened, failing.\n", stderr); exit(1); }