主要内容

MISRA C:2023 Rule 5.2

Identifiers declared in the same scope and name space shall be distinct

自 R2024a 起

描述

规则定义

Identifiers declared in the same scope and name space shall be distinct 1 .

理由

如果两个长名称几乎完全相同,差异仅出现在靠后的部分,则这两个名称很有可能被混用。这降低了代码的可读性。

Polyspace 实现

如果两个名称的前 63 个字符之间存在差异,则 Polyspace® 会将它们视为不同的名称。在 C90 中,前 31 个字符之间必须存在差异。要使用 C90 规则检查,请为 C 标准版本 (-c-version) 选项使用 c90 值。您可以使用 -code-behavior-specifications 选项更改比较的字符数。请参阅 -code-behavior-specifications

故障排除

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

示例

全部展开


        
extern int engine_exhaust_gas_temperature_raw;
static int engine_exhaust_gas_temperature_scaled;      /* Non-compliant */

extern double raw_engine_exhaust_gas_temperature;
static double scaled_engine_exhaust_gas_temperature;  /* Compliant */

void func ( void )
{
  /* Not in the same scope */
  int engine_exhaust_gas_temperature_local;            /* Compliant */
}

在此示例中,标识符 engine_exhaust_gas_temperature_scaled 与上一个标识符 engine_exhaust_gas_temperature_raw 具有相同的 31 个字符。

如果两个标识符具有相同的 31 个字符但作用域不同,则该规则不适用。例如,engine_exhaust_gas_temperature_localengine_exhaust_gas_temperature_raw 具有相同的 31 个字符但作用域不同。


        
extern int engine_xxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_x_raw;
static int engine_xxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_x_scale; /* Non-compliant */

    
extern int engine_gas_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx__raw;
static int engine_gas_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx__scale; /* Compliant */

void func ( void )
{
/* Not in the same scope */
    int engine_xxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_x_local; /* Compliant */
}

在此示例中,标识符 engine_xxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_x_scale 与上一个标识符 engine_xxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_xxxxxxxxx_x_raw 具有相同的 63 个字符。

检查信息

组:标识符
类别:必需
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.