title | description | ms.date | f1_keywords | helpviewer_keywords | no-loc | |||||
---|---|---|---|---|---|---|---|---|---|---|
Warning C26818 |
Reference for Microsoft C++ Code Analysis warning C26818 in Visual Studio. |
04/22/2020 |
|
|
|
Switch statement does not cover all cases. Consider adding a 'default' label (es.79).
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.
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;
}
}
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;
}
}