rspec/rules/S1871/kotlin/exceptions.adoc
2023-10-24 12:02:02 +00:00

26 lines
743 B
Plaintext

=== Exceptions
Blocks in an `if` chain or in a `when` branch that contain a single line of code are ignored.
[source,kotlin]
----
if (a == 1) {
doSomething() //no issue, usually this is done on purpose to increase the readability
} else if (a == 2) {
doSomethingElse()
} else {
doSomething()
}
----
But this exception does not apply to `if` chains without `else`-s, or to `when`-es without `else` clauses when all branches have the same single line of code. In case of `if` chains with `else`-s, or of `when`-es with default clauses, rule S3923 raises a bug.
[source,kotlin]
----
if (a == 1) {
doSomething() // Noncompliant, this might have been done on purpose but probably not
} else if (a == 2) {
doSomething()
}
----