Edit

Share via


Warning C6259

Labeled code is unreachable: ('expression' & 'constant') in switch-expr cannot evaluate to 'case-label'

Remarks

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

Code analysis name: DEADCODEINBITORLIMITEDSWITCH

Example

The following sample code generates this warning because the 'switch' expression (rand() & 3) can't 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