Bug 67314 - No warning on assigning an out-of-range integer to an enum
Summary: No warning on assigning an out-of-range integer to an enum
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 6.0
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2015-08-21 22:34 UTC by Chengnian Sun
Modified: 2021-11-30 08:02 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail: 4.9.3, 5.1.0, 6.0
Last reconfirmed: 2019-11-20 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Chengnian Sun 2015-08-21 22:34:45 UTC
$: cat t.c
enum E {e} ee = 99;
$: gcc-trunk -Wall -Wextra -c t.c
$: clang-trunk  -c t.c
$: cat t.c
enum E {e} ee = 99;
$: gcc-trunk -Wall -Wextra -c t.c
$: clang-trunk  -Wassign-enum -c t.c
t.c:1:17: warning: integer constant not in range of enumerated type 'enum E' [-Wassign-enum]
enum E {e} ee = 99;
                ^
1 warning generated.
Comment 1 Andrew Pinski 2015-08-21 23:20:42 UTC
This warning is only useful for c++. C defines the full range of the underlying type of the enum for the enum while c++ has different rules. 

Also I think this warning would be too noisy for c code and clang is wrong to implement it for c.
Comment 2 Chengnian Sun 2015-08-21 23:54:29 UTC
(In reply to Andrew Pinski from comment #1)
> This warning is only useful for c++. C defines the full range of the
> underlying type of the enum for the enum while c++ has different rules. 
> 
> Also I think this warning would be too noisy for c code and clang is wrong
> to implement it for c.

Thanks for your reply. So in C, if I understand correctly, the enum construct is mainly to define a set of integer constants. It is not like the enumerations in other languages, such as Java, to define a set of categorical values.
Comment 3 Manuel López-Ibáñez 2015-08-22 00:30:52 UTC
In fact, this is warned by -Wc++-compat. since this is invalid in C++. I think a patch that moves this warning under a new flag -Wassign-enum which is enabled by -Wc++-compat may be accepted, but you need to try. I doubt anyone else is going to implement this for you.
Comment 4 Martin Sebor 2016-01-16 00:48:58 UTC
I recall wishing for this warning as well.  The trouble is that while gcc makes it easy to assign without a warning values to enums that are outside the range of the enumerated type, it makes it difficult to handle such values in case and switch statements without eliciting one of the -Wswitch warnings.  In those cases gcc either complains about "case values being not in enumerated type" or it complains about a "switch missing default case" and there doesn't seem to be an easy way to make it happy.  I agree with Manuel that having a warning option like -Wassign-enum would be useful.  Thus I confirm this as an enhancement request.
Comment 5 Eric Gallager 2016-01-18 20:20:10 UTC
(In reply to Martin Sebor from comment #4)
> The trouble is that while gcc makes it easy to assign without a warning
> values to enums that are outside the range of the enumerated type, it makes
> it difficult to handle such values in case and switch statements without
> eliciting one of the -Wswitch warnings.  In those cases gcc either complains
> about "case values being not in enumerated type" or it complains about a
> "switch missing default case" and there doesn't seem to be an easy way to
> make it happy.

See also Bug 61864 for more on issues with warnings having to do with the
relationship between enums and switch statements.
Comment 6 Manuel López-Ibáñez 2016-01-18 20:35:44 UTC
(In reply to Martin Sebor from comment #4)
> The trouble is that while gcc
> makes it easy to assign without a warning values to enums that are outside
> the range of the enumerated type, it makes it difficult to handle such
> values in case and switch statements without eliciting one of the -Wswitch
> warnings.  In those cases gcc either complains about "case values being not
> in enumerated type" or it complains about a "switch missing default case"
> and there doesn't seem to be an easy way to make it happy. 

Cast to int?
Comment 7 Eric Gallager 2017-11-02 00:55:35 UTC
(In reply to Eric Gallager from comment #5)
> (In reply to Martin Sebor from comment #4)
> > The trouble is that while gcc makes it easy to assign without a warning
> > values to enums that are outside the range of the enumerated type, it makes
> > it difficult to handle such values in case and switch statements without
> > eliciting one of the -Wswitch warnings.  In those cases gcc either complains
> > about "case values being not in enumerated type" or it complains about a
> > "switch missing default case" and there doesn't seem to be an easy way to
> > make it happy.
> 
> See also Bug 61864 for more on issues with warnings having to do with the
> relationship between enums and switch statements.

Bug 78736 and bug 7654 are both also related.
Comment 8 Eric Gallager 2019-11-20 05:41:00 UTC
(In reply to Eric Gallager from comment #7)
> (In reply to Eric Gallager from comment #5)
> > (In reply to Martin Sebor from comment #4)
> > > The trouble is that while gcc makes it easy to assign without a warning
> > > values to enums that are outside the range of the enumerated type, it makes
> > > it difficult to handle such values in case and switch statements without
> > > eliciting one of the -Wswitch warnings.  In those cases gcc either complains
> > > about "case values being not in enumerated type" or it complains about a
> > > "switch missing default case" and there doesn't seem to be an easy way to
> > > make it happy.
> > 
> > See also Bug 61864 for more on issues with warnings having to do with the
> > relationship between enums and switch statements.
> 
> Bug 78736 and bug 7654 are both also related.

Update: Still no warning on this testcase even after -Wenum-conversion was added to fix bug 78736