diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/MultipleUnaryOperatorsInMethodCall.java b/core/src/main/java/com/google/errorprone/bugpatterns/MultipleUnaryOperatorsInMethodCall.java index 2b40f635c5a..767bc474324 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/MultipleUnaryOperatorsInMethodCall.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/MultipleUnaryOperatorsInMethodCall.java @@ -16,8 +16,11 @@ package com.google.errorprone.bugpatterns; import static com.google.errorprone.BugPattern.SeverityLevel.WARNING; +import static com.google.errorprone.matchers.Description.NO_MATCH; +import static java.util.stream.Collectors.groupingBy; import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Sets; import com.google.errorprone.BugPattern; import com.google.errorprone.VisitorState; import com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher; @@ -38,7 +41,7 @@ public class MultipleUnaryOperatorsInMethodCall extends BugChecker implements MethodInvocationTreeMatcher { private static final ImmutableSet UNARY_OPERATORS = - ImmutableSet.of( + Sets.immutableEnumSet( Kind.POSTFIX_DECREMENT, Kind.POSTFIX_INCREMENT, Kind.PREFIX_DECREMENT, @@ -47,16 +50,16 @@ public class MultipleUnaryOperatorsInMethodCall extends BugChecker @Override public Description matchMethodInvocation( MethodInvocationTree methodInvocationTree, VisitorState visitorState) { - if (methodInvocationTree.getArguments().stream() .filter(arg -> UNARY_OPERATORS.contains(arg.getKind())) .map(arg -> ASTHelpers.getSymbol(((UnaryTree) arg).getExpression())) - .collect(Collectors.groupingBy(Function.identity(), Collectors.counting())) + .filter(sym -> sym != null) + .collect(groupingBy(Function.identity(), Collectors.counting())) .entrySet() .stream() .anyMatch(e -> e.getValue() > 1)) { return describeMatch(methodInvocationTree); } - return Description.NO_MATCH; + return NO_MATCH; } } diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/testdata/MultipleUnaryOperatorsInMethodCallNegativeCases.java b/core/src/test/java/com/google/errorprone/bugpatterns/testdata/MultipleUnaryOperatorsInMethodCallNegativeCases.java index 867409e89ef..f880b710eaa 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/testdata/MultipleUnaryOperatorsInMethodCallNegativeCases.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/testdata/MultipleUnaryOperatorsInMethodCallNegativeCases.java @@ -17,7 +17,7 @@ /** @author sulku@google.com (Marsela Sulku) */ public class MultipleUnaryOperatorsInMethodCallNegativeCases { - public static void tests(int a, int b) { + public static void tests(int a, int b, int[] xs) { testMethod(a, b); testMethod(a + 1, b); testMethod(b, a + 1); @@ -25,6 +25,7 @@ public static void tests(int a, int b) { testMethod(--a, b); testMethod(a, b--); testMethod(a, ++b); + testMethod(xs[0]++, xs[0]++); } public static void testMethod(int one, int two) {}