https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118664

            Bug ID: 118664
           Summary: Improve -Wswitch and -Wswitch-enum warnings for enum
                    types
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pipcet at protonmail dot com
  Target Milestone: ---

This is a wishlist item.  I have a patch, but it's not finished yet.

I would like GCC to warn about a non-exhaustive switch (x) { ... } expression
where x is not of enum type, but the case labels are members of an enum.

This would make it more useful for becoming aware of extensions of
externally-provided anonymous enums.

There is also a weaker second proposal: Warn only about code such as

enum {
  ENUM_CASE_A,
  ENUM_CASE_B,
};

void doswitch (int x) {
  switch ((typeof (ENUM_CASE_A))x) {
  case ENUM_CASE_A:
    return;
  }
  while (1);
}

I believe that typeof (ENUM_CASE_A) is equivalent to "int", and must be, for
code generation.  I don't believe there is a prohibition in the C standard
against remembering where the type came from for the sole purpose of printing
better warnings.

If the second proposal is also unacceptable, maybe there's a way to add this to
the analyzer instead?

For the purposes of sending the patch, should I assume it's a separate new
warning flag or that it's covered by -Wswitch-enum?

Also, would it be a good idea to detect mixed enumeration switches, where some
case labels refer to one enum and others to another?  While doing that is
sometimes useful for extending enums, it can also be a genuine bug.
  • [Bug c/118664] New: Improve -Wsw... pipcet at protonmail dot com via Gcc-bugs

Reply via email to