Skip to content

Latest commit

 

History

History
88 lines (73 loc) · 1.76 KB

c26818.md

File metadata and controls

88 lines (73 loc) · 1.76 KB
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
C26818
C26818
default
int
char

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;
    }
}