Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid NPE in IsEmpty matcher #978

Merged
merged 1 commit into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
// XXX: Also recognize empty builders and `emptyBuilder.build()` invocations.
public final class IsEmpty implements Matcher<ExpressionTree> {
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<Tree> EMPTY_COLLECTION_CONSTRUCTOR_ARGUMENT =
anyOf(isPrimitiveType(), isSubtypeOf(Comparator.class));
Expand Down Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Integer> negative6() {",
" List<Integer> negative7() {",
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this renaming is a mess. But proposing an alternative is not on today's menu.

" return new ArrayList<>(ImmutableList.of(1));",
" }",
"",
" Map<Integer, Integer> negative7() {",
" Map<Integer, Integer> negative8() {",
" return new HashMap<>(ImmutableMap.of(1, 2));",
" }",
"",
" Set<Integer> negative8() {",
" Set<Integer> negative9() {",
" return new HashSet<>(ImmutableList.of(1));",
" }",
"",
" Map<Integer, Integer> negative9() {",
" Map<Integer, Integer> negative10() {",
" return new LinkedHashMap<>(ImmutableMap.of(1, 2));",
" }",
"",
" Set<Integer> negative10() {",
" Set<Integer> negative11() {",
" return new LinkedHashSet<>(ImmutableList.of(1));",
" }",
"",
" List<Integer> negative11() {",
" List<Integer> negative12() {",
" return new LinkedList<>(ImmutableList.of(1));",
" }",
"",
" Map<Integer, Integer> negative12() {",
" Map<Integer, Integer> negative13() {",
" return new HashMap<>(ImmutableMap.of(1, 2));",
" }",
"",
" Set<Integer> negative13() {",
" Set<Integer> negative14() {",
" return new HashSet<>(ImmutableList.of(1));",
" }",
"",
" List<Integer> negative14() {",
" List<Integer> negative15() {",
" return new Vector<>(ImmutableList.of(1));",
" }",
"",
" ImmutableList<Integer> negative15() {",
" ImmutableList<Integer> negative16() {",
" return ImmutableList.of(1);",
" }",
"",
" ImmutableSet<Integer> negative16() {",
" ImmutableSet<Integer> negative17() {",
" return ImmutableSet.of(1);",
" }",
"",
" ImmutableMap<Integer, Integer> negative17() {",
" ImmutableMap<Integer, Integer> negative18() {",
" return ImmutableMap.of(1, 2);",
" }",
"",
" ImmutableSetMultimap<Integer, Integer> negative18() {",
" ImmutableSetMultimap<Integer, Integer> negative19() {",
" return ImmutableSetMultimap.of(1, 2);",
" }",
"",
" List<Integer> negative19() {",
" List<Integer> negative20() {",
" return List.of(1);",
" }",
"",
" Map<Integer, Integer> negative20() {",
" Map<Integer, Integer> negative21() {",
" return Map.of(1, 2);",
" }",
"",
" Set<Integer> negative21() {",
" Set<Integer> negative22() {",
" return Set.of(1);",
" }",
"",
" Stream<Integer> negative22() {",
" Stream<Integer> negative23() {",
" return Stream.of(1);",
" }",
"",
Expand Down