Skip to content

Commit 1e2c8ac

Browse files
committed
Fix an NPE in MultipleUnaryOperatorsInMethodCall
Discovered while validating 2.4.0 release (#1639) ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=313711584
1 parent d216dc1 commit 1e2c8ac

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

core/src/main/java/com/google/errorprone/bugpatterns/MultipleUnaryOperatorsInMethodCall.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616
package com.google.errorprone.bugpatterns;
1717

1818
import static com.google.errorprone.BugPattern.SeverityLevel.WARNING;
19+
import static com.google.errorprone.matchers.Description.NO_MATCH;
20+
import static java.util.stream.Collectors.groupingBy;
1921

2022
import com.google.common.collect.ImmutableSet;
23+
import com.google.common.collect.Sets;
2124
import com.google.errorprone.BugPattern;
2225
import com.google.errorprone.VisitorState;
2326
import com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher;
@@ -38,7 +41,7 @@ public class MultipleUnaryOperatorsInMethodCall extends BugChecker
3841
implements MethodInvocationTreeMatcher {
3942

4043
private static final ImmutableSet<Kind> UNARY_OPERATORS =
41-
ImmutableSet.of(
44+
Sets.immutableEnumSet(
4245
Kind.POSTFIX_DECREMENT,
4346
Kind.POSTFIX_INCREMENT,
4447
Kind.PREFIX_DECREMENT,
@@ -47,16 +50,16 @@ public class MultipleUnaryOperatorsInMethodCall extends BugChecker
4750
@Override
4851
public Description matchMethodInvocation(
4952
MethodInvocationTree methodInvocationTree, VisitorState visitorState) {
50-
5153
if (methodInvocationTree.getArguments().stream()
5254
.filter(arg -> UNARY_OPERATORS.contains(arg.getKind()))
5355
.map(arg -> ASTHelpers.getSymbol(((UnaryTree) arg).getExpression()))
54-
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
56+
.filter(sym -> sym != null)
57+
.collect(groupingBy(Function.identity(), Collectors.counting()))
5558
.entrySet()
5659
.stream()
5760
.anyMatch(e -> e.getValue() > 1)) {
5861
return describeMatch(methodInvocationTree);
5962
}
60-
return Description.NO_MATCH;
63+
return NO_MATCH;
6164
}
6265
}

core/src/test/java/com/google/errorprone/bugpatterns/testdata/MultipleUnaryOperatorsInMethodCallNegativeCases.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@
1717

1818
/** @author [email protected] (Marsela Sulku) */
1919
public class MultipleUnaryOperatorsInMethodCallNegativeCases {
20-
public static void tests(int a, int b) {
20+
public static void tests(int a, int b, int[] xs) {
2121
testMethod(a, b);
2222
testMethod(a + 1, b);
2323
testMethod(b, a + 1);
2424
testMethod(a++, b);
2525
testMethod(--a, b);
2626
testMethod(a, b--);
2727
testMethod(a, ++b);
28+
testMethod(xs[0]++, xs[0]++);
2829
}
2930

3031
public static void testMethod(int one, int two) {}

0 commit comments

Comments
 (0)