Movatterモバイル変換


[0]ホーム

URL:


CodeQL documentation
CodeQL resources

Use of integer where enum is preferred

ID: cpp/integer-used-for-enumKind: problemSecurity severity: Severity: warningPrecision: mediumTags:   - maintainability   - readability   - language-features   - external/jsfQuery suites:   - cpp-security-and-quality.qls

Click to see the query in the CodeQL repository

This rule findsswitch statements that use an integer instead of an enumeration. Enumerations are preferred when dealing with a limited number of choices as they makes it easier to see if a case has been left out.

Recommendation

Use an enumeration instead of an integer to represent a limited set of choices.

Example

typedefenum{CASE_VAL1,CASE_VAL2}caseVals;voidf(){intcaseVal;//Wrong: switch statement uses an integerswitch(caseVal){case1://...case0xFF://...default://...}//Correct: switch statement uses enum. It is easier to see if a case//has been left out, and that all cases are valid valuescaseValscaseVal2;switch(caseVal2){caseCASE_VAL1://...caseCASE_VAL2://...default:}}

References

  • AV Rule 148,Joint Strike Fighter Air Vehicle C++ Coding Standards. Lockheed Martin Corporation, 2005.

  • C++ Switch statement


[8]ページ先頭

©2009-2025 Movatter.jp