主要内容

MISRA C:2023 Rule 10.7

R2026b

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

自 R2024a 起

描述

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 类别:建议
PQL 名称:std.misra_c_2023.R10_7

版本历史记录

在 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:2025

  • MISRA C++:2008

  • MISRA C++:2023

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