[previous] MISRA-C:2004  Rule  10.3:  (Required) [next] The value of a complex expression of integer type shall only be cast to a type of the same signedness that is no wider than the underlying type of the expression.
If a cast is to be used on any complex expression, the type of cast that may be applied is severely restricted. Conversions on complex expressions are often a source of confusion and it is therefore wise to be cautious. In order to comply with these rules, it may be necessary to use a temporary variable and introduce an extra statement.

... ( uint32_t )( u16a + u16b )               /* not compliant */
... ( uint32_t )u16a + u16b                   /* compliant     */
... ( uint32_t )u16a + ( uint32_t )u16b       /* compliant     */
... ( int16_t )( s32a - 12345 )               /* compliant     */
... ( uint8_t )( u16a * u16b )                /* compliant     */
... ( uint16_t )( u8a * u8b )                 /* not compliant */
... ( int16_t )( s32a * s32b )                /* compliant     */
... ( int32_t )( s16a * s16b )                /* not compliant */
... ( uint16_t )( f64a + f64b )               /* not compliant */
... ( float32_t )( u16a + u16b )              /* not compliant */
... ( float64_t )foo1( u16a + u16b )          /* compliant     */
... ( int32_t )buf16a[ u16a + u16b ]          /* compliant     */


QAC messages that encompass this guideline:

4118 Result of integer division operation cast to a floating type.
4390 A composite expression of 'essentially signed' type (%1s) is being cast to a wider signed type, '%2s'.
4391 A composite expression of 'essentially unsigned' type (%1s) is being cast to a wider unsigned type, '%2s'.
4393 A composite expression of 'essentially signed' type (%1s) is being cast to a different type category, '%2s'.
4394 A composite expression of 'essentially unsigned' type (%1s) is being cast to a different type category, '%2s'.



(c) The Motor Industry Research Association, 2004
QA C Source Code Analyser 8.1.2
MISRA-C:2004 Compliance Module 3.2
© 2013 Programming Research
www.programmingresearch.com
Personality Groups | Glossary | Message Index | MISRA-C:2004 Rule Index Contents