I am using GCC version 8.3 and have a switch statement:
#include <iostream>
enum class Type : char
{
Value1 = 'A',
Value2,
Value3,
Value4,
};
int main()
{
Type t;
switch(t)
{
case Type::Value1:
case Type::Value2:
case Type::Value3:
std::cout << "hw" << std::endl;
case Type::Value4:
break;
}
}
I want to be forced to use [[fallthrough]]
otherwise generate a warning.
I added gcc flags to detect missing enums and implict fallthrough:
g -o main.cc -O3 -Wswitch-enum -Wimplicit-fallthrough -std=c 1z
The new flags have been detected because I now get warnings I'm missing enums. I fixed those but I still receive no warnings about implicit fallthrough.
I've put the code here:
https://godbolt.org/z/rj5sTPWz4
Am I missing something? I was expecting to be warned?
UPDATE:
I changed the code to this:
struct Object
{
int a;
};
int main()
{
Type t;
Object o;
switch(t)
{
case Type::Value1:
case Type::Value2:
case Type::Value3:
case Type::Value4:
o.a = 5;
break;
}
}
and it still doesn't generate a warning.
CodePudding user response:
I don't think it's considered fallthrough if you've got no statements in between the labels—that's just multiple case labels on a statement.
Gcc doesn't even warn on
case Type::Value1:
x ; //real fallthrough here
case Type::Value2:
case Type::Value3:
case Type::Value4:
break;
though (unlike clang), but it does warn on
case Type::Value1:
x ; //real fallthrough here
case Type::Value2:
x ;
case Type::Value3:
case Type::Value4:
break;
https://godbolt.org/z/s8Ka84Gq6
so it seems you need both actual fallthrough (on both gcc and clang) and into something that has an effect in order to elicit the warning (on gcc).
CodePudding user response:
case Type::Value4:
break;
Because there is just break there, there is no difference, if you fall through or not. Add something there.
case Type::Value3:
std::cout << "something";
case Type::Value4:
std::cout << "actually something else";
break;