JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.
If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.
If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.
| CodeSonar® 9.2p0 | CONFIDENTIAL | CodeSecure Inc |
A switch whose cases suggest the code might be cleaner if an enumeration were employed, instead of raw integers.
Specifically, a switch statement for which all of the following are true.
For the sake of this warning class, the label density of a switch statement is computed as:
where
| Class Name | switch With Non-enum Expression | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Significance | style | ||||||||||||
| Mnemonic | LANG.STRUCT.SW.SWNEE | ||||||||||||
| Categories |
|
||||||||||||
| 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="switch With Non-enum Expression" |
int badlang_struct_sw_swnee( int myint ) {
switch( myint ){ /* 'switch With Non-enum Expression' warning issued here
* - controlling expression has int type
* - has 3 non-default, non-range, non-character-constant case labels
* (== threshold from factory setting of SWITCH_LABEL_CARDINALITY_THRESHOLD)
* - label density is (label count)/len(label range) = 3/3 = 1
* (above threshold from factory setting of SWITCH_LABEL_DENSITY_THRESHOLD)
*/
case 1:
case 2:
case 3:
return 0;
default:
return 1;
}
}
int fewer_cases_int_switch( int myint ) {
switch( myint ) { /* only has 2 non-default, non-range, labels
* - below threshold from factory setting of
* SWITCH_LABEL_CARDINALITY_THRESHOLD */
case 1:
case 2:
return 0;
default:
return 1;
}
}
int sparser_cases_int_switch( int myint ) {
switch( myint ) { /* label density = 3/(300-100+1) =~ 1.5%
* - below threshold from factory setting of
* SWITCH_LABEL_DENSITY_THRESHOLD */
case 100:
case 200:
case 300:
return 0;
default:
return 1;
}
}
typedef enum {
CASE1 = 1,
CASE2,
CASE3,
CASE4
} CASE_ENUM;
int enum_control( CASE_ENUM mycaseenum ) {
switch( mycaseenum ) { /* controlling expression has enumeration type */
case CASE1:
case CASE2:
case CASE3:
return 0;
default:
return 1;
}
}
The following configuration file parameters affect checks for this warning class.
To report problems with this documentation, please visit https://support.codesecure.com/.