Skip to content

Commit

Permalink
Add TimeUnit APIs to ReturnValueIgnored.
Browse files Browse the repository at this point in the history
#goodtime

PiperOrigin-RevId: 366095590
  • Loading branch information
kluever authored and Error Prone Team committed Mar 31, 2021
1 parent c5e1687 commit 86f8e23
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ExpressionTree> STRING_METHODS =
anyOf(
staticMethod().onClass("java.lang.String"),
instanceMethod().onExactClass("java.lang.String"));
anyMethod().onClass("java.lang.String");

private static final ImmutableSet<String> PRIMITIVE_TYPES =
ImmutableSet.of(
Expand Down Expand Up @@ -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<ExpressionTree> TIME_UNIT_METHODS =
anyMethod().onClass("java.util.concurrent.TimeUnit");

private static final String PROTO_MESSAGE = "com.google.protobuf.MessageLite";

/**
Expand Down Expand Up @@ -246,8 +251,16 @@ private static boolean functionalMethod(ExpressionTree tree, VisitorState state)
.namedAnyOf("containsKey", "containsValue")
.withParameters("java.lang.Object"));

private final Matcher<? super ExpressionTree> 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<? super ExpressionTree> specializedMatcher() {
return SPECIALIZED_MATCHER;
return matcher;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 86f8e23

Please sign in to comment.