主要内容

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

MISRA C++:2008 Rule 8-4-2

The identifiers used for the parameters in a re-declaration of a function shall be identical to those in the declaration

描述

规则定义

The identifiers used for the parameters in a re-declaration of a function shall be identical to those in the declaration. 1

理由

在重新声明中使用不同的标识符可能会导致开发人员混淆。在函数声明和函数重新声明中使用相同的参数标识符有助于使代码更易于理解、维护和记录。

Polyspace 实现

检查项检测以下参数名称之间是否不匹配:

  • 函数声明和相应的函数定义。

  • 在同一文件中声明了两个函数。

    如果声明出现在不同的文件中,检查器不会报告参数名称不匹配的违规情况。在不同的文件中声明相同的函数违反了 MISRA C++:2008 Rule 3-2-3

故障排除

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

示例

全部展开

在此代码中,函数 sumValues() 的重新声明使用了参数 int xint y,而不是原始声明中的参数 int sumAint sumB。在这种情况下,在重新声明中使用参数 int sumAint sumB 可使代码合规。

void sumValues(int sumA, int sumB);

void sumValues(int x, int y) { 	//Noncompliant
    int sum = x + y;
}

int main() {
	
    //...
    return 0;
}

检查信息

组:声明符
类别:必需

版本历史记录

在 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.