I see where you are coming from. I think it is one of these cases where we’re around the edges of where this rule still applies, however other issues may also apply, depending on how you write it. I also agree completely with you that we should consider raising issues in some of the other examples you provided. There are many rules that are not implemented for the Kotlin analyzer yet, so this will hopefully improve with time. You are right that the false
being a constant alone isn’t a sufficient justification for why this rule triggers.
As to why this rule triggers, I think we can nevertheless make an argument for a true positive here. The expression if (x is IllegalStateException) false else throw x
does two different things: it jumps and it produces a value. So it is effectively the same as:
if (x !is IllegalStateException) throw x
val result = if (x is IllegalStateException) false else true // else case added, as we can't compile otherwise
Now let’s ignore the first if statement for a second. It becomes a bit clearer as to why the false
here is redundant. We should instead write it as:
val result = x !is IllegalStateException
In fact this is a lot more readable as to what result
actually represents, which is that x
is something other than an IllegalStateException
. So this is the ‘value’ part of the if-else you originally posted, which does, in fact, contain the redundant boolean literal false
and it applies to the the original if-else statement too, even if it is more complex with the throw
thrown in there.
How we want to handle the throw x
is another question, which isn’t really what this rule is about. We could for example do
if (result) {
throw x
} else {
print(result)
}
or some of the things discussed earlier, however, it’s outside the scope of this particular rule.
And just to be clear, I think it’s very valuable to discuss details like this. It helps improve the rules and makes them more valuable for everyone using the plugin, so thanks for bringing it up, one way or the other
.