C6259

warning C6259: labeled code is unreachable: (<expression> & <constant>) in switch-expr cannot evaluate to <case-label>

This warning indicates unreachable code caused by the result of a bitwise-AND (&) comparison in a switch expression. The case statement that matches the constant in the switch expression is only reachable; all other case statements are not reachable.

Example

The following sample code generates this warning because the expression switch(rand() & 3) cannot evaluate to case label (case 4):

#include <stdlib.h>

void f()
{
     switch (rand () & 3) {
        case 3:
            /* Reachable */
            break;
        case 4:
            /* Not reachable */
            break;
        default:
            break;
    }
}

To correct this warning, remove the unreachable code or verify that the constant used in the case statement is correct. The following code removes the unreachable case statement:

#include <stdlib.h>

void f()
{
     switch (rand () & 3) {
        case 3:
            /* Reachable */
            break;
        default:
            break;
    }
}

See Also

Reference

switch Statement (C++)

switch Statement (C)