I would appreciate adding the enumeration typecast warnings. I mean: When I cast one enum to another and the first enum is not a subset of the second warning will be shown. Example: enum EParent { EP_VAL0 = 0, EP_VAL1 = 1, EP_VAL2 = 2 }; enum EChild { EC_VAL0 = 0, EC_VAL1 = 1 }; int main() { EChild childValue; EParent parentValue; ... childValue = (EChild)parentValue; // Warning, because value 2 is not present in EChild. parentValue = (EParent)childValue; // Ok, because all EChild values are present in EParent. return 0; };
Related to PR 12242.
I am not sure this is such a good idea. A casting typically means "I want to really do this". GCC normally suppress warnings when casting is added. A warning when you assign when enum type to another and the first enum is not a subset of the second could still be useful, but the casting should prevent the warning. I am not sure what happens for such assignment, is it undefined behaviour? If so, perhaps it should be warned by Wundefined. If not, then perhaps we could add it to Wconversion.
(In reply to comment #2) > I am not sure this is such a good idea. A casting typically means "I want to > really do this". GCC normally suppress warnings when casting is added. A > warning when you assign when enum type to another and the first enum is not a > subset of the second could still be useful, but the casting should prevent the > warning. That won't even compile without a cast, in C++ there is no implicit conversion to enumeration types. > I am not sure what happens for such assignment, is it undefined behaviour? If the source value is within the range of the target enum it's well-defined. It might be reasonable to warn as part of -Wconversion except that usually applies to implicit conversions, and for enums the implicit conversion isn't allowed.
(In reply to Andrew Pinski from comment #1) > Related to PR 12242. And also bug 67314 and bug 7654 (assignments without the cast), bug 78736 and bug 60591 (conversions), and bug 52763 (comparisons). Should probably be closed as a dup of one of them. Or maybe the other way around.
(In reply to Eric Gallager from comment #4) > (In reply to Andrew Pinski from comment #1) > > Related to PR 12242. > > And also bug 67314 and bug 7654 (assignments without the cast), bug 78736 > and bug 60591 (conversions), and bug 52763 (comparisons). Should probably be > closed as a dup of one of them. Or maybe the other way around. Any preferences as to which way to go?
(In reply to Marek Chovanec from comment #0) > I would appreciate adding the enumeration typecast warnings. I mean: > When I cast one enum to another and the first enum is not a subset of the > second warning will be shown. > > Example: > > enum EParent > { > EP_VAL0 = 0, > EP_VAL1 = 1, > EP_VAL2 = 2 > }; > > enum EChild > { > EC_VAL0 = 0, > EC_VAL1 = 1 > }; > > int main() > { > EChild childValue; > EParent parentValue; > > ... > > childValue = (EChild)parentValue; // Warning, because value 2 is not > present in EChild. > > parentValue = (EParent)childValue; // Ok, because all EChild values are > present in EParent. > > return 0; > }; At least as far as this particular testcase goes, there's a warning from -Wuninitialized at least... if you initialize parentValue to EP_VAL2 like I think you intended, I can confirm that there are no warnings then, even after -Wenum-conversion was added for bug 78736. Since that was for just C, though, and this is C++, I guess that would make this bug 92159? But that was closed as WORKSFORME though... I dunno, I'll just confirm this as separate for now...
(In reply to Eric Gallager from comment #6) > At least as far as this particular testcase goes, there's a warning from > -Wuninitialized at least... if you initialize parentValue to EP_VAL2 like I > think you intended, I can confirm that there are no warnings then, even > after -Wenum-conversion was added for bug 78736. Since that was for just C, > though, and this is C++, I guess that would make this bug 92159? But that > was closed as WORKSFORME though... Because as I said in comment 3 above, implicit conversions between distinct enum types is ill-formed in C++. You don't need a warning, because it's already an error. I think this is just another case of a user not understanding how enums actually work in C++. Values of the enum type which do not correspond to an enumerator are still valid values of the type. Adding a warning for explicit conversions between distinct types doesn't seem useful (as Manu said, you should know what you're doing if you use an explicit cast to do conversions that aren't allowed implicitly). My inclination is INVALID or WONTFIX.