November 07, 2011 Implicit case fallthrough warns only when a statement is in place | ||||
---|---|---|---|---|
| ||||
import std.stdio;
void main()
{
int y;
switch (y)
{
case 0:
{
// no warning here on fallthrough
}
case 1:
{
goto case 2;
}
case 2:
{
writeln("2");
break;
}
default:
}
}
This won't trigger any warnings when compiled via -w and -wi, and I think this could be improved.
DMD will trigger this warning only if you have a statement inside of 'case 0'. There could be a situation where you forgot to put a break statement (inside case 0), and you end up in a different case handler due to the fallthrough to the next case label which has a 'goto' statement just like above.
>From what I recall of the fallthrough topic, we can either put the
labels together if we really want fallthrough:
case 0:
case 1:
{
//...
}
or use "goto case", or an explicit goto:
case 0:
{
goto case; // or goto case 1;
}
case 1:
{
//...
}
IOW, if you have a case defined with braces there should be a warning on implicit fallthrough.
I did have a bug pop up because I was relying on this new fallthrough warning system, but it failed to warn me because I didn't have a statement in one of my cases (I forgot to put a break).
|
November 08, 2011 Re: Implicit case fallthrough warns only when a statement is in place | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | Andrej Mitrovic:
> I did have a bug pop up because I was relying on this new fallthrough warning system, but it failed to warn me because I didn't have a statement in one of my cases (I forgot to put a break).
-.- This is the consequence of special cases piled on special cases, in the D design. (I agree with Jonathan here).
Bye,
bearophile
|
Copyright © 1999-2021 by the D Language Foundation