-
Notifications
You must be signed in to change notification settings - Fork 39
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
Introduce Refaster rules that resolve EnumOrdinal
violations
#1104
Conversation
Looks good. No mutations were possible for these changes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some context :)
/** Don't explicitly compare enums by their ordinal. */ | ||
abstract static class ComparingEnum<E extends Enum<E>, T> { | ||
@Placeholder(allowsIdentity = true) | ||
abstract E toEnumFunction(@MayOptionallyUse T value); | ||
|
||
@BeforeTemplate | ||
@SuppressWarnings("EnumOrdinal" /* This violation will be rewritten. */) | ||
Comparator<T> before() { | ||
return comparingInt(v -> toEnumFunction(v).ordinal()); | ||
} | ||
|
||
@AfterTemplate | ||
@UseImportPolicy(STATIC_IMPORT_ALWAYS) | ||
Comparator<T> after() { | ||
return comparing(v -> toEnumFunction(v)); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will rewrite comparingInt(e -> e.getKey().ordinal())
to comparing(v -> v.getKey())
. That result will not also be rewritten to comparingByKey()
, because MapEntryComparingByKey matches against Map.Entry::getKey
but not the associated lambda expression. This limitation applies to many of our rules, so fixing it is out of scope. (We should really "just" fix the MethodReferenceUsage
check.)
I'm mentioning this because it'll prevent a cleanup opportunity in our internal code base, I see.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or combine this with OpenRewrite's https://docs.openrewrite.org/recipes/staticanalysis/replacelambdawithmethodreference if we can...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Neat ⭐
de23007
to
0c48f7a
Compare
Quality Gate passedIssues Measures |
Looks good. No mutations were possible for these changes. |
Suggested commit message:
These rules resolve some of the violations reported by the EnumOrdinal check new in Error Prone 2.26.0.