MISRA C:2023 Rule 10.8
The value of a composite expression shall not be cast to a different essential type category or a wider essential type
Since R2024a
Description
Rule Definition
The value of a composite expression shall not be cast to a different essential type category or a wider essential type.
Rationale
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 (
?
,:
)
Unary operators such as ~
and unary +
or -
are also considered composite operators.
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
.
Polyspace Implementation
The rule checker reports a violation only if the result of a composite expression is directly cast to a different or wider essential type. Unary operators are not considered as composite operators.
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. Since the composite expression is not directly cast to a different type, the checker does not raise a
violation.
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 */
Polyspace® does not report a violation of this rule for these cases:
An essentially real floating expression is cast to an essentially complex floating type but the corresponding real type of the complex type is not wider than the type of the real expression.
An essentially real complex expression is cast to an essentially real floating type but the floating type is not wider than the corresponding real type of the complex expression.
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 |
Version History
Introduced in R2024a