You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In Kotlin we can cast in a way that is "checked" or "unchecked". By "checked", we mean here that the cast is checked at runtime. Whatever type we tell the compiler to check as, at runtime it will be asserted that the reference is of that type. "unchecked" means that the compiler will assume that the reference is of the requested type, but will not check (or will not fully check) at runtime.
Example of a checked cast:
list isList<*>
Example of a unchecked cast:
list isList<Int>
The kotlin compiler detects some (but not all) unchecked casts. It emits a warning which can be suppressd with @Suppress("UNCHECKED_CAST"). If -Werror is enabled, compilation will fail on the unchecked casts it detects, but will not fail or even warn on the ones it does not detect.
The unchecked casts that are not detected by the compiler are in inline functions which cast to a reified type.
For example, this program contains an unchecked cast will not be reported by the kotlin compiler.
As a result, it throws a ClassCastException at runtime.
In theory, this rule would enable compilations that make it impossible for user code to throw a ClassCastException (can't prevent library code from throwing it). If we have:
If all of the above are implemented for a compilation, I think it might just logically prevent any ClassCastException from possibly being thrown directly from the code being compiled (of course dependencies can still throw it)
The text was updated successfully, but these errors were encountered:
Expected Behavior of the rule
Report whenever a reference is casted to reified type parameter.
Context
Kotlin has an open issue that the compiler doesn't warn about problematic casts in inline functions with reified generics
The way I worded it in my issue is False negative UNCHECKED_CAST when casting to reified type. My issue was left open but marked as a duplicate of the other one.
I will summarize the problem here.
In Kotlin we can cast in a way that is "checked" or "unchecked". By "checked", we mean here that the cast is checked at runtime. Whatever type we tell the compiler to check as, at runtime it will be asserted that the reference is of that type. "unchecked" means that the compiler will assume that the reference is of the requested type, but will not check (or will not fully check) at runtime.
Example of a checked cast:
Example of a unchecked cast:
The kotlin compiler detects some (but not all) unchecked casts. It emits a warning which can be suppressd with
@Suppress("UNCHECKED_CAST")
. If-Werror
is enabled, compilation will fail on the unchecked casts it detects, but will not fail or even warn on the ones it does not detect.The unchecked casts that are not detected by the compiler are in inline functions which cast to a reified type.
For example, this program contains an unchecked cast will not be reported by the kotlin compiler.
As a result, it throws a
ClassCastException
at runtime.In theory, this rule would enable compilations that make it impossible for user code to throw a ClassCastException (can't prevent library code from throwing it). If we have:
-Werror
enabledForbiddenSuppress
ForbiddenSuppress
forbidden (depends on Allow ForbiddenSuppress to disallow suppressing ForbiddenSuppress #7038)If all of the above are implemented for a compilation, I think it might just logically prevent any ClassCastException from possibly being thrown directly from the code being compiled (of course dependencies can still throw it)
The text was updated successfully, but these errors were encountered: