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

所有 if...else-if 分枝都应以 else 子句结束

9.1.15 ID_if_missingEndingElse
目录 › next › previous

所有 if...else-if 分枝都以 else 子句结束是“防御性编程”思想的良好体现。

单独的一个 if 分枝不要求接有 else 子句:

if (x > 0) {
    ....
}

存在多个 if...else-if 分枝时,要求接有 else 子句:

if (x > 0) {
    ....
}
else if (y < 0){
    ....
}
else {
    // Comment is the minimum requirement,
    // if here is unreachable logically,
    // it’s better to log or throw an exception
}

相关

ID_switch_missingDefault

参考

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