eckes
eckes

Reputation: 67077

Why does switch(0) not result in a warning while if(0) does?

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

Answers (4)

chqrlie
chqrlie

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

John Bode
John Bode

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 The if statement

Constraints

1     The controlling expression of an if statement shall have scalar type.
...
6.8.4.2 The switch statement

Constraints

1  The controlling expression of a switch statement shall have integer type.

2     If a switch statement has an associated case or default label within the scope of an identifier with a variably modified type, the entire switch statement shall be within the scope of that identifier.154)

3     The expression of each case label shall be an integer constant expression and no two of the case constant expressions in the same switch statement shall have the same value after conversion. There may be at most one default label in a switch statement. (Any enclosed switch statement may have a default label or case constant expressions with values that duplicate case constant expressions in the enclosing switch statement.)
154) That is, the declaration either precedes the switch statement, or it follows the last case or default label associated with the switch 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

gaetanoM
gaetanoM

Reputation: 42054

To get the warning in Visual Studio 2013 you need:

  1. right click on the project --> properties
  2. C/C++ in the section General set Warning level to ALL

The warning is only for the if statement because the switch is a block.

enter image description here

Upvotes: 1

ryyker
ryyker

Reputation: 23228

I just tried it on CLANG with extended warning turned on and received the following warning:

enter image description here

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

Related Questions