MISRA.ETYPE.INAPPR.CAST.2012The value of an expression is cast to an inappropriate essential type.
MISRA C 2012 Rule 10.5: The value of an expression should not be cast to an inappropriate essential typeCategory: Advisory Analysis: Decidable, Single Translation Unit Applies to: C90, C99 AmplificationThe casts which should be avoided are shown in the following table, where values are cast (explicitly converted) to the essential type category of the first column.
Note: * an enumerated type may be cast to an enumerated type provided that the cast is to the same essential enumerated type. Such casts are redundant.
Casting from void to any other type is not permitted as it results in undefined behaviour. This is covered by Rule 1.3. RationaleAn explicit cast may be introduced for legitimate functional reasons, for example:
However, some explicit casts are considered inappropriate:
ExceptionAn integer constant expression with the value 0 or 1 of either signedness may be cast to a type which is defined as essentially Boolean. This allows the implementation of non-C99 Boolean models. Example( bool_t ) false /* Compliant - C99 'false' is essentially Boolean */ ( int32_t ) 3U /* Compliant */ ( bool_t ) 0 /* Compliant - by exception */ ( bool_t ) 3U /* Non-compliant */ ( int32_t ) ena /* Compliant */ ( enum enuma ) 3 /* Non-compliant */ ( char ) enc /* Compliant */ |