An amount converted to one time unit is passed together with another TimeUnit
What it finds
executor.schedule(task, timeout.toMillis(), TimeUnit.SECONDS) waits a thousand times longer than meant: the amount is converted to milliseconds and then read as seconds. Methods that take an amount and a TimeUnit read the amount in that unit, as the java.util.concurrent documentation says.
Reported is an argument d.toMillis(), toNanos(), toSeconds(), getSeconds(), toMinutes(), toHours() or toDays() followed directly by a TimeUnit constant of another unit.
From the rule's source, duration_unit_mismatch.rs.
import java.time.Duration;import java.util.concurrent.ScheduledExecutorService;import java.util.concurrent.TimeUnit; class Timer { void plan(ScheduledExecutorService executor, Runnable task, Duration delay) throws Exception { executor.schedule(task, delay.toMillis(), TimeUnit.SECONDS);tx0 reports: comes from toMillis() but is read as TimeUnit.SECONDS executor.schedule(task, delay.toMillis(), TimeUnit.MILLISECONDS); executor.awaitTermination(delay.getSeconds(), TimeUnit.SECONDS); executor.awaitTermination(delay.toNanos(), TimeUnit.MICROSECONDS);tx0 reports: read as TimeUnit.MICROSECONDS }} Turn it on, off or suppress it
The rule runs in every tier. To turn it off for a project, set its level in tx0.toml:
[rules]
"tx0/java:duration-unit-mismatch" = "off"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/java:duration-unit-mismatch] reason="..."tx0 explain tx0/java:duration-unit-mismatch shows the same in the terminal. Found a false positive? Report it.