enum xpto { A = 0, B = 1, X = 512 }; extern void print (unsigned int); unsigned char bar (enum xpto a) { return a; } We don't get currently a warning for this return conversion if we use --short-enums. With -O2 --short-enums, sizeof enum xpto == 2, but sizeof unsigned char == 1, therefore we should warn the user there's loss of precision.
The code in unsafe_conversion_p does not handle enumeral_type only integer_type and real_type. Somebody will need to update it to handle it, being careful with existing warnings in the C++ FE that do warn about enumeral type conversions. Clang does warn: pr60591.c:11:10: warning: implicit conversion loses integer precision: 'enum xpto' to 'unsigned char' [-Wconversion] return a; ~~~~~~ ^
There are several other bugs open like this one, such as bug 78736
Clang++ gives a signedness conversion warning that g++ doesn't for C++ code. #1 with x86-64 clang (trunk) <source>:4:22: warning: implicit conversion changes signedness: 'things' to 'unsigned int' [-Wsign-conversion] unsigned int i = thing; ~ ^~~~~ 1 warning generated. Compiler returned: 0 enum things { thing=-1 }; //< declaration of a constant of type int int main() { unsigned int i = thing; return (int)i; }
Clang++ gives a nice error, can gcc improve to also make it clear it is an enum? -Wall -Wconversion -Wextra 1 <Compilation failed> <Compilation failed> Find x86-64 clang (trunk) - 349ms #1 with x86-64 clang (trunk) <source>:8:9: error: assigning to 'enum hello' from incompatible type 'enum things' h = a; ^ 1 error generated. Compiler returned: 1 #1 with x86-64 gcc (trunk) <source>: In function 'int main()': <source>:8:9: error: cannot convert 'things' to 'hello' in assignment 8 | h = a; | ^ | | | things Compiler returned: 1 enum things { thing=-1 }; enum hello {t}; int main() { enum things a; a= thing; enum hello h; h = a; return (int)a; }
(In reply to Eric Gallager from comment #2) > There are several other bugs open like this one, such as bug 78736 This is fixed now. It's probably still worth checking some of the other bugs under its "See Also" section though.