dimsuz
11/11/2022, 2:34 PMwhen
on enums, e.g.
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?Chris Lee
11/11/2022, 2:37 PMephemient
11/11/2022, 3:12 PMDone. Warning in 1.6.0, error starting with 1.7.0.
dimsuz
11/11/2022, 3:50 PMwhen
, but I'm thinking about suggesting converting if
-s on enums to when
-s, so that the check you've mentioned would kick indimsuz
11/11/2022, 3:53 PMif (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.ephemient
11/11/2022, 4:16 PMdimsuz
11/11/2022, 4:28 PMBrais Gabin
11/11/2022, 10:49 PMJavier
11/11/2022, 10:51 PMdimsuz
11/11/2022, 11:10 PMI think it shouldn’t be a default enabled ruleYes, I agree. Not everyone would like it. I find this style helpful in larger/spaggetized codebases, but it's not for every project/team.