[previous] 1484 [next] Constant expression cast to enum type.
Arithmetic type - Enum types REFERENCE - ISO:C90-6.5.2.2 Enumeration Specifiers

The value of a non-constant expression is being cast to an enum type. This is permitted in the C language but it is rather an odd thing to do. Is it intended ?

If the value of the expression is a valid constant within the enumeration type, it would be better practice to use the symbolic constant instead. If the value is not valid, it may still be stored in the enum object but this would be a serious abuse of the enum type and a further message will be generated, either 1461 or 1317.

For example:


/*PRQA S 2017,2211,2213,3120,3198,3227,3408,3447,3448,3602,4130 ++*/

typedef enum { RED = 1, GREEN = 2, BLUE = 4 } COLOUR;

extern COLOUR col;

extern void foo(void)
{
    col = (COLOUR)2;                               /* Message 1484        */
    col = (COLOUR)5;                               /* Message 1484 + 1461 */
    col = (COLOUR)10;                              /* Message 1484 + 1317 */

    return;
}


No MISRA C:2012 Rules applicable to message 1484


See also:

QA·C Source Code Analyser 8.1
MISRA C:2012 Compliance Module 1.0
© 2012 Programming Research.
www.programmingresearch.com
Personality Groups | Glossary | Message Index | MISRA C:2012 Rule Index Contents