Subject: bug#75964: Switching the Emacs build to -Wswitch-enum in src/

From: Eli Zaretskii <[email-masked]>
Date: Fri, 31 Jan 2025 13:50:12 +0200

> Date: Fri, 31 Jan 2025 09:39:45 +0000
> From:  Pip Cet via "Bug reports for GNU Emacs,
>  the Swiss army knife of text editors" <[email-masked]>
> 
> I'm proposing to enable -Wswitch-enum as a warning option when compiling
> Emacs C sources in src/, and to modify those source files to make good
> use of it.
> 
> GCC's -Wswitch-enum will warn about a switch statement of the form
> 
> enum ABC { A, B, C };
> 
> enum ABC x = ...;
> 
> switch (x)
> {
>   case A:
>   case B:
>     return 1;
>   default:
>     return 0;
> }
> 
> The reason is that the "default" branch covers both case C and the case
> that the value of x isn't A, B, or, C.  C allows this latter case and
> requires compilers to support it, and some Emacs code relies on
> non-enumerated values to be valid.
> 
> Instead, with -Wswitch-enum, one should write:
> 
> enum ABC { A, B, C };
> 
> enum ABC x = ...;
> 
> switch (x)
> {
>   case A:
>   case B:
>     return 1;
>   case C:
>     return 0;
>   default: eassume (false);
> }
> 
> assuming x is known never to have a non-enumerated value (this is almost
> always the case in Emacs).

What should one do if the enumeration is large and the code wants to
treat all but the few values the same?  This is a legitimate use case,
isn't it?  Having to spell out all of the values is quite tedious, and
eassume will not do what we want in this case.