Kotlin provides the operators as
and as?
to cast an expression to a specific type, and is
to check the
assignment compatibility with a type. These operators are used for downcasts, smart casts, and run-time type checking.
In case the as
or as?
operator is used for casting between incompatible types, that is, distinct types and neither being
a subtype of the other, the cast will never succeed but always throw a ClassCastException
. This results in dead code and is likely a
symptom of wrong program logic.
Likewise, the is
operator is redundant and will never return true
if the type of the expression on the left side is
incompatible with the type on the right.
What is the potential impact?
Code redundancy
Since the operation will never succeed, it is pointless to use it. It also leads to dead code branches because as
will always break
the regular control flow with an exception, while conditions with is
will never or always be satisfied.
Wrong logic
Type casts and type checks that can never succeed are likely a symptom of wrong program logic. Developers will not have intended the redundancy of
the type check or type cast, but it might result from an error elsewhere.