A when has a single branch, which reads more simply as an if
What it finds
when (x) { is Done -> finish() } with one branch, or with only an else, is an if written at length. Reported for when with one entry besides an optional else, used as a statement.
From the rule's source, single_branch_when.rs.
fun handle(event: Any) { when (event) {tx0 reports: this when has a single branch is String -> println(event) } when (event) { is String -> println(event) else -> println("other") } val size = when (event) { is String -> event.length else -> 0 }} 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:single-branch-when" = "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:single-branch-when] reason="..."tx0 explain tx0/kotlin:single-branch-when shows the same in the terminal. Found a false positive? Report it.