diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/UnnecessaryBoxedAssignment.java b/core/src/main/java/com/google/errorprone/bugpatterns/UnnecessaryBoxedAssignment.java index 1aff9bdcf8a..a7aeecbde11 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/UnnecessaryBoxedAssignment.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/UnnecessaryBoxedAssignment.java @@ -65,7 +65,8 @@ public Description matchVariable(VariableTree tree, VisitorState state) { } private Description matchCommon(ExpressionTree expression, VisitorState state) { - if (expression == null || !VALUE_OF_MATCHER.matches(expression, state)) { + if (!(expression instanceof MethodInvocationTree) + || !VALUE_OF_MATCHER.matches(expression, state)) { return Description.NO_MATCH; } MethodInvocationTree methodInvocationTree = (MethodInvocationTree) expression; diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/testdata/UnnecessaryBoxedAssignmentCases.java b/core/src/test/java/com/google/errorprone/bugpatterns/testdata/UnnecessaryBoxedAssignmentCases.java index 79009c45657..501a303e143 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/testdata/UnnecessaryBoxedAssignmentCases.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/testdata/UnnecessaryBoxedAssignmentCases.java @@ -16,6 +16,8 @@ package com.google.errorprone.bugpatterns.testdata; +import java.util.function.Function; + /** * @author awturner@google.com (Andy Turner) */ @@ -76,4 +78,8 @@ Integer positive_wrappedAgain(int aInteger) { a = Integer.valueOf(aInteger); return Integer.valueOf(a); } + + void negative_methodReference() { + Function toBoolean = Boolean::valueOf; + } } diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/testdata/UnnecessaryBoxedAssignmentCases_expected.java b/core/src/test/java/com/google/errorprone/bugpatterns/testdata/UnnecessaryBoxedAssignmentCases_expected.java index 87216d4087c..fc3fd40fd30 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/testdata/UnnecessaryBoxedAssignmentCases_expected.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/testdata/UnnecessaryBoxedAssignmentCases_expected.java @@ -16,6 +16,8 @@ package com.google.errorprone.bugpatterns.testdata; +import java.util.function.Function; + /** * @author awturner@google.com (Andy Turner) */ @@ -76,4 +78,8 @@ Integer positive_wrappedAgain(int aInteger) { a = aInteger; return a; } + + void negative_methodReference() { + Function toBoolean = Boolean::valueOf; + } }