-
Notifications
You must be signed in to change notification settings - Fork 745
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New check proposal: use of Enum.compareTo
#4720
Comments
I honestly suspect that a lot of Java devs don't actually know what |
Oh, a final note: |
Pete!! Hi :)
I recall a past discussion or two about In an ideal world, maybe enums would be able to opt in or out of comparability? I suppose we could imagine declaring an annotation that you can apply to an enum to opt in or out, as seems more appropriate. Maybe we should specifically warn about protobuf enums, given that they have their own concept of "value" that does not always align with the ordinal? Perhaps this is even a reason to add to the list of reasons that proto enums shouldn't be Java enums? I found cl/17405778 (not that you can see that :)) in a search of CLs that mention enum comparability. (I also found unrelated cl/60926695, which reorders existing constant to match the desired comparison order, rather than supply a separate I'm going to collect a sample of more calls and see if enough jump out at me as scary like your version example. Of course, looking only for direct |
I checked Java Practices and saw that it has advice that "If order means anything, specify it." It also points out that ordering additionally shows up in I also made a failed attempt to deprecate |
Hi Chris! In addition to the stability thing and the For what it's worth, the project I'm working on now doesn't actually use ErrorProne, so this is very much a drive-by suggestion as I found myself wondering whether it would have caught my issue or not. I'm happy to let you folks decide on whether it makes sense or not. |
The whole thing is made worse by the fact that people find it really easy to mess up the sign when writing |
I looked at 19 files (because I asked for a sample of 20 and the 20th turned out to be some kind of compiler test :)). [edit: cl/705191034, for the record] There was a "clearly meaningful" ordering (typically documented as such, maybe always?) in 7 cases: 2 protobuf enums and 5 non-protobuf enums. One of the proto cases did seem closer to One other user (of a protobuf enum) was using The other 11 (5 proto, 6 not) seemed to be OK with an arbitrary order. One had a comment that explicitly mentioned "determinism," which of course we're big fans of :) One was the "type" enum for a protobuf I am not totally sure what conclusions to draw from all of that (other than the |
Oh, and I forgot to also say that, following up on the cl/60926695 that I mentioned earlier, I saw some other gross things:
|
Fwiw, Gradle has a JavaVersion enum that's meant to be comparable, even though it also has an isCompatibleWith method (that just delegates to compare to). In Kotlin and Groovy it makes things very readable though: (not sure how that fit in the discussion though) |
That found some matches, though not a ton. In most cases, |
The
EnumOrdinal
check wisely says that "You should almost never invoke theEnum.ordinal()
method" because it's not guaranteed to be stable. I think there should be a similarEnumCompareTo
check sinceEnum.compareTo()
compares by ordinal, and the same considerations about lack of stability apply.The text was updated successfully, but these errors were encountered: