rspec/rules/S131/kotlin/rule.adoc

62 lines
970 B
Plaintext
Raw Normal View History

== Why is this an issue?
2020-06-30 12:47:33 +02:00
include::../description.adoc[]
=== Noncompliant code example
2020-06-30 12:47:33 +02:00
2022-02-04 17:28:24 +01:00
[source,kotlin]
2020-06-30 12:47:33 +02:00
----
when(param) { // missing else
1 -> doSomething()
2 -> doSomethingElse()
}
----
=== Compliant solution
2020-06-30 12:47:33 +02:00
2022-02-04 17:28:24 +01:00
[source,kotlin]
2020-06-30 12:47:33 +02:00
----
when(param) {
1 -> doSomething()
2 -> doSomethingElse()
else -> error("myMessage")
}
----
=== Exceptions
2020-06-30 12:47:33 +02:00
2021-01-27 13:42:22 +01:00
If the ``++when++`` clause is used as an expression, Kotlin compiler already enforces that all possible cases are covered with branch conditions. So no issue will be raised in this case.
2020-06-30 12:47:33 +02:00
2021-02-02 15:02:10 +01:00
2020-06-30 12:47:33 +02:00
Example:
[source,kotlin]
2020-06-30 12:47:33 +02:00
----
enum class Day {
SUNDAY, MONDAY
}
...
val myVal = when(day) {
MyEnum.SUNDAY -> 1
MyEnum.MONDAY -> 2
}
----
include::../see.adoc[]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view[]