From 3f5867aac144898c29e809562ed1888abd6839d6 Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Tue, 16 Jan 2024 09:12:34 +0100 Subject: [PATCH] Avoid NPE in `IsEmpty` matcher --- .../errorprone/refaster/matchers/IsEmpty.java | 3 +- .../refaster/matchers/IsEmptyTest.java | 41 +++++++++++-------- 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/refaster-support/src/main/java/tech/picnic/errorprone/refaster/matchers/IsEmpty.java b/refaster-support/src/main/java/tech/picnic/errorprone/refaster/matchers/IsEmpty.java index fb0c77b748..fd44397a57 100644 --- a/refaster-support/src/main/java/tech/picnic/errorprone/refaster/matchers/IsEmpty.java +++ b/refaster-support/src/main/java/tech/picnic/errorprone/refaster/matchers/IsEmpty.java @@ -56,6 +56,7 @@ // XXX: Also recognize empty builders and `emptyBuilder.build()` invocations. public final class IsEmpty implements Matcher { private static final long serialVersionUID = 1L; + private static final Integer ZERO = 0; private static final Pattern EMPTY_INSTANCE_FACTORY_METHOD_PATTERN = Pattern.compile("empty.*"); private static final Matcher EMPTY_COLLECTION_CONSTRUCTOR_ARGUMENT = anyOf(isPrimitiveType(), isSubtypeOf(Comparator.class)); @@ -145,7 +146,7 @@ private static boolean isEmptyArrayCreation(ExpressionTree tree) { NewArrayTree newArray = (NewArrayTree) tree; return (!newArray.getDimensions().isEmpty() - && ASTHelpers.constValue(newArray.getDimensions().get(0), Integer.class) == 0) + && ZERO.equals(ASTHelpers.constValue(newArray.getDimensions().get(0), Integer.class))) || (newArray.getInitializers() != null && newArray.getInitializers().isEmpty()); } } diff --git a/refaster-support/src/test/java/tech/picnic/errorprone/refaster/matchers/IsEmptyTest.java b/refaster-support/src/test/java/tech/picnic/errorprone/refaster/matchers/IsEmptyTest.java index 318328cbec..42656cab6a 100644 --- a/refaster-support/src/test/java/tech/picnic/errorprone/refaster/matchers/IsEmptyTest.java +++ b/refaster-support/src/test/java/tech/picnic/errorprone/refaster/matchers/IsEmptyTest.java @@ -55,75 +55,80 @@ void matches() { " return new int[][] {{0}};", " }", "", - " Random negative5() {", + " int[] negative5() {", + " int i = hashCode();", + " return new int[i];", + " }", + "", + " Random negative6() {", " return new Random();", " }", "", - " List negative6() {", + " List negative7() {", " return new ArrayList<>(ImmutableList.of(1));", " }", "", - " Map negative7() {", + " Map negative8() {", " return new HashMap<>(ImmutableMap.of(1, 2));", " }", "", - " Set negative8() {", + " Set negative9() {", " return new HashSet<>(ImmutableList.of(1));", " }", "", - " Map negative9() {", + " Map negative10() {", " return new LinkedHashMap<>(ImmutableMap.of(1, 2));", " }", "", - " Set negative10() {", + " Set negative11() {", " return new LinkedHashSet<>(ImmutableList.of(1));", " }", "", - " List negative11() {", + " List negative12() {", " return new LinkedList<>(ImmutableList.of(1));", " }", "", - " Map negative12() {", + " Map negative13() {", " return new HashMap<>(ImmutableMap.of(1, 2));", " }", "", - " Set negative13() {", + " Set negative14() {", " return new HashSet<>(ImmutableList.of(1));", " }", "", - " List negative14() {", + " List negative15() {", " return new Vector<>(ImmutableList.of(1));", " }", "", - " ImmutableList negative15() {", + " ImmutableList negative16() {", " return ImmutableList.of(1);", " }", "", - " ImmutableSet negative16() {", + " ImmutableSet negative17() {", " return ImmutableSet.of(1);", " }", "", - " ImmutableMap negative17() {", + " ImmutableMap negative18() {", " return ImmutableMap.of(1, 2);", " }", "", - " ImmutableSetMultimap negative18() {", + " ImmutableSetMultimap negative19() {", " return ImmutableSetMultimap.of(1, 2);", " }", "", - " List negative19() {", + " List negative20() {", " return List.of(1);", " }", "", - " Map negative20() {", + " Map negative21() {", " return Map.of(1, 2);", " }", "", - " Set negative21() {", + " Set negative22() {", " return Set.of(1);", " }", "", - " Stream negative22() {", + " Stream negative23() {", " return Stream.of(1);", " }", "",