主要内容

本页采用了机器翻译。点击此处可查看最新英文版本。

MISRA C++:2008 Rule 6-6-4

For any iteration statement there shall be no more than one break or goto statement used for loop termination

描述

规则定义

For any iteration statement there shall be no more than one break or goto statement used for loop termination. 1

理由

在迭代语句中使用多个 breakgoto 语句会增加代码的复杂度。为了实现良好的结构化编程,请使用单个的 breakgoto 语句。

Polyspace 实现

当单个循环中同时存在多个 break 语句、goto 语句或这些语句的组合时,Polyspace® 会触发此缺陷。

故障排除

如果您预期会出现违规,而 Polyspace 未报告该违规,请参阅诊断为何编码规范违规未按预期显示

示例

全部展开

#include<iostream>
void foo(int x, int y)
{
	for (x = 0; x <= y; x++)
	{
		if (x == y)
		{
			// do this
			break;		//Compliant
		}
		else if (x > y)
		{
			goto error;	  //Noncompliant
		}
		else
		{
			//do this
		}
	}

error:
	std::cout << "Error message" << std::endl;
}

由于 if 语句包含 breakgoto 语句,因此 Polyspace 将该语句标记为不合规。在此情况下,Polyspace 将第一个 breakgoto 语句标记为合规。Polyspace 将所有后续的 breakgoto 语句标记为不合规。

检查信息

组:声明
类别:必需

版本历史记录

在 R2013b 中推出


1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.

The MISRA coding standards referenced in the Polyspace Bug Finder™ documentation are from the following MISRA standards:

  • MISRA C:2004

  • MISRA C:2012

  • MISRA C:2023

  • MISRA C++:2008

  • MISRA C++:2023

MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.