diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/time/TimeUnitConversionChecker.java b/core/src/main/java/com/google/errorprone/bugpatterns/time/TimeUnitConversionChecker.java index 142beb2a8303..5be093299405 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/time/TimeUnitConversionChecker.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/time/TimeUnitConversionChecker.java @@ -32,6 +32,7 @@ import com.sun.source.tree.ExpressionTree; import com.sun.source.tree.MethodInvocationTree; import com.sun.source.tree.Tree; +import com.sun.tools.javac.code.Symbol; import java.util.concurrent.TimeUnit; /** Check for problematic or suspicious TimeUnit conversion calls. */ @@ -73,7 +74,11 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState // TimeUnit SECONDS = TimeUnit.MINUTES; // long about2500 = SECONDS.toSeconds(42); // but... I think that's bad enough to ignore here :) - String timeUnitName = ASTHelpers.getSymbol(receiverOfConversion).getSimpleName().toString(); + Symbol receiverOfConversionSymbol = ASTHelpers.getSymbol(receiverOfConversion); + if (receiverOfConversionSymbol == null) { + return Description.NO_MATCH; + } + String timeUnitName = receiverOfConversionSymbol.getSimpleName().toString(); Optional receiver = Enums.getIfPresent(TimeUnit.class, timeUnitName); if (!receiver.isPresent()) { return Description.NO_MATCH; diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/time/TimeUnitConversionCheckerTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/time/TimeUnitConversionCheckerTest.java index d928fd12a6d6..3364c04bd29d 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/time/TimeUnitConversionCheckerTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/time/TimeUnitConversionCheckerTest.java @@ -168,4 +168,18 @@ public void largeUnitToSmallUnit_succeeds() { "}") .doTest(); } + + @Test + public void receiverExpression() { + helper + .addSourceLines( + "TestClass.java", + "import java.util.concurrent.TimeUnit;", + "public class TestClass {", + " long f(TimeUnit timeUnit, long time) {", + " return (timeUnit != null ? timeUnit : TimeUnit.MILLISECONDS).toMillis(time);", + " }", + "}") + .doTest(); + } }