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

            Bug ID: 87951
           Summary: GCC warns about reaching end of non-void function when
                    all switch is completely handled
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vlovich at gmail dot com
  Target Milestone: ---

If a function has a single switch statement that handles all enum values &
returns a value GCC will warn about the function not returning a value whereas
clang does not.  GCC requires an explicit __builtin_unreachable() annotation
after the switch. As of C++17 it seems to now be undefined behaviour rather
than unspecified behaviour for an enum to have a value that's not enumerated.

http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1766

> cat test.c

enum Enum {
  A,
  B,
};

int CoverMyBases(enum Enum x) {
        switch (x) {
                case A:
                        return 1;
                case B:
                        return 0;
        }
}

int main(int argc, const char **argv) {
        CoverMyBases(A);
        CoverMyBases(B);
        return 0;
}

> g++-8 -Wall --std=c++17 test.c

test.c: In function 'CoverMyBases':
test.c:16:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^

> clang++ -std=c++17 -Wall test.c

NOTE: Clang never warns about this even prior to C++17 or in C either.

Reply via email to