C and C++


LANG.STRUCT.SW.BAD : Malformed switch Statement

Summary

A switch statement has one or more of the following issues.

This class is a strict superset of each of the following classes.

Properties

Class Name Malformed switch Statement
Significance style
Mnemonic LANG.STRUCT.SW.BAD
Categories
MisraC2023 MisraC2023:16.1 All switch statements shall be well-formed
Misra2012 Misra2012:16.1 All switch statements shall be well-formed
Misra2004 Misra2004:15.0 The MISRA C switch syntax shall be used
AUTOSARC++14 AUTOSARC++14:M6-4-3 A switch statement shall be a well-formed switch statement.
MisraC++2008 MisraC++2008:6-4-3 A switch statement shall be a well-formed switch statement.
MisraC++2023 MisraC++2023:9.4.2 The structure of a switch statement shall be appropriate
TS17961 TS17961:5.17-swtchdflt 5.17. Use of an implied default in a switch statement
CERT-C CERT-C:DCL41-C Do not declare variables inside a switch statement before the first case label
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="Malformed switch Statement"
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_bad(int i){

    switch(i){                                         /* has none of the listed issues */
        default: {/*...*/ break;}
        case 1:  {/*...*/ break;}
        case 2:  {/*...*/ break;}
        }

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

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


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

    switch(i%3){                    /* 'Malformed switch Statement' warning issued here */
                                    /* 'Empty switch Statement warning also issued */
        default :  {/*...*/ break;}
    }
}

Relevant Configuration File Parameters

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