synchronized locks a literal, an object that unrelated code may lock too
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.
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() }} 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/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.