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.
This commit is contained in:
Paul Eggert 2025-02-02 22:09:09 -08:00
parent d7c6f85189
commit 008bb4732b
15 changed files with 46 additions and 30 deletions

View File

@ -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])

View File

@ -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]);

View File

@ -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);
}

View File

@ -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 ();
}
}

View File

@ -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");
}

View File

@ -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)

View File

@ -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");
}
}

View File

@ -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;

View File

@ -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);
}
}

View File

@ -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 ();
}

View File

@ -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

View File

@ -1683,7 +1683,7 @@ main (int argc, char **argv)
}
break;
default:
case type_undef: default:
affirm (false);
}

View File

@ -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 ();
}

View File

@ -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:

View File

@ -923,7 +923,7 @@ main (int argc, char **argv)
case AI_ERR_MEM:
xalloc_die ();
default:
case AI_ERR_OK: default:
unreachable ();
}