主要内容

MISRA C++:2008 Rule 2-10-1

R2026b

Different identifiers shall be typographically unambiguous

描述

Different identifiers shall be typographically unambiguous. 1

理由

当您使用排字看起来相似的不同标识符时,可能会在后续代码中无意间使用错误的标识符。这类排字易混淆的标识符可能导致难以诊断的 Bug。

应使用有明确差异的标识符。避免使用仅因以下各项的组合而有所不同的标识符:

  • 使用小写字母代替大写字母,反之亦然。

  • 是否存在下划线字符。

  • 字母 O 与数字 0 互换。

  • 字母 I 与数字 1 互换。

  • 字母 I 与字母 l 互换。

  • 字母 S 与数字 5 互换。

  • 字母 Z 与数字 2 互换。

  • 字母 n 与字母 h 互换。

  • 字母 B 与数字 8 互换。

  • 字母 rn 与字母 m 互换。

此规则不考虑标识符的作用域。

Polyspace 实现

如果两个标识符的差异只是前述列表中易混淆字符的组合,Polyspace® 会报告缺陷。在检查此规则时,规则检查项不会考虑变量的完全限定名称。规则检查项不考虑标识符的作用域。使用 MISRA C++:2008 Rule 2-10-1 可识别作用域特定标识符声明违规。

故障排除

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

示例

全部展开

void func(void) {
    int id1_numval;
    int id1_num_val;  //Non-compliant

    int id2_numval;
    int id2_numVal;   //Non-compliant

    int id3_lvalue;
    int id3_Ivalue;   //Non-compliant

    int id4_xyZ;
    int id4_xy2;      //Non-compliant

    int id5_zerO;
    int id5_zer0;     //Non-compliant

    int id6_rn;
    int id6_m;        //Non-compliant
}

在此示例中,当使用可能会相互混淆的标识符时,便会违反规则。

检查信息

组:词法规范
类别:必需

版本历史记录

在 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:2025

  • MISRA C++:2008

  • MISRA C++:2023

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