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

From: Eli Zaretskii <[email-masked]>
Date: Sun, 02 Feb 2025 21:27:52 +0200

> Date: Sun, 2 Feb 2025 10:27:02 -0800
> Cc: [email-masked], [email-masked],
>  Eli Zaretskii <[email-masked]>
> From: Paul Eggert <[email-masked]>
> 
> * Cleanly compile with -Wswitch-enum to catch potential errors.
> * A style that is easy to explain and understand.
> * No changes to GCC needed for now.
> * No new C macros in the Emacs source code.
> * Source code that is shorter and easier to maintain.

What are the problems in etags.c this tries to solve? just that its
source is 23 lines too long?

> Here's the proposal:
> 
> Suggestion 1: Don't treat all enum switch statements the same. Some enum 
> switch statements merely want to treat a couple of enum values 
> specially, with all other enum values being default. For these, it's 
> unlikely that forcing the programmer to list a case for every enum value 
> will catch many bugs; it's even possible that this would cause more 
> problems than it'll cure, and it's certainly an annoyance.
> 
> For these switch statements, use "switch (+E)" instead of "switch (E)". 
> This pacifies GCC and clearly signals to the reader that the switch's 
> cases are not intended to exhaust the enum. A "switch (E)" must list all 
> the enum values; a "switch (+E)" need not do so. A reasonable guideline 
> is that if a switch statement has more than three "case X: break;"s then 
> it may be a good idea to use "switch (+E)" instead of "switch (E)".
> 
> Suggestion 2: Omit "default: break;"s present only to tell GCC and/or 
> the reader that the switch is otherwise not exhaustive. "switch (+expr)" 
> already does this more concisely and more usefully.

"switch (+expr)" has the disadvantage that it is not widely known.
E.g., I couldn't find its description in the two GNU manuals of the C
language I have.  Why would we want to use a construct that people
might not be familiar with?

> Combine these two suggestions, and I made etags.c shorter (by 23 lines) 
> and easier to read once you know the style. See attached patch, which 
> compiles cleanly with -Wswitch-enum.

I'm not sure shortening etags.c by 23 lines justifies this.