www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Implicit case fallthrough warns only when a statement is in place

reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
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).
Nov 07 2011
parent bearophile <bearophileHUGS lycos.com> writes:
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
Nov 07 2011