CodeQL documentation

Use of integer where enum is preferred

ID: cpp/integer-used-for-enum
Kind: problem
Security severity: 
Severity: warning
Precision: medium
   - 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.


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


typedef enum {
} caseVals;

void f() {
	int caseVal;
	//Wrong: switch statement uses an integer
	switch(caseVal) {
	case 1:
	case 0xFF:

	//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:


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

  • C++ Switch statement

  • © GitHub, Inc.
  • Terms
  • Privacy