C and C++


LANG.STRUCT.SW.MPD : Misplaced default

Summary

A switch statement contains a default label, but it is neither the very first nor the very last switch label in the statement.

This class is a strict subset of Malformed switch Statement.

If a switch statement exhibits multiple issues from the following list, the analysis may report only a subset of them.

Properties

Class Name Misplaced default
Significance style
Mnemonic LANG.STRUCT.SW.MPD
Categories
MisraC2023 MisraC2023:16.5 A default label shall appear as either the first or the last switch label of a switch statement
Misra2012 Misra2012:16.5 A default label shall appear as either the first or the last switch label of a switch statement
Misra2004 Misra2004:15.3 The final clause of a switch statement shall be the default clause
AUTOSARC++14 AUTOSARC++14:M6-4-6 The final clause of a switch statement shall be the default-clause.
MisraC++2008 MisraC++2008:6-4-6 The final clause of a switch statement shall be the default-clause.
MisraC++2023 MisraC++2023:9.4.2 The structure of a switch statement shall be appropriate
JSF++ JSF++:194 All switch statements that do not intend to test for every enumeration value shall contain a final default clause.
Availability Available for C and C++.
Enabling Checks for this warning class are disabled by default, and require the unnormalized C ASTs for the project. To enable them, add the following WARNING_FILTER rule and RETAIN_UNNORMALIZED_C_AST specification to the project configuration file.
RETAIN_UNNORMALIZED_C_AST = Yes
WARNING_FILTER += allow class="Misplaced default"
Note that retaining the unnormalized ASTs will increase the disk space used to store the project representation, and may make the analysis take longer.

Example

void lang_struct_sw_mpd(int i){

    switch (i){
        case 1:  {/* ... */ break;}
        case 2:  {/* ... */ break;}
        case 3: 
        default: {/* ... */ break;}                                /* default is last label */
    }

    switch (i){
        default:                                                   /* default is first label */
        case 1:  {/* ... */ break;}
        case 2:  {/* ... */ break;}
        case 3:  {/* ... */ break;}
    }

    switch (i){
        case 1: 
        default: {/* ... */ break;}    /* 'Misplaced default' warning issued here */
        case 2:  {/* ... */ break;}
        case 3:  {/* ... */ break;}
    }

    switch (i){
        case 1:  {/* ... */ break;}
        default:                       /* 'Misplaced default' warning issued here */
        case 2:  {/* ... */ break;}
        case 3:  {/* ... */ break;}
    }

    switch (i){
        case 1:  {/* ... */ break;}
        case 2:  {/* ... */ break;}
        default:                       /* 'Misplaced default' warning issued here */
        case 3:  {/* ... */ break;}
    }

}

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.