CodeQL documentation

Missing enum case in switch

ID: cpp/missing-case-in-switch
Kind: problem
Security severity: 
Severity: warning
Precision: medium
Tags:
   - reliability
   - correctness
   - external/cwe/cwe-478
Query suites:
   - cpp-security-and-quality.qls

Click to see the query in the CodeQL repository

This rule finds switch statements that switch on values of an enumeration type but do not provide cases for all the enumeration constants or a default case. This is an indication that there may be cases unhandled by the switch statement.

Recommendation

Provide a case for every enumeration constant, or introduce a default case if several constants should be treated the same way.

Example

typedef enum {
	RED,
	ORANGE,
	YELLOW,
	GREEN,
	BLUE,
	INDIGO,
	VIOLET
} colors;

int f(colors c) {
	switch (c) {
	case RED:
		//...
	case GREEN:
		//...
	case BLUE:
		//...
		//wrong: does not use all enum values, and has no default
	}

	switch(c) {
	case RED:
		//...
	case GREEN:
		//...
	default:
		//correct: does not use all enum values, but has a default
	}
}

References

  • © GitHub, Inc.
  • Terms
  • Privacy
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy