From 3f6e01790514e3d8bb0f48a4ed88650cd6e9f12f Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Sun, 6 Aug 2023 17:41:13 +0200 Subject: [PATCH] One more --- .../errorprone/refaster/matchers/IsEmpty.java | 5 +++- .../refaster/matchers/IsEmptyTest.java | 27 +++++++++++-------- 2 files changed, 20 insertions(+), 12 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 7cbb8560e01..c21a6fd204c 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 @@ -1,5 +1,6 @@ package tech.picnic.errorprone.refaster.matchers; +import static com.google.common.base.Verify.verify; import static com.google.errorprone.matchers.FieldMatchers.staticField; import static com.google.errorprone.matchers.Matchers.allOf; import static com.google.errorprone.matchers.Matchers.anyOf; @@ -47,6 +48,7 @@ public final class IsEmpty implements Matcher { private static final long serialVersionUID = 1L; private static final Pattern EMPTY_INSTANCE_FACTORY_METHOD_PATTERN = Pattern.compile("empty.*"); private static final Matcher PRIMITIVE_TYPE = isPrimitiveType(); + // XXX: Extend this list to include additional JDK collection types with a public constructor. private static final Matcher MUTABLE_COLLECTION_TYPE = anyOf( isSameType(ArrayList.class), @@ -117,7 +119,8 @@ private boolean isEmptyCollectionConstructor(ExpressionTree tree, VisitorState s * This looks like a copy constructor, in which case the resultant collection is empty if its * argument is. */ - return arguments.size() == 1 && matches(arguments.get(0), state); + verify(arguments.size() == 1, "Unexpected %s-ary constructor", arguments.size()); + return matches(arguments.get(0), state); } private static boolean isEmptyArrayCreation(ExpressionTree tree) { 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 46a71800e3a..705d7b5f994 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 @@ -24,6 +24,7 @@ void matches() { "import java.util.LinkedList;", "import java.util.List;", "import java.util.Map;", + "import java.util.Random;", "import java.util.Set;", "import java.util.Stack;", "import java.util.Vector;", @@ -49,47 +50,51 @@ void matches() { " return new int[][] {{0}};", " }", "", - " List negative5() {", + " Random negative5() {", + " return new Random();", + " }", + "", + " List negative6() {", " return new ArrayList<>(ImmutableList.of(1));", " }", "", - " Map negative6() {", + " Map negative7() {", " return new HashMap<>(ImmutableMap.of(1, 2));", " }", "", - " Map negative7() {", + " Map negative8() {", " return new LinkedHashMap<>(ImmutableMap.of(1, 2));", " }", "", - " ImmutableList negative8() {", + " ImmutableList negative9() {", " return ImmutableList.of(1);", " }", "", - " ImmutableSet negative9() {", + " ImmutableSet negative10() {", " return ImmutableSet.of(1);", " }", "", - " ImmutableMap negative10() {", + " ImmutableMap negative11() {", " return ImmutableMap.of(1, 2);", " }", "", - " ImmutableSetMultimap negative11() {", + " ImmutableSetMultimap negative12() {", " return ImmutableSetMultimap.of(1, 2);", " }", "", - " List negative12() {", + " List negative13() {", " return List.of(1);", " }", "", - " Map negative13() {", + " Map negative14() {", " return Map.of(1, 2);", " }", "", - " Set negative14() {", + " Set negative15() {", " return Set.of(1);", " }", "", - " Stream negative15() {", + " Stream negative16() {", " return Stream.of(1);", " }", "",