主要内容

MISRA C:2023 Rule 3.2

Line-splicing shall not be used in // comments

自 R2024a 起

描述

规则定义

Line-splicing shall not be used in // comments 1 .

理由

\ 字符后紧跟一个换行符时,就会发生行拼接。行拼接用于跨多行的语句。

如果您在 // 注释中使用行拼接,则下一行可能会成为该注释的一部分。在大多数情况下,\ 是多余的,并可能导致无意中将代码注释掉。

Polyspace 实现

Polyspace 会检查字符 \ 是否为以 // 开头的注释的最后一个字符。

故障排除

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

示例

全部展开

#include <stdbool.h>

extern _Bool b;

void func ( void )
{
    unsigned short x = 0;   // Non-compliant - Line-splicing \
    if ( b )
    {
        ++b;      
    }
}

由于行拼接,语句 if ( b ) 成为前一个 // 注释的一部分。因此,语句 b++ 始终会执行,使得 if 代码块变得多余。

检查信息

组:注释
类别:必需
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.