Files
Ivan Kochurkin d50c6f1b6d [FIR] Fix missing USELESS_CAST
`FirUselessTypeOperationCallChecker` always checks exact types matching for `as` operator

Simplify code of cast checker and utils

^KT-56629 Fixed
^KT-56615 Fixed
^KT-59820 Fixed
2023-10-24 20:59:56 +00:00

37 lines
928 B
Kotlin
Vendored

// !LANGUAGE: +SafeCastCheckBoundSmartCasts
interface SomeClass {
val data: Any?
}
interface SomeSubClass : SomeClass {
val foo: Any?
}
fun g(a: SomeClass?) {
if (a as? SomeSubClass != null) {
// 'a' can be cast to SomeSubClass
a.hashCode()
a.foo
(a <!USELESS_CAST!>as? SomeSubClass<!>)<!UNSAFE_CALL!>.<!>foo
(a <!USELESS_CAST!>as SomeSubClass<!>).foo
}
val b = (a as? SomeSubClass)?.foo
if (b != null) {
// 'a' can be cast to SomeSubClass
a.hashCode()
a.foo
(a <!USELESS_CAST!>as? SomeSubClass<!>)<!UNSAFE_CALL!>.<!>foo
(a <!USELESS_CAST!>as SomeSubClass<!>).foo
}
val c = a as? SomeSubClass
if (c != null) {
// 'a' and 'c' can be cast to SomeSubClass
a.hashCode()
a.foo
(a <!USELESS_CAST!>as? SomeSubClass<!>)<!UNSAFE_CALL!>.<!>foo
c.hashCode()
c.foo
}
}