Insufficient control flow analysis of enum switch in GCC

Viewed 1700

In the following C++ code:

typedef enum { a, b, c } Test;

int foo(Test test) {
    switch (test) {
        case a: return 0;
        case b: return 1;
        case c: return 0;
    }
}

a warning is issued when compiling with -Wall, saying that control reaches end of non-void function. Why?


Edit

Its not generally correct to say that the variable test in the example can contain any value.

foo(12354) does not compile:

> test.cpp:15:14: error: invalid conversion from ‘int’ to ‘Test’
> test.cpp:15:14: error:   initializing argument 1 of ‘int foo(Test)’

because 12354 isn't a valid Test value (though it indeed would be valid in plain C, but it's not in C++).

You sure could explicitly cast an arbitrary integer constant to the enum type, but isn't that considered Undefined Behaviour?

4 Answers
Related