diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/TimeZoneUsage.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/TimeZoneUsage.java index 1bda1d207b..c4426261ff 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/TimeZoneUsage.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/TimeZoneUsage.java @@ -26,6 +26,9 @@ import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; +import java.time.OffsetDateTime; +import java.time.OffsetTime; +import java.time.ZonedDateTime; /** A {@link BugChecker} that flags illegal time-zone related operations. */ @AutoService(BugChecker.class) @@ -58,7 +61,10 @@ public final class TimeZoneUsage extends BugChecker implements MethodInvocationT .onClassAny( LocalDate.class.getName(), LocalDateTime.class.getName(), - LocalTime.class.getName()) + LocalTime.class.getName(), + OffsetDateTime.class.getName(), + OffsetTime.class.getName(), + ZonedDateTime.class.getName()) .named("now"), staticMethod().onClassAny(Instant.class.getName()).named("now").withNoParameters()); diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/TimeZoneUsageTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/TimeZoneUsageTest.java index b675225b92..ad4e8a8b07 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/TimeZoneUsageTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/TimeZoneUsageTest.java @@ -1,17 +1,11 @@ package tech.picnic.errorprone.bugpatterns; -import static com.google.common.base.Predicates.containsPattern; - import com.google.errorprone.CompilationTestHelper; import org.junit.jupiter.api.Test; final class TimeZoneUsageTest { private final CompilationTestHelper compilationHelper = - CompilationTestHelper.newInstance(TimeZoneUsage.class, getClass()) - .expectErrorMessage( - "X", - containsPattern( - "Derive the current time from an existing `Clock` Spring bean, and don't rely on a `Clock`'s time zone")); + CompilationTestHelper.newInstance(TimeZoneUsage.class, getClass()); @Test void identification() { @@ -26,7 +20,10 @@ void identification() { "import java.time.LocalDate;", "import java.time.LocalDateTime;", "import java.time.LocalTime;", + "import java.time.OffsetDateTime;", + "import java.time.OffsetTime;", "import java.time.ZoneId;", + "import java.time.ZonedDateTime;", "", "class A {", " void m() {", @@ -36,48 +33,69 @@ void identification() { " Clock.offset(clock, Duration.ZERO);", " Clock.tick(clock, Duration.ZERO);", "", - " // BUG: Diagnostic matches: X", + " // BUG: Diagnostic contains:", " Clock.systemUTC();", - " // BUG: Diagnostic matches: X", + " // BUG: Diagnostic contains:", " Clock.systemDefaultZone();", - " // BUG: Diagnostic matches: X", + " // BUG: Diagnostic contains:", " Clock.system(UTC);", - " // BUG: Diagnostic matches: X", + " // BUG: Diagnostic contains:", " Clock.tickMillis(UTC);", - " // BUG: Diagnostic matches: X", + " // BUG: Diagnostic contains:", " Clock.tickMinutes(UTC);", - " // BUG: Diagnostic matches: X", + " // BUG: Diagnostic contains:", " Clock.tickSeconds(UTC);", - " // BUG: Diagnostic matches: X", + " // BUG: Diagnostic contains:", " clock.getZone();", - " // BUG: Diagnostic matches: X", + " // BUG: Diagnostic contains:", " clock.withZone(UTC);", "", - " // BUG: Diagnostic matches: X", + " // BUG: Diagnostic contains:", " Instant.now();", " // This is equivalent to `clock.instant()`, which is fine.", " Instant.now(clock);", "", - " // BUG: Diagnostic matches: X", + " // BUG: Diagnostic contains:", " LocalDate.now();", - " // BUG: Diagnostic matches: X", + " // BUG: Diagnostic contains:", " LocalDate.now(clock);", - " // BUG: Diagnostic matches: X", + " // BUG: Diagnostic contains:", " LocalDate.now(UTC);", "", - " // BUG: Diagnostic matches: X", + " // BUG: Diagnostic contains:", " LocalDateTime.now();", - " // BUG: Diagnostic matches: X", + " // BUG: Diagnostic contains:", " LocalDateTime.now(clock);", - " // BUG: Diagnostic matches: X", + " // BUG: Diagnostic contains:", " LocalDateTime.now(UTC);", "", - " // BUG: Diagnostic matches: X", + " // BUG: Diagnostic contains:", " LocalTime.now();", - " // BUG: Diagnostic matches: X", + " // BUG: Diagnostic contains:", " LocalTime.now(clock);", - " // BUG: Diagnostic matches: X", + " // BUG: Diagnostic contains:", " LocalTime.now(UTC);", + "", + " // BUG: Diagnostic contains:", + " OffsetDateTime.now();", + " // BUG: Diagnostic contains:", + " OffsetDateTime.now(clock);", + " // BUG: Diagnostic contains:", + " OffsetDateTime.now(UTC);", + "", + " // BUG: Diagnostic contains:", + " OffsetTime.now();", + " // BUG: Diagnostic contains:", + " OffsetTime.now(clock);", + " // BUG: Diagnostic contains:", + " OffsetTime.now(UTC);", + "", + " // BUG: Diagnostic contains:", + " ZonedDateTime.now();", + " // BUG: Diagnostic contains:", + " ZonedDateTime.now(clock);", + " // BUG: Diagnostic contains:", + " ZonedDateTime.now(UTC);", " }", "", " abstract class ForwardingClock extends Clock {",