☰
  • 首页
  • 规则分类
  • 项目介绍
search
•••

switch 语句应配有 default 分枝

9.5.12 ID_switch_missingDefault
目录 › next › previous

所有 switch 语句都配有 default 分枝是“防御性编程”思想的良好体现。

示例:

switch (v)
{
case 0:
    ....
    break;
case 1:
    ....
    break;
default:
    // Comment is the minimum requirement,
    // if here is unreachable logically,
    // it’s better to log or throw an exception
    break;
}

例外:

当 switch 条件为枚举类型,且 case 标签已对应所有枚举项时,不再要求有 default 分枝。

相关

ID_if_missingEndingElse

参考

CWE-478 MISRA C 2012 16.4 MISRA C++ 2008 6-4-6 SEI CERT MSC01-C
Copyright©2024 360 Security Technology Inc., Licensed under the Apache-2.0 license.