GNU bug report logs - #75964
Switching the Emacs build to -Wswitch-enum in src/

Previous Next

Package: emacs;

Reported by: Pip Cet <pipcet <at> protonmail.com>

Date: Fri, 31 Jan 2025 09:41:02 UTC

Severity: wishlist

Full log


View this message in rfc822 format

From: Eli Zaretskii <eliz <at> gnu.org>
To: Paul Eggert <eggert <at> cs.ucla.edu>
Cc: pipcet <at> protonmail.com, 75964 <at> debbugs.gnu.org, stefankangas <at> gmail.com
Subject: bug#75964: Switching the Emacs build to -Wswitch-enum in src/
Date: Sun, 02 Feb 2025 21:27:52 +0200
> Date: Sun, 2 Feb 2025 10:27:02 -0800
> Cc: 75964 <at> debbugs.gnu.org, stefankangas <at> gmail.com,
>  Eli Zaretskii <eliz <at> gnu.org>
> From: Paul Eggert <eggert <at> cs.ucla.edu>
> 
> * 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.




This bug report was last modified 128 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.