Muokkaa

Jaa


Warning C26819

Unannotated fallthrough between switch labels (es.78).

Remarks

This check covers implicit fallthrough in switch statements. Implicit fallthrough is when control flow transfers from one switch case directly into a following switch case without the use of the [[fallthrough]]; statement. This warning is raised when an implicit fallthrough is detected in a switch case containing at least one statement.

For more information, see ES.78: Don't rely on implicit fallthrough in switch statements in the C++ Core Guidelines.

Example

In this sample, implicit fallthrough occurs from a nonempty switch case into a following case.

void fn1();
void fn2();

void foo(int a)
{
    switch (a)
    {
        case 0:    // implicit fallthrough from case 0 to case 1 is OK because case 0 is empty
        case 1:
            fn1(); // implicit fallthrough from case 1 into case 2
        case 2:    // Warning C26819.
            fn2();
            break;
        default:
            break;
    }
}

To fix this issue, insert a [[fallthrough]]; statement where the fallthrough occurs.

void fn1();
void fn2();

void foo(int a)
{
    switch (a)
    {
        case 0:
        case 1:
            fn1();
            [[fallthrough]]; // fallthrough is explicit
        case 2:
            fn2();
            break;
        default:
            break;
    }
}

Another way to fix the issue is to remove the implicit fallthrough.

void fn1();
void fn2();

void foo(int a)
{
    switch (a)
    {
        case 0:
        case 1:
            fn1();
            break; // case 1 no longer falls through into case 2
        case 2:
            fn2();
            break;
        default:
            break;
    }
}

See also

ES.78: Don't rely on implicit fallthrough in switch statements