Hi,
Our static analyzer is flagging this code:
The rationale from the supplier is:
We argue however that the code is perfectly safe and idiomatic, the Standard guarantees that the conversion is made to an integer able to hold all the values of the enumerator, so it's impossible for data loss to occur.
What is your opinion, is M5-0-4 truly applicable in this case?
Thanks!
Our static analyzer is flagging this code:
Code:
// third_party.h
typedef enum
{
FOO_A,
FOO_B
} Foo;
// main.cpp
#include <third_party.h>
int get(Foo x)
{
switch(x) // Non-compliant with M5-0-4 ?
{
case FOO_A: // Non-compliant with M5-0-4 ?
return 123;
case FOO_B: // Non-compliant with M5-0-4 ?
return 321;
}
return -1;
}The rationale from the supplier is:
- "Foo" is an unscoped enum without fixed underlying type.
- Thus, its underlying type is implementation-defined.
- On this implementation, the actual type is "unsigned int".
- The line with "switch(x)" performs an integral promotion of "x" to "int", thus changing the signedness from "unsigned int" to "int".
- Similarly, the "case FOO_*" lines also perform an integral promotion to "int".
- This change in signedness is a violation of M5-0-4.
We argue however that the code is perfectly safe and idiomatic, the Standard guarantees that the conversion is made to an integer able to hold all the values of the enumerator, so it's impossible for data loss to occur.
What is your opinion, is M5-0-4 truly applicable in this case?
Thanks!

