主要内容

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

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

Different identifiers shall be typographically unambiguous

描述

规则定义

Different identifiers shall be typographically unambiguous. 1

理由

当您使用外观上相似的不同标识符时,可能会在代码的后续部分无意中使用了错误的标识符。此类排版上存在歧义的标识符可能导致难以诊断的错误。

使用明确且唯一标识的标识符。避免使用由以下组合构成的标识符:

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

  • 是否存在下划线字符。

  • 字母 O 与数字 0 互换。

  • 字母 I 与数字 1 互换。

  • 字母 I 与字母 l 互换。

  • 字母 S 与数字 5 互换。

  • 字母 Z 与数字 2 互换。

  • 字母 n 与字母 h 互换。

  • 字母 B 与数字 8 互换。

  • 字母 rn 与字母 m 互换。

Polyspace 实现

Polyspace® 在两个标识符由前面列表中提到的模糊字符组合而成且不同的情况下报告一个缺陷。规则检查项在检查此规则时不会考虑变量的完全限定名。

故障排除

如果您预期会出现违规,而 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++:2008

  • MISRA C++:2023

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