https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60439

--- Comment #10 from Gary Funck <gary at intrepid dot com> ---
The following test case when compiled against a recent trunk revision (211365
2014-06-08) generates a warning, as intended by the patch described in comment
8.


int a, x;

int
main ()
{

  switch (!x)
    {
    case 0:
      a = 1;
      break;
    case 1:
      a = 2;
      break;
    }

  return 0;
}

s.c: In function ‘main’:
s.c:8:11: warning: switch condition has boolean value [-Wswitch-bool]
   switch (!x)
           ^

However, -Wno-switch-bool does *not* suppress the warning.

Looking at gcc/c-family/c.opt, it has an Init(1) clause but no Var() clause. 
It seems that if an Init() clause is present that a Var() clause must also be
present for this option to work as expected.

This patch fixes the issue.

Index: gcc/c-family/c.opt
===================================================================
--- gcc/c-family/c.opt  (revision 211365)
+++ gcc/c-family/c.opt  (working copy)
@@ -539,7 +539,7 @@ C ObjC C++ ObjC++ Var(warn_switch_enum)
 Warn about all enumerated switches missing a specific case

 Wswitch-bool
-C ObjC C++ ObjC++ Warning Init(1)
+C ObjC C++ ObjC++ Var(warn_switch_bool) Warning Init(1)
 Warn about switches with boolean controlling expression

 Wmissing-format-attribute

However, I'm not so certain that this option should be enabled by default, for
a few reasons:

1) The test case above shows the use of a boolean value used in the case
expression where both alternatives (0 and 1) are accounted for and no other
(overflow) cases are mentioned.  Adding a cast to (int) will not clarify the
code at all and in fact then leaves apparent cases unaccounted for, which might
arguably lead to a warning to that effect on some compilers (present or
future).

2) If the compiler performed control flow analysis and range analysis to
determine that some cases are not accounted for or that some cases are
out-of-range, then enabling by default would seem appropriate.  In the test
case above (in my opinion) no warning should be issued because both cases are
accounted for.

3) Perhaps this option should only be enabled explicitly or by -Wall.  For
example, -Wswitch is enabled by -Wall.

Wswitch
C ObjC C++ ObjC++ Var(warn_switch) Warning LangEnabledBy(C ObjC C++ ObjC++,Wall
)
Warn about enumerated switches, with no default, missing a case

Reply via email to