From: Paul Eggert Date: Mon, 3 Feb 2025 06:09:09 +0000 (-0800) Subject: maint: pacify ‘gcc -Wswitch-enum’ X-Git-Tag: v9.7~51 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=008bb4732b75310f1aaf59e6311888058e81ca77;p=thirdparty%2Fcoreutils.git maint: pacify ‘gcc -Wswitch-enum’ I thought of a way to pacify -Wswitch-enum without much trouble. Either add all the enums, or if that’s too verbose use ‘switch (+E)’ to indicate to the reader that there need not be a case for every enum value. Since this approach improves static checking, make the change everywhere and check it with -Wswitch-enum. * configure.ac: Compile with -Wswitch-enum if it works and --enable-gcc-warnings. No need to remove -Wswitch-default since Gnulib no longer adds it. * src/chmod.c (describe_change): * src/chown-core.c (describe_change): * src/copy.c (copy_debug_string, copy_debug_sparse_string): * src/df.c (decode_output_arg, get_dev): * src/du.c (main): * src/factor.c (print_factors): * src/head.c (diagnose_copy_fd_failure): * src/ls.c (time_type_to_statx, calc_req_mask) (decode_line_length, get_funky_string, parse_ls_color) (gobble_file, print_long_format): * src/split.c (main): * src/sync.c (sync_arg): * src/tr.c (is_char_class_member): * src/wc.c (main): Add switch cases to pacify -Wswitch-enum. * src/copy.c (copy_debug_string, copy_debug_sparse_string): Add unreachable () for unreachable cases. * src/digest.c (main): * src/od.c (decode_one_format): * src/tr.c (get_next, get_spec_stats): switch (E) → switch (+E). * src/digest.c (main): * src/tr.c (get_next): Omit unnecessary ‘default: break;’ that merely pacified GCC, as the new pacification style is better. * src/ls.c (decode_line_length): Add default unreachable case to prevent warning that function might not return a value. (gobble_file): Distinguish DEREF_NEVER from unreachable cases. --- diff --git a/configure.ac b/configure.ac index bf6da2a5a8..661398702d 100644 --- a/configure.ac +++ b/configure.ac @@ -205,8 +205,6 @@ if test $gl_gcc_warnings != no; then nw="$nw -Wredundant-decls" # openat.h declares e.g., mkdirat nw="$nw -Wformat-nonliteral" # who.c and pinky.c strftime uses nw="$nw -Wnested-externs" # use of XARGMATCH/verify_function__ - nw="$nw -Wswitch-enum" # Too many warnings for now - nw="$nw -Wswitch-default" # Too many warnings for now nw="$nw -Wstack-protector" # not worth working around nw="$nw -Wformat-overflow=2" # False alarms due to GCC bug 110333 nw="$nw -Wformat-truncation=2" # False alarm in ls.c, probably related @@ -230,6 +228,7 @@ if test $gl_gcc_warnings != no; then # Code like that still infloops with gcc-4.6.0 and -O2. Scary indeed. gl_MANYWARN_ALL_GCC([ws]) + AS_VAR_APPEND([ws], [' -Wswitch-enum']) gl_MANYWARN_COMPLEMENT([ws], [$ws], [$nw]) for w in $ws; do gl_WARN_ADD([$w]) diff --git a/src/chmod.c b/src/chmod.c index a5a54b1417..1e1cc4cd59 100644 --- a/src/chmod.c +++ b/src/chmod.c @@ -170,7 +170,7 @@ describe_change (char const *file, struct change_status const *ch) printf (_("%s could not be accessed\n"), quoted_file); return; - default: + case CH_FAILED: case CH_NO_CHANGE_REQUESTED: case CH_SUCCEEDED: break; } @@ -196,7 +196,7 @@ describe_change (char const *file, struct change_status const *ch) fmt = _("mode of %s retained as %04lo (%s)\n"); printf (fmt, quoted_file, m, &perms[1]); return; - default: + case CH_NO_STAT: case CH_NOT_APPLIED: default: affirm (false); } printf (fmt, quoted_file, old_m, &old_perms[1], m, &perms[1]); diff --git a/src/chown-core.c b/src/chown-core.c index fab04e6f26..0e08f93e60 100644 --- a/src/chown-core.c +++ b/src/chown-core.c @@ -197,6 +197,7 @@ describe_change (char const *file, enum Change_status changed, : group ? _("group of %s retained as %s\n") : _("ownership of %s retained\n")); break; + case CH_NOT_APPLIED: default: affirm (false); } diff --git a/src/copy.c b/src/copy.c index 7ffb998f6c..22d9830ba6 100644 --- a/src/copy.c +++ b/src/copy.c @@ -160,7 +160,11 @@ copy_debug_string (enum copy_debug_val debug_val) case COPY_DEBUG_YES: return "yes"; case COPY_DEBUG_AVOIDED: return "avoided"; case COPY_DEBUG_UNSUPPORTED: return "unsupported"; - default: return "unknown"; + case COPY_DEBUG_UNKNOWN: return "unknown"; + + case COPY_DEBUG_EXTERNAL: + case COPY_DEBUG_EXTERNAL_INTERNAL: + default: unreachable (); } } @@ -173,7 +177,11 @@ copy_debug_sparse_string (enum copy_debug_val debug_val) case COPY_DEBUG_YES: return "zeros"; case COPY_DEBUG_EXTERNAL: return "SEEK_HOLE"; case COPY_DEBUG_EXTERNAL_INTERNAL: return "SEEK_HOLE + zeros"; - default: return "unknown"; + case COPY_DEBUG_UNKNOWN: return "unknown"; + + case COPY_DEBUG_AVOIDED: + case COPY_DEBUG_UNSUPPORTED: + default: unreachable (); } } diff --git a/src/df.c b/src/df.c index 5c7efd81f8..a969c5c9b9 100644 --- a/src/df.c +++ b/src/df.c @@ -487,6 +487,7 @@ decode_output_arg (char const *arg) alloc_field (field, N_("Avail")); break; + case INVALID_FIELD: default: affirm (!"invalid field"); } @@ -1226,6 +1227,7 @@ get_dev (char const *device, char const *mount_point, char const *file, cell = xstrdup (mount_point); break; + case INVALID_FIELD: default: affirm (!"unhandled field"); } diff --git a/src/digest.c b/src/digest.c index d9810c10bd..1f65c73b48 100644 --- a/src/digest.c +++ b/src/digest.c @@ -1505,7 +1505,7 @@ main (int argc, char **argv) #endif #if HASH_ALGO_CKSUM - switch (cksum_algorithm) + switch (+cksum_algorithm) { case bsd: case sysv: @@ -1516,8 +1516,6 @@ main (int argc, char **argv) _("--check is not supported with " "--algorithm={bsd,sysv,crc,crc32b}")); break; - default: - break; } if (base64_digest && raw_digest) diff --git a/src/du.c b/src/du.c index d87800aa66..0e5d860a92 100644 --- a/src/du.c +++ b/src/du.c @@ -1060,7 +1060,7 @@ main (int argc, char **argv) goto argv_iter_done; case AI_ERR_MEM: xalloc_die (); - default: + case AI_ERR_OK: default: affirm (!"unexpected error code from argv_iter"); } } diff --git a/src/factor.c b/src/factor.c index 37837984f6..1f43ccafe8 100644 --- a/src/factor.c +++ b/src/factor.c @@ -2539,6 +2539,9 @@ print_factors (char const *input) /* Try GMP. */ break; + case LONGINT_INVALID: + case LONGINT_INVALID_SUFFIX_CHAR: + case LONGINT_INVALID_SUFFIX_CHAR_WITH_OVERFLOW: default: error (0, 0, _("%s is not a valid positive integer"), quote (input)); return false; diff --git a/src/head.c b/src/head.c index 062fcd0079..1620f17bc7 100644 --- a/src/head.c +++ b/src/head.c @@ -158,7 +158,7 @@ diagnose_copy_fd_failure (enum Copy_fd_status err, char const *filename) case COPY_FD_UNEXPECTED_EOF: error (0, errno, _("%s: file has shrunk too much"), quotef (filename)); break; - default: + case COPY_FD_OK: default: affirm (false); } } diff --git a/src/ls.c b/src/ls.c index 1bc85f4655..23309b8126 100644 --- a/src/ls.c +++ b/src/ls.c @@ -1160,7 +1160,7 @@ time_type_to_statx (void) return STATX_ATIME; case time_btime: return STATX_BTIME; - default: + case time_numtypes: default: unreachable (); } return 0; @@ -1200,7 +1200,7 @@ calc_req_mask (void) case sort_size: mask |= STATX_SIZE; break; - default: + case sort_numtypes: default: unreachable (); } @@ -1927,8 +1927,13 @@ decode_line_length (char const *spec) case LONGINT_OVERFLOW: return 0; - default: + case LONGINT_INVALID: + case LONGINT_INVALID_SUFFIX_CHAR: + case LONGINT_INVALID_SUFFIX_CHAR_WITH_OVERFLOW: return -1; + + default: + unreachable (); } } @@ -2710,7 +2715,7 @@ get_funky_string (char **dest, char const **src, bool equals_end, state = ST_ERROR; break; - default: + case ST_END: case ST_ERROR: default: unreachable (); } } @@ -2871,7 +2876,7 @@ parse_ls_color (void) case PS_FAIL: goto done; - default: + case PS_DONE: default: affirm (false); } } @@ -3484,10 +3489,13 @@ gobble_file (char const *name, enum filetype type, ino_t inode, } FALLTHROUGH; - default: /* DEREF_NEVER */ + case DEREF_NEVER: err = do_lstat (full_name, &f->stat); do_deref = false; break; + + case DEREF_UNDEFINED: default: + unreachable (); } if (err != 0) @@ -4335,7 +4343,7 @@ print_long_format (const struct fileinfo *f) if (when_timespec.tv_sec == -1 && when_timespec.tv_nsec == -1) btime_ok = false; break; - default: + case time_numtypes: default: unreachable (); } diff --git a/src/od.c b/src/od.c index 7676c8f366..88d467c73c 100644 --- a/src/od.c +++ b/src/od.c @@ -770,7 +770,7 @@ decode_one_format (char const *s_orig, char const *s, char const **next, unreachable (); } - switch (size_spec) + switch (+size_spec) { case CHAR: print_function = (fmt == SIGNED_DECIMAL @@ -879,7 +879,7 @@ decode_one_format (char const *s_orig, char const *s, char const **next, size_t decimal_point_len = (locale->decimal_point[0] ? strlen (locale->decimal_point) : 1); - switch (size_spec) + switch (+size_spec) { case FLOAT_HALF: print_function = fmt == BFLOATING_POINT diff --git a/src/split.c b/src/split.c index f3fec22741..9cbbcccd1e 100644 --- a/src/split.c +++ b/src/split.c @@ -1683,7 +1683,7 @@ main (int argc, char **argv) } break; - default: + case type_undef: default: affirm (false); } diff --git a/src/sync.c b/src/sync.c index 26ebbc8065..7c518b5fae 100644 --- a/src/sync.c +++ b/src/sync.c @@ -139,13 +139,12 @@ sync_arg (enum sync_mode mode, char const *file) sync_status = fsync (fd); break; -#if HAVE_SYNCFS case MODE_FILE_SYSTEM: +#if HAVE_SYNCFS sync_status = syncfs (fd); break; #endif - - default: + case MODE_SYNC: default: unreachable (); } diff --git a/src/tr.c b/src/tr.c index 6219879488..426065d11f 100644 --- a/src/tr.c +++ b/src/tr.c @@ -407,7 +407,7 @@ is_char_class_member (enum Char_class char_class, unsigned char c) case CC_XDIGIT: result = c_isxdigit (c); break; - default: + case CC_NO_CLASS: default: unreachable (); } @@ -1053,7 +1053,7 @@ get_next (struct Spec_list *s, enum Upper_Lower_class *class) case RE_CHAR_CLASS: if (class) { - switch (p->u.char_class) + switch (+p->u.char_class) { case CC_LOWER: *class = UL_LOWER; @@ -1061,8 +1061,6 @@ get_next (struct Spec_list *s, enum Upper_Lower_class *class) case CC_UPPER: *class = UL_UPPER; break; - default: - break; } } @@ -1265,7 +1263,7 @@ get_spec_stats (struct Spec_list *s) for (int i = 0; i < N_CHARS; i++) if (is_char_class_member (p->u.char_class, i)) ++len; - switch (p->u.char_class) + switch (+p->u.char_class) { case CC_UPPER: case CC_LOWER: diff --git a/src/wc.c b/src/wc.c index 7f2e0c0475..ed4ad62c41 100644 --- a/src/wc.c +++ b/src/wc.c @@ -923,7 +923,7 @@ main (int argc, char **argv) case AI_ERR_MEM: xalloc_die (); - default: + case AI_ERR_OK: default: unreachable (); }