Implicit case fallthrough warns only when a statement is in place

Andrej Mitrovic andrej.mitrovich at gmail.com
Mon Nov 7 15:38:12 PST 2011


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).


More information about the Digitalmars-d-learn mailing list