← 返回首页
Use of integer where enum is preferred — CodeQL query help documentation CodeQL docs
CodeQL documentation
CodeQL resources

Use of integer where enum is preferred

ID: cpp/integer-used-for-enum Kind: problem Security severity: Severity: warning Precision: medium Tags: - maintainability - readability - language-features - external/jsf Query suites: - cpp-security-and-quality.qls

Click to see the query in the CodeQL repository

This rule finds switch 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

typedef enum { CASE_VAL1, CASE_VAL2 } caseVals; void f() { int caseVal; //Wrong: switch statement uses an integer switch(caseVal) { case 1: //... case 0xFF: //... default: //... } //Correct: switch statement uses enum. It is easier to see if a case //has been left out, and that all cases are valid values caseVals caseVal2; switch (caseVal2) { case CASE_VAL1: //... case CASE_VAL2: //... default: } }

References