Skip to content

Commit 664e9dd

Browse files
committed
Prevent NPE in Enum equals with empty select
Fixes #143
1 parent be14ab4 commit 664e9dd

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/main/java/org/openrewrite/staticanalysis/CompareEnumsWithEqualityOperator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
5555
@Override
5656
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext executionContext) {
5757
J.MethodInvocation m = (J.MethodInvocation) super.visitMethodInvocation(method, executionContext);
58-
if (enumEquals.matches(m)) {
58+
if (enumEquals.matches(m) && m.getSelect() != null) {
5959
Cursor parent = getCursor().dropParentUntil(is -> is instanceof J.Unary || is instanceof J.Block);
6060
boolean isNot = parent.getValue() instanceof J.Unary && ((J.Unary) parent.getValue()).getOperator() == J.Unary.Type.Not;
6161
if (isNot) {

src/test/java/org/openrewrite/staticanalysis/CompareEnumsWithEqualityOperatorTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public void defaults(RecipeSpec spec) {
2929
spec.recipe(new CompareEnumsWithEqualityOperator());
3030
}
3131

32+
//language=java
3233
SourceSpecs enumA = java(
3334
"""
3435
package a;
@@ -198,4 +199,21 @@ void m(Object parameterValue, Type partType) {
198199
"""
199200
));
200201
}
202+
203+
@Test
204+
@Issue("https://github.com/openrewrite/rewrite-static-analysis/issues/143")
205+
void noSelect() {
206+
rewriteRun(
207+
//language=java
208+
java("""
209+
package a;
210+
public enum A {
211+
FOO, BAR, BUZ;
212+
boolean isFoo() {
213+
return equals(FOO);
214+
}
215+
}
216+
""")
217+
);
218+
}
201219
}

0 commit comments

Comments
 (0)