Hi! I'm thinking about writing a rule which would ...
# detekt
d
Hi! I'm thinking about writing a rule which would nudge the programmer to use exhaustive
when
on enums, e.g.
Copy code
enum Variant { One, Two, Three }

// non-compliant
if (variant == Variant.One) 
  doStuff() else doOtherStuff()

// compliant
when (variant) {
  One -> doStuff()
  Two, Three -> doOtherStuff()
}
with rationale being that in the latter case if
Four
would be added it's better to have the compiler tell you that you should think about how you would handle it in every place you depend on this enum. But first I wanted to ask: is there something like this in Detekt's rule set already?
c
You may wish to review this and follow up on the current status (vis-a-vis Kotlin version).
e
as https://youtrack.jetbrains.com/issue/KT-12380 says,
Done. Warning in 1.6.0, error starting with 1.7.0.
d
yeah, those are about
when
, but I'm thinking about suggesting converting
if
-s on enums to
when
-s, so that the check you've mentioned would kick in
as of now, if I have
Copy code
if (variant == Variant.One)
nothing will tell me that I'm non-exhaustively swiping all other options under
else
, especially when more and more branches are added to the
Variant
enum.
d
thank you!
b
And, answering the question, no, a rule like that doesn't exist on the Detekt default rule set.
j
I think it shouldn’t be a default enabled rule
d
I think it shouldn’t be a default enabled rule
Yes, I agree. Not everyone would like it. I find this style helpful in larger/spaggetized codebases, but it's not for every project/team.