synchronized locks a literal, an object that unrelated code may lock too

tx0/kotlin:lock-on-literaldefaultwarningreliability/concurrencydepth fastbug, concurrency

What it finds

String literals are interned and small boxed numbers are cached, so synchronized("cache") { } and synchronized(1) { } lock objects that any other code using the same literal locks as well. A private Any() makes a lock no one else has.

From the rule's source, synchronized_on_literal.rs.

Example from the rule's tests
private val lock = Any() fun update() {    synchronized("cache") { work() }tx0 reports: is shared with all code that uses the same literal    synchronized(1) { work() }tx0 reports: 1 is shared with all code    synchronized(lock) { work() }}
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 in every tier. To turn it off for a project, set its level in tx0.toml:

tx0.toml
[rules]
"tx0/kotlin:lock-on-literal" = "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/kotlin:lock-on-literal] reason="..."

tx0 explain tx0/kotlin:lock-on-literal shows the same in the terminal. Found a false positive? Report it.