diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/time/PreferJavaTimeOverload.java b/core/src/main/java/com/google/errorprone/bugpatterns/time/PreferJavaTimeOverload.java index a02e6030b66..aecfab273e2 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/time/PreferJavaTimeOverload.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/time/PreferJavaTimeOverload.java @@ -121,7 +121,8 @@ public final class PreferJavaTimeOverload extends BugChecker private static final Matcher JAVA_DURATION_DECOMPOSITION_MATCHER = instanceMethod() .onExactClass(JAVA_DURATION) - .namedAnyOf("toNanos", "toMillis", "getSeconds", "toMinutes", "toHours", "toDays"); + .namedAnyOf( + "toNanos", "toMillis", "toSeconds", "toMinutes", "toHours", "toDays", "getSeconds"); // TODO(kak): Add support for constructors that accept a or JodaTime Duration diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/time/PreferJavaTimeOverloadTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/time/PreferJavaTimeOverloadTest.java index 2d7a3c3d7c3..3c574748bca 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/time/PreferJavaTimeOverloadTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/time/PreferJavaTimeOverloadTest.java @@ -66,6 +66,27 @@ public CacheBuilder foo(CacheBuilder builder) { @Test public void callingLongTimeUnitMethodWithDurationOverload_durationDecompose() { + helper + .addSourceLines( + "TestClass.java", + """ + import com.google.common.cache.CacheBuilder; + import java.time.Duration; + import java.util.concurrent.TimeUnit; + + public class TestClass { + public CacheBuilder foo(CacheBuilder builder) { + Duration duration = Duration.ofMillis(12345); + // BUG: Diagnostic contains: builder.expireAfterAccess(duration); + return builder.expireAfterAccess(duration.toSeconds(), TimeUnit.SECONDS); + } + } + """) + .doTest(); + } + + @Test + public void callingLongTimeUnitMethodWithDurationOverload_durationDecompose_getSeconds() { helper .addSourceLines( "TestClass.java",