Reputation: 67077
Given this code:
#include <stdio.h>
int main( int argc, char** argv )
{
switch (0) {
case 1: printf("1");
case 2: printf("2");
default:
printf("default");
break;
}
return 0;
}
I'd expect that the compiler would tell me something about either conditional expression is constant (switch (0)
while VS2012 issues this warning for an if(0)
) or unreachable code (case 1
, case 2
).
However, neither on ideone nor on Visual Studio 2012 nor on recent GCCs I receive anything like that.
Why don't even decent compilers complain here?
Upvotes: 4
Views: 838
Reputation: 144780
I just tried clang -Weverything
: it complains about argc
and argv
not being used, but stays silent about switch(0)
, just like gcc
.
While it is not mandatory for the compiler to issue a diagnostic, a warning might help if the error is a typo. Try this one for example:
int test(int l) {
switch (1) {
case 0: return 0;
case 1: return 1;
default: return -1;
}
You should file a bug on both projects.
EDIT: clang
seems to have fixed this recently, unlike what ryyker shows in his response, -Wunreachable-code
does not complain on my laptop. I might have an older version.
Upvotes: 1
Reputation: 123488
From the C 2011 standard:
5.1.1.3 Diagnostics
1 A conforming implementation shall produce at least one diagnostic message (identified in an implementation-defined manner) if a preprocessing translation unit or translation unit contains a violation of any syntax rule or constraint, even if the behavior is also explicitly specified as undefined or implementation-defined. Diagnostic messages need not be produced in other circumstances.9)
9) The intent is that an implementation should identify the nature of, and where possible localize, each violation. Of course, an implementation is free to produce any number of diagnostics as long as a valid program is still correctly translated. It may also successfully translate an invalid program.
Emphasis added.
So, a diagnostic is required if using a constant expression in a switch
or if
control expression is a constraint violation...
6.8.4.1 Theif
statement
Constraints
1 The controlling expression of anif
statement shall have scalar type.
...
6.8.4.2 Theswitch
statement
Constraints
1 The controlling expression of aswitch
statement shall have integer type.
2 If aswitch
statement has an associatedcase
ordefault
label within the scope of an identifier with a variably modified type, the entireswitch
statement shall be within the scope of that identifier.154)
3 The expression of eachcase
label shall be an integer constant expression and no two of the case constant expressions in the sameswitch
statement shall have the same value after conversion. There may be at most onedefault
label in a switch statement. (Any enclosedswitch
statement may have adefault
label orcase
constant expressions with values that duplicatecase
constant expressions in the enclosingswitch
statement.)
154) That is, the declaration either precedes theswitch
statement, or it follows the lastcase
ordefault
label associated with theswitch
that is in the block containing the declaration.
...which it isn't.
This is basically a quality of implementation issue; the implementation may issue a diagnostic for using a constant expression in an if
or switch
statement, but it doesn't have to. if( 0 )
and switch( 0 )
may strike most of us as being hinky, but the implementation is not required to issue any diagnostics over them.
Upvotes: 3
Reputation: 42054
To get the warning in Visual Studio 2013 you need:
The warning is only for the if statement because the switch is a block.
Upvotes: 1
Reputation: 23228
I just tried it on CLANG with extended warning turned on and received the following warning:
Referring to (oddly enough) only the the first of the two lines in the switch that will never be executed:
case 1: printf("1");
Upvotes: 4