The following code compiles fine without explicit fall-through, but with -version=bad it errors.
int hexStringConstant(dchar* p)
{
while (1)
{
dchar c = *p++;
switch (c)
{
case ' ':
case '\t':
case '\v':
case '\f':
continue;
case '\r':
if (*p == '\n')
continue;
version(bad) /// <-- Look here
goto case;
case '\n':
continue;
case 0:
case 0x1A:
return 111;
case '"':
return 222;
default:
break;
}
}
}
> dmd -c bug.d
[ no problem ]
> dmd -c bug.d -w
bug.d(18): Warning: switch case fallthrough - use 'goto case;' if intended
> dmd -c bug.d -version=bad
bug.d(1): Error: function bug.hexStringConstant no return exp; or assert(0); at end of function
Comment #1 by jbc.engelen — 2016-02-27T13:59:11Z
DMD version = 2.070.0
Comment #2 by coolfool4 — 2018-10-28T08:09:55Z
I can't get errors nor warnings for fallthroughs except with final switches on enums (-w or -wi don't help).
Compiler versions:
- DMD64 D Compiler v2.082.0
- nightly at https://run.dlang.io/
import std.stdio;
void main() {
int x = 0;
switch (x) {
case 0:
case 1:
writeln("woot");
return;
default:
}
}
Comment #3 by coolfool4 — 2018-10-28T08:25:35Z
Actually I don't get fallthrough warnings for *any* switches.
Comment #4 by coolfool4 — 2018-10-28T08:28:38Z
So it seems to accept *empty* cases to fall through. I guess this is intended, then?
Comment #5 by razvan.nitu1305 — 2022-11-03T10:09:28Z
When compiling the initial example I get:
test.d(18): Error: switch case fallthrough - use 'goto case;' if intended
This seems to have been fixed
Comment #6 by dkorpel — 2022-11-03T10:22:18Z
(In reply to RazvanN from comment #5)
> When compiling the initial example I get:
>
> test.d(18): Error: switch case fallthrough - use 'goto case;' if intended
>
>
> This seems to have been fixed
The problem is that with -version=bad, it incorrectly raises an error about missing a return statement or assert(0). That's still the case.
Here's a reduced example:
```
// accepts valid
int a()
{
while (1)
{
switch (0)
{
case 0: // < implicit fallthrough
default: continue;
}
}
}
// rejects valid
int b()
{
while (1)
{
switch (0)
{
case 0: goto default; // < explicit fallthrough
default: continue;
}
}
}
```
Comment #7 by robert.schadek — 2024-12-13T18:46:59Z