diff --git a/src/main/java/org/openrewrite/staticanalysis/EqualsAvoidsNullVisitor.java b/src/main/java/org/openrewrite/staticanalysis/EqualsAvoidsNullVisitor.java index f443df249..932d2d6dd 100644 --- a/src/main/java/org/openrewrite/staticanalysis/EqualsAvoidsNullVisitor.java +++ b/src/main/java/org/openrewrite/staticanalysis/EqualsAvoidsNullVisitor.java @@ -47,16 +47,11 @@ public class EqualsAvoidsNullVisitor
extends JavaVisitor
{ private static final String JAVA_LANG_STRING = "java.lang.String "; - private static final MethodMatcher EQUALS = new MethodMatcher(JAVA_LANG_STRING + - "equals(java.lang.Object)"); - private static final MethodMatcher EQUALS_IGNORE_CASE = new MethodMatcher(JAVA_LANG_STRING + - "equalsIgnoreCase(java.lang.String)"); - private static final MethodMatcher COMPARE_TO = new MethodMatcher(JAVA_LANG_STRING + - "compareTo(java.lang.String)"); - private static final MethodMatcher COMPARE_TO_IGNORE_CASE = new MethodMatcher(JAVA_LANG_STRING + - "compareToIgnoreCase(java.lang.String)"); - private static final MethodMatcher CONTENT_EQUALS = new MethodMatcher(JAVA_LANG_STRING + - "contentEquals(java.lang.CharSequence)"); + private static final MethodMatcher EQUALS = new MethodMatcher(JAVA_LANG_STRING + "equals(java.lang.Object)"); + private static final MethodMatcher EQUALS_IGNORE_CASE = new MethodMatcher(JAVA_LANG_STRING + "equalsIgnoreCase(java.lang.String)"); + private static final MethodMatcher COMPARE_TO = new MethodMatcher(JAVA_LANG_STRING + "compareTo(java.lang.String)"); + private static final MethodMatcher COMPARE_TO_IGNORE_CASE = new MethodMatcher(JAVA_LANG_STRING + "compareToIgnoreCase(java.lang.String)"); + private static final MethodMatcher CONTENT_EQUALS = new MethodMatcher(JAVA_LANG_STRING + "contentEquals(java.lang.CharSequence)"); EqualsAvoidsNullStyle style; @@ -108,10 +103,8 @@ private void maybeHandleParentBinary(J.MethodInvocation m) { if (parent instanceof J.Binary) { if (((J.Binary) parent).getOperator() == J.Binary.Type.And && ((J.Binary) parent).getLeft() instanceof J.Binary) { J.Binary potentialNullCheck = (J.Binary) ((J.Binary) parent).getLeft(); - if (isNullLiteral(potentialNullCheck.getLeft()) && matchesSelect(potentialNullCheck.getRight(), - requireNonNull(m.getSelect())) || - isNullLiteral(potentialNullCheck.getRight()) && matchesSelect(potentialNullCheck.getLeft(), - requireNonNull(m.getSelect()))) { + if (isNullLiteral(potentialNullCheck.getLeft()) && matchesSelect(potentialNullCheck.getRight(), requireNonNull(m.getSelect())) || + isNullLiteral(potentialNullCheck.getRight()) && matchesSelect(potentialNullCheck.getLeft(), requireNonNull(m.getSelect()))) { doAfterVisit(new RemoveUnnecessaryNullCheck<>((J.Binary) parent)); } } diff --git a/src/test/java/org/openrewrite/staticanalysis/EqualsAvoidsNullTest.java b/src/test/java/org/openrewrite/staticanalysis/EqualsAvoidsNullTest.java index 0a33883df..629289ae0 100644 --- a/src/test/java/org/openrewrite/staticanalysis/EqualsAvoidsNullTest.java +++ b/src/test/java/org/openrewrite/staticanalysis/EqualsAvoidsNullTest.java @@ -326,8 +326,7 @@ private void isFoo(List