主要内容

MISRA C:2023 Dir 4.5

Identifiers in the same name space with overlapping visibility should be typographically unambiguous

自 R2024a 起

描述

指令定义

Identifiers in the same name space with overlapping visibility should be typographically unambiguous 1 .

理由

“明确易辨”的含义取决于编写源代码时所用的字母表和语言。当您使用在排字上很接近的标识符时,您可能会产生混淆。

对于英语单词中使用的拉丁字母,至少要避免标识符之间只有以下区别:

  • 小写字母与其对应的大写字母互换。

  • 是否存在下划线字符。

  • 字母 O 与数字 0 互换。

  • 字母 I 与数字 1 互换。

  • 字母 I 与字母 l 互换。

  • 字母 S 与数字 5 互换。

  • 字母 Z 与数字 2 互换。

  • 字母 n 与字母 h 互换。

  • 字母 B 与数字 8 互换。

  • 字母 rn 与字母 m 互换。

Polyspace 实现

检查项会标记在同一作用域内且仅存在上述字符差异的标识符。Polyspace® 会忽略以 doforswitchwhile 开头的宏中定义的标识符。

如果两个标识符的作用域不同,它们的使用就不会产生歧义。对于作用域不同的标识符,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 */

	int numVal;
}

void foo() {
	int numval;
}

在此示例中,当使用可能会相互混淆的标识符时,便会违反规则。在不同作用域中声明的类似标识符不会被报告为违反此规则。

检查信息

组:代码设计
类别:建议
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.