-
What language is this for?
Kotlin -
Which rule?
kotlin:S108 “nested block of code should not be left empty” -
Why do you believe it’s a false-positive/false-negative?
“when” statements are sometimes required to be exhaustive, for instance when the subject is an enum, even if only certain cases should actually trigger any code.
This is commonly and cleanly solved using an “else” clause with an empty body block as a catch-all.Sonar, however, flags the block with “Either remove or fill this block of code.”
This is misguided:- Without the block (or the else) the code will not compile.
- There is nothing sensible to fill it with (and no, comments or dummy code to appease a misguided tool do not constitute sensible content)
It is possible to use “else → Unit” in stead of “else → {}” to work around this flag, but it is far from clear that this is in any way better or more readable code. “{}” communicates “do nothing” very clearly. And this is in any case not what the warning is advocating.
-
Are you using
- SonarLint 8.0.0.63273 in IntelliJ IDEA 2022.3.2
- connected to SonarQube Community EditionVersion 9.3
- SonarLint 8.0.0.63273 in IntelliJ IDEA 2022.3.2
-
How can we reproduce the problem? Give us a self-contained snippet of code (formatted text, no screenshots)
when(LocalDate.now().dayOfWeek){
SATURDAY, SUNDAY -> println("No work!")
else -> {}
}