Warning C26818
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;
}
}
Feedback
https://aka.ms/ContentUserFeedback.
În curând: Pe parcursul anului 2024, vom elimina treptat Probleme legate de GitHub ca mecanism de feedback pentru conținut și îl vom înlocui cu un nou sistem de feedback. Pentru mai multe informații, consultați:Trimiteți și vizualizați feedback pentru