[previous] 2881 [next] The code in this 'default' clause is unreachable.
Control flow

The 'default' clause in this 'switch' statement appears to be unreachable. Is this intended ?

Coding standards often require that a 'default' clause should be included in every 'switch' statement as a defensive measure, even when the clause may appear to be 'unreachable code'.

Message 2880 is a message which generally identifies unreachable code. However, an unreachable 'default' statement in a switch construct is identified using message 2881 instead,

Message 2881 is often generated in a 'switch' statement where the controlling expression is an object of enum type and 'case' labels exist for all constants in the enumeration-list. In this situation, a 'default' label is theoretically only reachable if the value of the enum object has been "corrupted" with an inappropriate value; something which can be done quite easily in the C language by an injudicious assignment.

Example:


/*PRQA S 1253,1278,2016,2017,2018,2211,2213,3120,3227,3355,3408,3602,3605 ++*/

void foo(unsigned int un)
{
    if (un < 3)
    {
        switch (un)
        {
        case 0U:
            break;
        case 1U:
            break;
        case 2U:
            break;
        default:                        /* 2881 */
            break;
        }
    }
}

QA·C Source Code Analyser 8.1.2
© 2013 Programming Research.
www.programmingresearch.com
Personality Groups | Glossary | Message Index Contents