Bug 60591 - Report enum conversions as part of Wconversion
Summary: Report enum conversions as part of Wconversion
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.9.0
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2014-03-19 16:07 UTC by pmatos
Modified: 2021-11-29 19:01 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2015-08-22 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description pmatos 2014-03-19 16:07:53 UTC
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.
Comment 1 Manuel López-Ibáñez 2014-03-19 16:50:48 UTC
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;
  ~~~~~~ ^
Comment 2 Eric Gallager 2018-02-20 09:25:01 UTC
There are several other bugs open like this one, such as bug 78736
Comment 3 Jonny Grant 2019-04-19 12:55:37 UTC
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;
}
Comment 4 Jonny Grant 2019-04-19 13:00:03 UTC
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;
}
Comment 5 Eric Gallager 2019-10-19 05:35:55 UTC
(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.