Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Switch statement does not cover all cases. Consider adding a 'default' label (es.79).
Remarks
This check covers the missing default
label in switch statements that switch over a non-enumeration type, such as int
, char
, and so on.
For more information, see ES.79: Use default to handle common cases (only) in the C++ Core Guidelines.
Example
This example has a missing default
label when switching over an int
.
void fn1();
void fn2();
void foo(int a)
{
switch (a)
{
case 0:
fn1();
break;
case 2:
fn2();
break;
}
}
Solution
To fix this issue, insert a default
label to cover all remaining cases.
void fn1();
void fn2();
void default_action();
void foo(int a)
{
switch (a)
{
case 0:
fn1();
break;
case 2:
fn2();
break;
default:
default_action();
break;
}
}
If no default action needs to be taken, insert an empty default
label to indicate that the other cases haven't been forgotten.
void fn1();
void fn2();
void foo(int a)
{
switch (a)
{
case 0:
fn1();
break;
case 2:
fn2();
break;
default:
// do nothing for the rest of the cases
break;
}
}