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
| クラス名 | switch With Non-enum Expression | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 日本語クラス名 | switch With Non-enum Expression | ||||||||||||
| クラス分類 | スタイル (style) | ||||||||||||
| ニーモニック | LANG.STRUCT.SW.SWNEE | ||||||||||||
| カテゴリー |
|
||||||||||||
| 対応言語 | C および C++ で利用可能です。 |
||||||||||||
| 有効/無効設定 | このワーニングクラスのチェックはデフォルトで無効になっており、プロジェクトには非正規の C向けAST
が必要になります。有効にするにはプロジェクト設定ファイル (configuration file) に以下の WARNING_FILTER ルールと
RETAIN_UNNORMALIZED_C_AST 設定を追加してください。
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;
}
}
設定ファイルの以下のパラメータがこのワーニングクラスのチェックに影響します。
To report problems with this documentation, please visit https://support.codesecure.com/.