Implicit case fallthrough warns only when a statement is in place

Jonathan M Davis jmdavisProg at gmx.com
Mon Nov 7 16:14:25 PST 2011


On Monday, November 07, 2011 15:38 Andrej Mitrovic wrote:
> 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).

I expect that it's simpler for the compiler to not worry about the braces, 
since it can just look at the AST without worrying about the tokens. I don't 
know if Walter would consider it worth changing it for this particular case or 
not. Just open up an enhancement request.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list