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.
This commit is contained in:
Benedikt Heine 2018-07-07 11:43:44 +02:00
parent 7187290536
commit f4fb95c827
9 changed files with 42 additions and 47 deletions

View File

@ -13,7 +13,7 @@
static char *markup_quote(char *str) static char *markup_quote(char *str)
{ {
assert(str != NULL); assert(str);
str = string_replace_all("&", "&", str); str = string_replace_all("&", "&", 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) static char *markup_unquote(char *str)
{ {
assert(str != NULL); assert(str);
str = string_replace_all(""", "\"", str); str = string_replace_all(""", "\"", 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) static char *markup_br2nl(char *str)
{ {
assert(str != NULL); assert(str);
str = string_replace_all("<br>", "\n", str); str = string_replace_all("<br>", "\n", str);
str = string_replace_all("<br/>", "\n", str); str = string_replace_all("<br/>", "\n", str);
@ -230,9 +230,8 @@ void markup_strip_img(char **str, char **urls)
*/ */
char *markup_strip(char *str) char *markup_strip(char *str)
{ {
if (str == NULL) { if (!str)
return NULL; return NULL;
}
/* strip all tags */ /* strip all tags */
string_strip_delimited(str, '<', '>'); string_strip_delimited(str, '<', '>');
@ -249,9 +248,8 @@ char *markup_strip(char *str)
*/ */
char *markup_transform(char *str, enum markup_mode markup_mode) char *markup_transform(char *str, enum markup_mode markup_mode)
{ {
if (str == NULL) { if (!str)
return NULL; return NULL;
}
switch (markup_mode) { switch (markup_mode) {
case MARKUP_NULL: case MARKUP_NULL:

View File

@ -193,7 +193,7 @@ void dispatch_menu_result(const char *input)
*/ */
void context_menu(void) void context_menu(void)
{ {
if (settings.dmenu_cmd == NULL) { if (!settings.dmenu_cmd) {
LOG_C("Unable to open dmenu: No dmenu command set."); LOG_C("Unable to open dmenu: No dmenu command set.");
return; return;
} }

View File

@ -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 //Comparing raw icons is not supported, assume they are not identical
if (settings.icon_position != icons_off if (settings.icon_position != icons_off
&& (a->raw_icon != NULL || b->raw_icon != NULL)) && (a->raw_icon || b->raw_icon))
return false; return false;
return strcmp(a->appname, b->appname) == 0 return strcmp(a->appname, b->appname) == 0
@ -579,10 +579,10 @@ void notification_do_action(notification *n)
context_menu(); context_menu();
} else if (n->urls) { } else if (n->urls) {
if (strstr(n->urls, "\n") == NULL) if (strstr(n->urls, "\n"))
open_browser(n->urls);
else
context_menu(); context_menu();
else
open_browser(n->urls);
} }
} }

View File

@ -83,9 +83,8 @@ section_t *get_section(const char *name)
void add_entry(const char *section_name, const char *key, const char *value) void add_entry(const char *section_name, const char *key, const char *value)
{ {
section_t *s = get_section(section_name); section_t *s = get_section(section_name);
if (s == NULL) { if (!s)
s = new_section(section_name); s = new_section(section_name);
}
s->entry_count++; s->entry_count++;
int len = 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) int ini_get_int(const char *section, const char *key, int def)
{ {
const char *value = get_value(section, key); const char *value = get_value(section, key);
if (value == NULL) if (value)
return def;
else
return atoi(value); return atoi(value);
else
return def;
} }
double ini_get_double(const char *section, const char *key, double def) double ini_get_double(const char *section, const char *key, double def)
{ {
const char *value = get_value(section, key); const char *value = get_value(section, key);
if (value == NULL) if (value)
return def;
else
return atof(value); return atof(value);
else
return def;
} }
bool ini_is_set(const char *ini_section, const char *ini_key) 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) if (section_count == 0)
return NULL; return NULL;
if (section == NULL) { if (!section)
return sections[0].name; return sections[0].name;
}
for (int i = 0; i < section_count; i++) { for (int i = 0; i < section_count; i++) {
if (strcmp(section, sections[i].name) == 0) { 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) int ini_get_bool(const char *section, const char *key, int def)
{ {
const char *value = get_value(section, key); const char *value = get_value(section, key);
if (value == NULL) if (value) {
return def;
else {
switch (value[0]) { switch (value[0]) {
case 'y': case 'y':
case 'Y': case 'Y':
@ -200,6 +196,8 @@ int ini_get_bool(const char *section, const char *key, int def)
default: default:
return def; return def;
} }
} else {
return def;
} }
} }
@ -350,10 +348,10 @@ char *cmdline_get_string(const char *key, const char *def, const char *descripti
if (str) if (str)
return g_strdup(str); return g_strdup(str);
if (def == NULL) if (def)
return NULL;
else
return g_strdup(def); return g_strdup(def);
else
return NULL;
} }
char *cmdline_get_path(const char *key, const char *def, const char *description) 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); cmdline_usage_append(key, "int", description);
const char *str = cmdline_get_value(key); const char *str = cmdline_get_value(key);
if (str == NULL) if (str)
return def;
else
return atoi(str); return atoi(str);
else
return def;
} }
double cmdline_get_double(const char *key, double def, const char *description) 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); cmdline_usage_append(key, "double", description);
const char *str = cmdline_get_value(key); const char *str = cmdline_get_value(key);
if (str == NULL) if (str)
return def;
else
return atof(str); return atof(str);
else
return def;
} }
int cmdline_get_bool(const char *key, int def, const char *description) int cmdline_get_bool(const char *key, int def, const char *description)

View File

@ -77,7 +77,7 @@ void load_settings(char *cmdline_config_path)
xdgInitHandle(&xdg); xdgInitHandle(&xdg);
if (cmdline_config_path != NULL) { if (cmdline_config_path) {
if (0 == strcmp(cmdline_config_path, "-")) { if (0 == strcmp(cmdline_config_path, "-")) {
config_file = stdin; config_file = stdin;
} else { } else {
@ -88,14 +88,14 @@ void load_settings(char *cmdline_config_path)
DIE("Cannot find config file: '%s'", 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); 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 /* Fall back to just "dunstrc", which was used before 2013-06-23
* (before v0.2). */ * (before v0.2). */
config_file = xdgConfigOpen("dunstrc", "r", &xdg); config_file = xdgConfigOpen("dunstrc", "r", &xdg);
if (config_file == NULL) { if (!config_file) {
LOG_W("No dunstrc found."); LOG_W("No dunstrc found.");
xdgWipeHandle(&xdg); xdgWipeHandle(&xdg);
} }
@ -669,7 +669,7 @@ void load_settings(char *cmdline_config_path)
r = match; r = match;
} }
if (r == NULL) { if (!r) {
r = g_malloc(sizeof(rule_t)); r = g_malloc(sizeof(rule_t));
rule_init(r); rule_init(r);
rules = g_slist_insert(rules, r, -1); rules = g_slist_insert(rules, r, -1);
@ -689,7 +689,7 @@ void load_settings(char *cmdline_config_path)
"markup", NULL "markup", NULL
); );
if (c != NULL) { if (c) {
r->markup = parse_markup_mode(c); r->markup = parse_markup_mode(c);
g_free(c); g_free(c);
} }

View File

@ -14,7 +14,7 @@
char *string_replace_char(char needle, char replacement, char *haystack) char *string_replace_char(char needle, char replacement, char *haystack)
{ {
char *current = haystack; char *current = haystack;
while ((current = strchr(current, needle)) != NULL) while ((current = strchr(current, needle)))
*current++ = replacement; *current++ = replacement;
return haystack; return haystack;
} }
@ -49,9 +49,8 @@ char *string_replace(const char *needle, const char *replacement, char *haystack
{ {
char *start; char *start;
start = strstr(haystack, needle); start = strstr(haystack, needle);
if (start == NULL) { if (!start)
return haystack; return haystack;
}
return string_replace_at(haystack, (start - haystack), strlen(needle), replacement); 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); start = strstr(haystack, needle);
repl_len = strlen(replacement); repl_len = strlen(replacement);
while (start != NULL) { while (start) {
needle_pos = start - haystack; needle_pos = start - haystack;
haystack = string_replace_at(haystack, needle_pos, needle_len, replacement); haystack = string_replace_at(haystack, needle_pos, needle_len, replacement);
start = strstr(haystack + needle_pos + repl_len, needle); start = strstr(haystack + needle_pos + repl_len, needle);

View File

@ -46,7 +46,7 @@ static double get_xft_dpi_value(void)
XrmInitialize(); XrmInitialize();
char *xRMS = XResourceManagerString(xctx.dpy); char *xRMS = XResourceManagerString(xctx.dpy);
if (xRMS == NULL) { if (!xRMS) {
dpi = 0; dpi = 0;
return 0; return 0;
} }

View File

@ -815,7 +815,7 @@ static void x_shortcut_ungrab(keyboard_shortcut *ks)
*/ */
static void x_shortcut_init(keyboard_shortcut *ks) static void x_shortcut_init(keyboard_shortcut *ks)
{ {
if (ks == NULL || ks->str == NULL) if (!ks|| !ks->str)
return; return;
if (!strcmp(ks->str, "none") || (!strcmp(ks->str, ""))) { if (!strcmp(ks->str, "none") || (!strcmp(ks->str, ""))) {

View File

@ -279,7 +279,7 @@ TEST test_option_get_bool(void)
SUITE(suite_option_parser) SUITE(suite_option_parser)
{ {
FILE *config_file = fopen("data/test-ini", "r"); 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); fputs("\nTest config file 'data/test-ini' couldn't be opened, failing.\n", stderr);
exit(1); exit(1);
} }