The value of a composite expression shall not be cast to a different essential type category or a wider essential type
The value of a composite expression shall not be cast to a different essential type category or a wider essential type.
A composite expression is a non-constant expression using a composite operator. In the Essential Type Model, composite operators are:
Multiplicative (*, /, %)
Additive (binary +, binary -)
Bitwise (&, |, ^)
Shift (<<, >>)
Conditional (?, :)
Casting to a wider type is not permitted because the result may vary between implementations. Consider this expression:
(uint32_t) (u16a +u16b);
For more information on essential types, see MISRA C:2012 Rule
10.1.
The rule checker raises a defect only if the result of a composite expression is cast to a different or wider essential type.
For instance, in this example, a violation is shown in the first assignment to
i but not the second. In the first assignment, a composite expression
i+1 is directly cast from a signed to an unsigned type. In the second
assignment, the composite expression is first cast to the same type and then the result is
cast to a different
type.
typedef int int32_T; typedef unsigned char uint8_T; ... ... int32_T i; i = (uint8_T)(i+1); /* Noncompliant */ i = (uint8_T)((int32_T)(i+1)); /* Compliant */
The value of a composite expression shall not be cast to a different essential type category.
The value of a composite expression shall not be cast to a wider essential type.
If you expect a rule violation but do not see it, refer to Coding Standard Violations Not Displayed.
| Group: The Essential Type Model |
| Category: Required |
| AGC Category: Advisory |