Two branches of a when have the same body, so their conditions could be one branch
What it finds
When two branches of a when do exactly the same, the conditions can be listed in one branch separated by commas. A copy that was meant to differ is a bug. Bodies that are a single literal or name, as in a mapping table, are left out.
From the rule's source, duplicate_when_body.rs.
fun handle(code: Int) { when (code) { 1 -> { log("start") start() } 2 -> stop() 3 -> {tx0 reports: this branch does the same as an earlier one log("start") start() } 4 -> stop() else -> {} }} Turn it on, off or suppress it
The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/kotlin:duplicate-when-branch" = "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:duplicate-when-branch] reason="..."tx0 explain tx0/kotlin:duplicate-when-branch shows the same in the terminal. Found a false positive? Report it.