digitalmars.D - Re: Wish: Variable Not Used Warning
- Robert Fraser <fraserofthenight gmail.com> Jul 11 2008
superdan Wrote:Robert Fraser Wrote:Don Wrote:superdan wrote:4. warning - switch statement has no default another example of a motherfuck. just require total coverage. in closed-set cases i routinely write anyway: switch (crap) { case a: ...; break; case b: ...; break; default: assert(crap == c): ...; break; } again: vast majority of code already has a default. the minority just has to add a little code. make it an error.
Yup. Make it an error.
I agree with everything else, but this one I think shouldn't be an error or warning (the implicit assert(0) is enough). This is because the vast majority of switch statements I use (and many I see) are over enums, and if every branch in the enumeration is covered, a pointless "default" will just complicate code.
you are not disagreeing. switching over an enum is already closed if you mention all cases. the compiler knows that. it should indeed just throw an error if you have an out-of-range value that you forged from an int. but that's an uncommon case. don't make all pay for a rare bug.
Ah, in that case we are in agreement. But as of a few versions ago, DMD still gives a warning even if you use all the possible values of an enum in a switch statement.
Jul 11 2008