diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/ReturnValueIgnored.java b/core/src/main/java/com/google/errorprone/bugpatterns/ReturnValueIgnored.java index 19dcf80a9a3..09f3257b353 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/ReturnValueIgnored.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/ReturnValueIgnored.java @@ -30,6 +30,7 @@ import com.google.common.collect.ImmutableSet; import com.google.errorprone.BugPattern; +import com.google.errorprone.ErrorProneFlags; import com.google.errorprone.VisitorState; import com.google.errorprone.matchers.Matcher; import com.google.errorprone.util.ASTHelpers; @@ -150,9 +151,7 @@ private static boolean functionalMethod(ExpressionTree tree, VisitorState state) * void-returning ones, which won't be checked by AbstractReturnValueIgnored). */ private static final Matcher STRING_METHODS = - anyOf( - staticMethod().onClass("java.lang.String"), - instanceMethod().onExactClass("java.lang.String")); + anyMethod().onClass("java.lang.String"); private static final ImmutableSet PRIMITIVE_TYPES = ImmutableSet.of( @@ -209,6 +208,12 @@ private static boolean functionalMethod(ExpressionTree tree, VisitorState state) instanceMethod().onExactClass("java.util.Optional").named("isPresent"), instanceMethod().onExactClass("java.util.Optional").named("isEmpty")); + /** + * The return values of {@link java.util.concurrent.TimeUnit} methods should always be checked. + */ + private static final Matcher TIME_UNIT_METHODS = + anyMethod().onClass("java.util.concurrent.TimeUnit"); + private static final String PROTO_MESSAGE = "com.google.protobuf.MessageLite"; /** @@ -246,8 +251,16 @@ private static boolean functionalMethod(ExpressionTree tree, VisitorState state) .namedAnyOf("containsKey", "containsValue") .withParameters("java.lang.Object")); + private final Matcher matcher; + + public ReturnValueIgnored(ErrorProneFlags flags) { + boolean checkTimeUnit = flags.getBoolean("ReturnValueIgnored:TimeUnit").orElse(true); + this.matcher = + checkTimeUnit ? anyOf(SPECIALIZED_MATCHER, TIME_UNIT_METHODS) : SPECIALIZED_MATCHER; + } + @Override public Matcher specializedMatcher() { - return SPECIALIZED_MATCHER; + return matcher; } } diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/ReturnValueIgnoredTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/ReturnValueIgnoredTest.java index 42294cf5940..43eeadcbb0d 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/ReturnValueIgnoredTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/ReturnValueIgnoredTest.java @@ -177,6 +177,22 @@ public void optionalInstanceMethods() { .doTest(); } + @Test + public void timeUnitApis() { + compilationHelper + .addSourceLines( + "Test.java", + "import static java.util.concurrent.TimeUnit.MILLISECONDS;", + "class Test {", + " void timeUnit() {", + " long ms = 4200;", + " // BUG: Diagnostic contains: ReturnValueIgnored", + " MILLISECONDS.toNanos(ms);", + " }", + "}") + .doTest(); + } + @Test public void issue1565_enumDeclaration() { compilationHelper