主要内容

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

MISRA C:2012 Rule 8.3

All declarations of an object or function shall use the same names and type qualifiers

描述

规则定义

All declarations of an object or function shall use the same names and type qualifiers 1 .

理由

在同一对象或函数的声明中一致使用参数名称和类型,有助于加强类型检查。这样更容易检查所有声明中是否使用了相同的函数接口。

例外情况:

  • 使用相同基本类型的兼容版本不会违反此规则。例如,您可以互换使用 signedintsigned int

  • 使用未命名的函数参数并不违反此规则。例如,将声明 void foo(int a) 与声明 void foo(int) 互换使用。

Polyspace 实现

规则检查项可检测多个声明之间或声明与定义之间参数名称或数据类型不同的情况。检查项会考虑所有翻译单元中的声明,并标记编译器可能无法检测到的错误。

故障排除

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

示例

全部展开

extern int div (int num, int den);

int div(int den, int num) { /* Non compliant */
    return(num/den);
}

在此示例中,由于声明和定义中的参数名称被调换,因此违反了该规则。

typedef unsigned short width;
typedef unsigned short height;
typedef unsigned int area;

extern area calculate(width w, height h);

area calculate(width w, width h) { /* Non compliant */
    return w*h;
}

在此示例中,由于函数 calculate 的第二个参量数据类型为:

  • 在声明中为 height

  • 在定义中为 width

即使 heightwidth 的底层类型相同,该规则仍被违反。

检查信息

组:声明和定义
类别:必需
AGC 类别:必需

版本历史记录

在 R2014b 中推出

全部展开


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.