#include <stdio.h> int main() { int x = 0; scanf("%d", &x); bool b = x; switch (b) { case true: x = 23; break; case false: x = 45; break; default: x = 69; } return x; } The following code should give a warning to the effect of unreachable code in the default: block, which it doesn't. -Dhruv.
Use -Wunreachable-code.
(In reply to comment #1) > Use -Wunreachable-code. 3.4 still doesn't show the warning. I had tested with -Wall. Does mainline show the warning now?
Andrew, would you mind checking a little more carefully? With mainline, I get this warning message: g/x> /home/bangerth/bin/gcc-4*-pre/bin/c++ -c x.cc -Wunreachable-code x.cc: In function `int main()': x.cc:24: warning: will never be executed This is indeed a progression, though line 24 is the closing brace of main and it is therefore not at all clear what will never be executed. So there are two bugs here: first that the warning message is on the wrong line, second that the message doesn't state what exactly is never executed (the message should probably read like "statement will never be executed" or similar). W.
.
This is because switches are not optimizated as much as ifs or anything else is. The warning which Wolfgang gives is a different bug which is already filed. Note the tree which are generated is "(int) (x != 0)" so we are switching on that but the middle-end does not produce good RTL for that to optimize right.
Created attachment 15077 [details] Emit warning for surplus case labels in a switch stmt with a boolean condition The attached patchlet would warn about a superfluous default_label in a switch_statement that uses a boolean as condition. It does not burden VRP to emit this warning but checks in c-common.c instead. gcc/ChangeLog 2008-02-02 Bernhard Fischer <aldot> * c-common.c (record_surplus_node): New function. (c_do_switch_warnings): Warn for surplus default label in switch stmt.
Reconfirm for 4.3.0 / 4.4.x
@Bernhard You need to properly submit the patch to gcc-patches (plus copyright assignment in place, testcases, changelog)? See http://gcc.gnu.org/contribute.html As for the patch itself. Notice that c_do_switch_warnings returns before your code if (!warn_switch && !warn_switch_enum && !warn_switch_default) return; If you make your warning conditional on Wunreachable-code, you need to add warn_unreachable_code to that test. Alternatively, make it conditional on -Wswitch and it will work. Then, you should use warning (OPT_Wswitch, ""). Also, you should pass to warning a explicit location (ideally, the one corresponding ot the default label or if that is not possible, then the one of the switch): warning (OPT_Wswitch, "%Hwarning", &location); Bonus points if the code for this and enums could be merged somehow!
This is fixed by -Wswitch-bool in GCC 5.0