From: Zbigniew Jędrzejewski-Szmek Date: Wed, 7 Apr 2021 15:01:50 +0000 (+0200) Subject: shared/format-table: use goto to make code flow clear X-Git-Tag: v249-rc1~456^2~4 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=46cbdcd9fe8e6f30f1e3ec3ce4d0360ac4f55243;p=thirdparty%2Fsystemd.git shared/format-table: use goto to make code flow clear gcc 9.3.0 "cc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0" with --optimization=1 was not able to figure out that all cases are covered because r is either set in the switch or type < _TABLE_DATA_TYPE_MAX. But for a human reader this might also not be obvious: the cases are not in exactly the same order as enum definitions, and it's a long list. By using the goto, there should be no doubt, and we avoid checking the condition a second time. --- diff --git a/src/shared/format-table.c b/src/shared/format-table.c index 8d816f4ec6c..abc4bb36516 100644 --- a/src/shared/format-table.c +++ b/src/shared/format-table.c @@ -966,43 +966,43 @@ int table_add_many_internal(Table *t, TableDataType first_type, ...) { size_t w = va_arg(ap, size_t); r = table_set_minimum_width(t, last_cell, w); - break; + goto check; } case TABLE_SET_MAXIMUM_WIDTH: { size_t w = va_arg(ap, size_t); r = table_set_maximum_width(t, last_cell, w); - break; + goto check; } case TABLE_SET_WEIGHT: { unsigned w = va_arg(ap, unsigned); r = table_set_weight(t, last_cell, w); - break; + goto check; } case TABLE_SET_ALIGN_PERCENT: { unsigned p = va_arg(ap, unsigned); r = table_set_align_percent(t, last_cell, p); - break; + goto check; } case TABLE_SET_ELLIPSIZE_PERCENT: { unsigned p = va_arg(ap, unsigned); r = table_set_ellipsize_percent(t, last_cell, p); - break; + goto check; } case TABLE_SET_COLOR: { const char *c = va_arg(ap, const char*); r = table_set_color(t, last_cell, c); - break; + goto check; } case TABLE_SET_RGAP_COLOR: { const char *c = va_arg(ap, const char*); r = table_set_rgap_color(t, last_cell, c); - break; + goto check; } case TABLE_SET_BOTH_COLORS: { @@ -1015,19 +1015,19 @@ int table_add_many_internal(Table *t, TableDataType first_type, ...) { } r = table_set_rgap_color(t, last_cell, c); - break; + goto check; } case TABLE_SET_URL: { const char *u = va_arg(ap, const char*); r = table_set_url(t, last_cell, u); - break; + goto check; } case TABLE_SET_UPPERCASE: { int u = va_arg(ap, int); r = table_set_uppercase(t, last_cell, u); - break; + goto check; } case _TABLE_DATA_TYPE_MAX: @@ -1039,9 +1039,8 @@ int table_add_many_internal(Table *t, TableDataType first_type, ...) { assert_not_reached("Uh? Unexpected data type."); } - if (type < _TABLE_DATA_TYPE_MAX) - r = table_add_cell(t, &last_cell, type, data); - + r = table_add_cell(t, &last_cell, type, data); + check: if (r < 0) { va_end(ap); return r;