C and C++


LANG.STRUCT.SW.BOOL : Boolean switch Expression

Summary

The controlling expression of a switch statement is a Boolean expression.

This warning class utilizes the notion of essential type category as described in MISRA C:2012 Guidelines for the use of the C language in critical systems.

Properties

Class Name Boolean switch Expression
Significance style
Mnemonic LANG.STRUCT.SW.BOOL
Categories
MisraC2023 MisraC2023:16.7 A switch-expression shall not have essentially Boolean type
Misra2012 Misra2012:16.7 A switch-expression shall not have essentially Boolean type
Misra2004 Misra2004:15.4 A switch expression shall not represent a value that is effectively Boolean
AUTOSARC++14 AUTOSARC++14:M6-4-7 The condition of a switch statement shall not have bool type.
MisraC++2008 MisraC++2008:6-4-7 The condition of a switch statement shall not have bool type.
JSF++ JSF++:195 A switch expression will not represent a Boolean value.
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="Boolean switch Expression"
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

int lang_struct_sw_bool(int i, int j){
    int a = 0;
 
    switch (i){                              /* not a Boolean expression */
        case 1: {a +=3; break;}
        default: {a++; break;}
    }

    switch (j==i){         /* 'Boolean switch Expression' warning issued here */
        case 0:  {a += i; break;}
        default: {a++; break;}
    }

    return a;
}

Relevant Configuration File Parameters

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