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

禁止 goto 语句向前跳转

9.7.2 ID_forbidGotoBack
目录 › next › previous

向先于当前 goto 语句定义的标签跳转,可读性较差,是公认的不良实现。

示例:

int foo() {
    int i = 0, j = 0;
L:
    j += 1;
    i += j;
    if (j > 100) {
        goto M;      // Compliant
    }
    goto L;          // Non-compliant
M:
    return i;
}

例中 goto M 向后跳转符合本规则要求,而 goto L 向前跳转不符合要求,应改用循环等结构性语句。

相关

ID_forbidGotoBlocks ID_forbidGoto

参考

MISRA C 2012 15.2 MISRA C++ 2008 6-6-2
Copyright©2024 360 Security Technology Inc., Licensed under the Apache-2.0 license.