From dd50bdfa6f3691432a0a9b56a957fc24d1315fe1 Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Sat, 21 Oct 2023 08:51:30 -0700 Subject: [PATCH] UnnecessaryBoxedAssignment: avoid CCE on method reference Fixes #4157 FUTURE_COPYBARA_INTEGRATE_REVIEW=https://github.com/google/error-prone/pull/4157 from PicnicSupermarket:sschroevers/avoid-CCE-in-UnnecessaryBoxedAssignment 73dc35858a9842431e2df7ce7ec44d6c24effcc6 PiperOrigin-RevId: 575469406 --- .../errorprone/bugpatterns/UnnecessaryBoxedAssignment.java | 3 ++- .../testdata/UnnecessaryBoxedAssignmentCases.java | 6 ++++++ .../testdata/UnnecessaryBoxedAssignmentCases_expected.java | 6 ++++++ 3 files changed, 14 insertions(+), 1 deletion(-) 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; + } }