Duration.withNanos()
may not produce correct results JAVA-E1087Using Duration.withNanos()
may produce wrong results, because it will only set the value of the nanoseconds field of
the duration, and will not correctly adjust for any overflow.
Duration d = Duration.of(2, ChronoUnit.SECONDS);
// Any overflow from nanos to seconds will not be handled!
d = d.withNanos(extraNanoseconds);
Use the two-argument overload
of Duration.ofSeconds()
instead.
Duration d = Duration.ofSeconds(2, extraNanoseconds);
This rule will respect suppress annotations like @SuppressWarnings("JavaDurationWithNanos")
applied to the enclosing
block or declaration.