MISRA C:2012 Rule 10.4
Both operands of an operator in which the usual arithmetic conversions are performed shall have the same essential type category
Description
Note
Using Code Prover for checking coding rules is no longer supported. See Version History.
Rule Definition
Both operands of an operator in which the usual arithmetic conversions are performed shall have the same essential type category.
Rationale
The use of implicit conversions between types can lead to unintended results, including possible loss of value, sign, or precision.
For more information on essential types, see Essential Types in MISRA C Rules 10.x.
Polyspace Implementation
The checker reports a violation of this rule if the two operands of an operation have different essential types. The checker message states the types detected on the two sides of the operation.
The checker does not report a violation of this rule:
If one of the operands is the constant zero.
If one of the operands is a signed constant and the other operand is unsigned, and the signed constant has the same representation as its unsigned equivalent. For instance, the statement
u8b = u8a + 3;
, whereu8a
andu8b
areunsigned char
variables, does not violate the rule because the constants3
and3U
have the same representation.
These cases are not true violations based on the rationale behind the rule. For instance, if a signed constant has the same representation as its unsigned equivalent, there is no risk of the value, sign or precision loss that this rule seeks to avoid.
Troubleshooting
If you expect a rule violation but do not see it, refer to Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
Group: The Essential Type Model |
Category: Required |
AGC Category: Advisory |