]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
maint: pacify ‘gcc -Wswitch-enum’
authorPaul Eggert <eggert@cs.ucla.edu>
Mon, 3 Feb 2025 06:09:09 +0000 (22:09 -0800)
committerPaul Eggert <eggert@cs.ucla.edu>
Mon, 3 Feb 2025 06:09:52 +0000 (22:09 -0800)
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.

15 files changed:
configure.ac
src/chmod.c
src/chown-core.c
src/copy.c
src/df.c
src/digest.c
src/du.c
src/factor.c
src/head.c
src/ls.c
src/od.c
src/split.c
src/sync.c
src/tr.c
src/wc.c

index bf6da2a5a8873157f52ed9ddcf942be30f881a93..661398702d6c91208ee32d755f2bf9fa57dbfb46 100644 (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])
index a5a54b1417aeb2a58a91cf26984e1f18eec620dd..1e1cc4cd590ae7b892e8ed3cef61e6f4332c1466 100644 (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]);
index fab04e6f26c2f01164920921a0890783e7b784bf..0e08f93e609c5de8fa271b4e982df4608a663d97 100644 (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);
     }
index 7ffb998f6c480d19a382de486ae46db0fe2e45e7..22d9830ba6a01f72f27669e3013239c33f83279e 100644 (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 ();
     }
 }
 
index 5c7efd81f89d0288911a4f53d480cd6ef24b4206..a969c5c9b961aaabdc9c1fae8d0f99cd86c5c366 100644 (file)
--- 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");
         }
index d9810c10bd5e5776ba98f9e868cbf3cd3639bd2e..1f65c73b48e3f46818ffb315c2f23b9582ff6a6d 100644 (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)
index d87800aa668b23e046dbad2f9b9ce96bc58a7770..0e5d860a926f1ad12c519875ae31ff5fa1ea3f11 100644 (file)
--- 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");
             }
         }
index 37837984f673592d45fb5b30cd403845fbced379..1f43ccafe8198a62ff4db37622db7ad109ee2d36 100644 (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;
index 062fcd00792c67fd796cf9c1791b9d6ad3936630..1620f17bc7c17162998ad71547cbe885d4636b8e 100644 (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);
     }
 }
index 1bc85f4655dc6af098d4763301d8e374a2eb7ba0..23309b812696089db962fe3fbc16fc2df3d39692 100644 (file)
--- 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 ();
     }
 
index 7676c8f36620cf1fba1d68090299cb977ca410d0..88d467c73c811da1bcefcdd8db06df1efdc921c5 100644 (file)
--- 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
index f3fec227410134b8c287065bdf3b19f9515a558a..9cbbcccd1eedb06b68b1b214a34ca1029081324a 100644 (file)
@@ -1683,7 +1683,7 @@ main (int argc, char **argv)
       }
       break;
 
-    default:
+    case type_undef: default:
       affirm (false);
     }
 
index 26ebbc8065dc6ad5d97476346655fb18a1aabbe1..7c518b5faec003689b12b81cfc139da170161d27 100644 (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 ();
         }
 
index 62198794887d62fb7bd2d1fb26634322b9fa9e4b..426065d11f12784d0a99e2335b894233cfd15671 100644 (file)
--- 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:
index 7f2e0c0475442f0d4b26f392719addaf86d7a394..ed4ad62c412b75ff64603831ff8f7f7df7c58a89 100644 (file)
--- 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 ();
     }