So, after upgrading to kotlin 1.7.10, the exhaustive when clause has kicked in. This has caused us to have to add a lot of this type of block to our code in cases where we’re only interested in one or two cases of an enum:
Copy code
SOME_VALUE_WE_DONT_CARE_ABOUT -> { }
The issue is that sonarqube is treating this as a code smell, and our quality gate is blocking merges when this happens. So we have to either change our quality gate, or use admin privs to merge. Does anyone know if there’s a way to tell sonar to ignore these?
k
Klitos Kyriacou
09/21/2022, 1:38 PM
Isn't Sonar happy if you just put
else -> {}
?
k
kenkyee
09/21/2022, 2:18 PM
else ->
{ /*intentionally skipped* / }
m
mattinger
09/21/2022, 2:28 PM
@Klitos Kyriacou As an org, we don’t want an else block like that. The problem is that as new enum values are added, you are not forced to make a conscious decision whether you need to handle it or not. That was my original inclination to do that but after conferring with some of the other devs, we decided to be specific and use the “add missing cases” resolution in AS instead.
👍 1
m
Michael de Kaste
09/21/2022, 2:35 PM
unless the enums/sealeds are enormous, we manually put in all the other values
so if an enum consists of 5 values:
enum class Count{ ONE, TWO, THREE, FOUR, FIVE }
and we only do something on even values:
Copy code
when(count){
TWO, FOUR -> doThing()
ONE, THREE, FIVE -> { }
}
when we add
SIX
as a value, we get compile errors
Michael de Kaste
09/21/2022, 2:36 PM
I guess thats the consensus for making your code future safe