CodeQL documentation

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

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

  • C++ Switch statement

  • © GitHub, Inc.
  • Terms
  • Privacy