A static field holds a formatter, calendar, digest or matcher, which is not thread-safe

tx0/java:shared-unsafe-instancerecommendedwarningbug, concurrency

What it finds

A static field holding a SimpleDateFormat, a Calendar, a MessageDigest or a regex Matcher shares an object that keeps state while it works between all threads. Two threads formatting at once corrupt each other's result, often silently. A local instance, a ThreadLocal, or a thread-safe class such as DateTimeFormatter avoids it.

From the rule's source, shared_unsafe_instance.rs.

Example from the rule's tests
import java.text.SimpleDateFormat;import java.time.format.DateTimeFormatter; class Dates {  private static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd");tx0 reports: the SimpleDateFormat in this static field is shared between threads  private static final DateTimeFormatter SAFE = DateTimeFormatter.ISO_DATE;  private final SimpleDateFormat local = new SimpleDateFormat("yyyy");  private static java.security.MessageDigest digest;tx0 reports: the MessageDigest in this static field}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/java:shared-unsafe-instance" = "warning"

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:shared-unsafe-instance] reason="..."

tx0 explain tx0/java:shared-unsafe-instance shows the same in the terminal. Found a false positive? Report it.