警告 C26819

switch 标签之间未批注的 fallthrough (es.78)。

备注

此检查涵盖 switch 语句中的隐式 fallthrough。 隐式 fallthrough 是指控制流在不使用 [[fallthrough]]; 语句的情况下,从一个 switch case 直接传输到下一个 switch case。 当在包含至少一条语句的 switch case 中检测到隐式 fallthrough 时,将引发此警告。

有关详细信息,请参阅 C++ Core Guidelines 中的 ES.78:不要依赖 switch 语句中的隐式 fallthrough

示例

在此示例中,隐式 fallthrough 发生在从非空 switchcase 到下一个 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;
    }
}

要修复此问题,请在发生 fallthrough 的位置插入 [[fallthrough]]; 语句。

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

修复此问题的另一种方法是删除隐式 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;
    }
}

另请参阅

ES.78:不依赖于 switch 语句中的隐式 fallthrough