For completeness, a switch over the values of an enum must either address each value in the enum or contain
a default case. switch statements that are not over enum must end with a default case.
This rule is a more nuanced version of S131. Use S131 if you want to require a default case for every
switch even if it already handles all enumerators of an enum. Otherwise, use this rule.
Noncompliant code example
typedef enum {APPLE, GRAPE, KIWI} fruit;
void example(fruit f, int i) {
switch (f) { // Noncompliant; no case for KIWI
case APPLE:
//...
case GRAPE:
//...
case 3: // Noncompliant; case value not in enum
// ...
}
switch (i) { // Noncompliant; no default
case 0:
// ...
case 1:
// ...
}
}
Compliant solution
typedef enum {APPLE, GRAPE, KIWI} fruit;
void example(fruit f) {
switch (f) {
case APPLE:
//...
case GRAPE:
//...
default:
// ...
}
switch (i) {
case 0:
// ...
case 1:
// ...
default:
// ...
}
}
or
typedef enum {APPLE, GRAPE, KIWI} fruit;
void example(fruit f) {
switch (f) {
case APPLE:
//...
case GRAPE:
//...
case KIWI:
//...
}
switch (i) {
case 0:
case 1:
// ...
default:
// ...
}
}