A @Volatile property is updated with ++ or +=, which is not atomic
What it finds
@Volatile var count = 0 makes each read and write visible to other threads, but count++ reads and then writes, and two threads can both read the old value. Updates are lost without any error. An atomic type or a lock makes the update atomic.
From the rule's source, volatile_compound_update.rs.
class Counter { @Volatile private var count = 0 @Volatile private var running = false private var plain = 0 fun hit() { count++tx0 reports: the update of the volatile count is not atomic this.count += 2tx0 reports: the update of the volatile count is not atomic count = 0 running = true plain++ }} 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:
[rules]
"tx0/kotlin:volatile-compound-update" = "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/kotlin:volatile-compound-update] reason="..."tx0 explain tx0/kotlin:volatile-compound-update shows the same in the terminal. Found a false positive? Report it.