主要内容

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

MISRA C:2012 Rule 10.7

If a composite expression is used as one operand of an operator in which the usual arithmetic conversions are performed then the other operand shall not have wider essential type

描述

If a composite expression is used as one operand of an operator in which the usual arithmetic conversions are performed, then the other operand shall not have wider essential type 1 .

理由

一个复合表达式是使用复合运算符的非常量表达式。在基本类型模型中,复合运算符包括:

  • 乘法运算符(*/%

  • 加法运算符(二进制 +、二进制 -

  • 位运算符(&|^

  • 移位运算符(<<>>

  • 条件运算符(?:

限制复合表达式中的隐式转换意味着,表达式中的算术运算序列必须使用相同的基本类型。此限制可减少混淆,避免值、符号、精度或布局丢失。然而,这条规则并不意味着表达式中的所有操作数都属于同一基本类型。

在本质上为实数浮点的类型与本质上为复数浮点的类型之间执行算术运算并不违反此规则。

有关基本类型的详细信息,请参阅Essential Type Model Used in MISRA C Rule Checking

Polyspace 实现

Polyspace® 报告复合表达式中的操作数被隐式转换为更宽的基本类型时,违反了此规则。

对于指针运算,以及本质上为实数浮点类型与本质上为复数浮点类型之间的转换,Polyspace 不会报告此规则的违规。

故障排除

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

示例

全部展开

此示例显示复合表达式中的合规和不合规算术运算。

#include <stdint.h>
#include <complex.h>
void foo() {
	uint16_t u16x = 0, u16y = 0;
	uint32_t u32x = 0, u32y = 0;
	u32x * (u16x + u16y);         // Noncompliant
	u32x = (u16x + u16y) + u32x;  // Noncompliant
	

}


void bar() {
	_Complex float cx;
	_Complex double dx;
	float f;
	double d;

	cx + f;         // Compliant
	cx + d;         // Compliant
}

foo() 中,当一个操作数被隐式转换为更宽的基本类型时,Polyspace 会报告违规情况。在 bar() 中,复合表达式将本质上为实数的浮点数隐式转换为本质上为复数的浮点数。这些隐式转换是合规的。

检查信息

组:基本类型模型
类别:必需
AGC 类别:建议

版本历史记录

全部展开


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.