Skip to content

Commit

Permalink
Fix an NPE in MultipleUnaryOperatorsInMethodCall
Browse files Browse the repository at this point in the history
Discovered while validating 2.4.0 release (#1639)

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=313711584
  • Loading branch information
cushon committed May 29, 2020
1 parent d216dc1 commit 1e2c8ac
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
Expand Up @@ -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;
Expand All @@ -38,7 +41,7 @@ public class MultipleUnaryOperatorsInMethodCall extends BugChecker
implements MethodInvocationTreeMatcher {

private static final ImmutableSet<Kind> UNARY_OPERATORS =
ImmutableSet.of(
Sets.immutableEnumSet(
Kind.POSTFIX_DECREMENT,
Kind.POSTFIX_INCREMENT,
Kind.PREFIX_DECREMENT,
Expand All @@ -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;
}
}
Expand Up @@ -17,14 +17,15 @@

/** @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);
testMethod(a++, b);
testMethod(--a, b);
testMethod(a, b--);
testMethod(a, ++b);
testMethod(xs[0]++, xs[0]++);
}

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

0 comments on commit 1e2c8ac

Please sign in to comment.