ReadObject is synchronized, although no other thread can see the object it reads

tx0/java:synchronized-read-objectrecommendedwarningsmell

What it finds

While readObject runs, the object being read is not yet visible to any other thread, so locking it guards nothing and only costs time. If other state needs guarding, a lock on that state says so.

Reported is a synchronized method readObject with one parameter.

From the rule's source, synchronized_read_object.rs.

Example from the rule's tests
import java.io.ObjectInputStream;import java.io.Serializable; class State implements Serializable {  private synchronized void readObject(ObjectInputStream in) throws Exception {tx0 reports: no other thread can see the object    in.defaultReadObject();  }}
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:synchronized-read-object" = "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:synchronized-read-object] reason="..."

tx0 explain tx0/java:synchronized-read-object shows the same in the terminal. Found a false positive? Report it.