MISRA.ETYPE.CATEGORY.DIFFERENT.2012The operands of an operator in which the usual arithmetic conversions are performed do not have the same essential type category.
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 categoryC90 [Implementation 21], C99 [Implementation 3.6(4)] Category: Required Analysis: Decidable, Single Translation Unit Applies to: C90, C99 AmplificationThis rule applies to operators that are described in usual arithmetic conversions (see C90 Section 6.2.1.5, C99 Section 6.3.1.8). This includes all the binary operators, excluding the shift, logical &&, logical || and comma operators. In addition, the second and third operands of the ternary operator are covered by this rule.
Note: the increment and decrement operators are not covered by this rule.
RationaleThe C language allows the programmer considerable freedom and will permit conversions between different arithmetic types to be performed automatically. However, the use of these implicit conversions can lead to unintended results, with the potential for loss of value, sign or precision. Further details of concerns with the C type system can be found in Appendix C. The use of stronger typing, as enforced by the MISRA essential type model, allows implicit conversions to be restricted to those that should then produce the answer expected by the developer. ExceptionThe following are permitted to allow a common form of character manipulation to be used:
Exampleenum enuma { A1, A2, A3 } ena; enum enumb { B1, B2, B3 } enb; The following are compliant as they have the same essential type category: ena > A1 u8a + u16b The following is compliant by exception 1: cha += u8a The following is non-compliant with this rule and also violates Rule 10.3: s8a += u8a /* signed and unsigned */ The following are non-compliant: u8b + 2 /* unsigned and signed */ enb > A1 /* enum<enumb> and enum<enuma> */ ena == enb /* enum<enuma> and enum<enumb> */ u8a += cha /* unsigned and char */ |