主要内容

MISRA C:2023 Rule 3.1

The character sequences /* and // shall not be used within a comment

自 R2024a 起

描述

规则定义

The character sequences /* and // shall not be used within a comment 1 .

理由

这些字符序列不允许出现在代码注释中,原因如下:

  • 如果您的代码在 /* */ 注释中包含 /*//,这通常意味着您无意中将代码注释掉了。

  • 如果您的代码在 // 注释中包含 /*,这通常意味着您无意中取消注释了 /* */ 注释。

Polyspace 实现

您不能在源代码中为此规则添加注解。

有关注解的信息,请参阅为代码添加注解并隐藏已知或可接受的结果

故障排除

如果您预期会出现违规,但未看到该违规,请参阅诊断为何编码规范违规未按预期显示

示例

全部展开

int x;
int y;
int z;

void non_compliant_comments ( void )
{
    x = y //  	/* Non-compliant
        + z
        //  */
        ;
    z++;	//	Compliant with exception: // permitted within a // comment
}

void compliant_comments ( void )
{
    x = y /*  	Compliant
      + z
	  */
        ;
    z++;	//	Compliant with exception: // is permitted within a // comment
}

在此示例中,在 non_compliant_comments 函数中,/* 字符出现在看似 // 注释的位置,违反了该规则。由于注释结构的原因,执行的运算为 x = y + z;。不过,如果没有这两个 //,则会发生完全不同的运算 x=y;。无法明确知道打算执行的是哪个运算。

请使用一种能够清楚表达您的意图的注释格式。例如,在 compliant_comments 函数中,可以清楚地看出意图执行的运算是 x=y;

检查信息

组:注释
类别:必需
AGC 类别:必需

版本历史记录

在 R2024a 中推出


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.