CAST_NEVER_SUCCEEDS: do not report when casting nullable to nullable #KT-260 Fixed
This commit is contained in:
@@ -37,9 +37,12 @@ object CastDiagnosticsUtil {
|
||||
lhsType: KotlinType,
|
||||
rhsType: KotlinType,
|
||||
platformToKotlinClassMap: PlatformToKotlinClassMap): Boolean {
|
||||
if (KotlinBuiltIns.isNullableNothing(lhsType) && !TypeUtils.isNullableType(rhsType)) return false
|
||||
val rhsNullable = TypeUtils.isNullableType(rhsType)
|
||||
val lhsNullable = TypeUtils.isNullableType(lhsType)
|
||||
if (KotlinBuiltIns.isNullableNothing(lhsType) && !rhsNullable) return false
|
||||
if (KotlinBuiltIns.isNothing(rhsType)) return false
|
||||
if (KotlinBuiltIns.isNullableNothing(rhsType)) return TypeUtils.isNullableType(lhsType)
|
||||
if (KotlinBuiltIns.isNullableNothing(rhsType)) return lhsNullable
|
||||
if (lhsNullable && rhsNullable) return true
|
||||
if (lhsType.isError) return true
|
||||
if (isRelated(lhsType, rhsType, platformToKotlinClassMap)) return true
|
||||
// This is an oversimplification (which does not render the method incomplete):
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
// From KT-13324: always succeeds
|
||||
val x = null <!USELESS_CAST!>as String?<!>
|
||||
// From KT-260: sometimes succeeds
|
||||
fun foo(a: String?): Int? {
|
||||
val c = a as? Int?
|
||||
return c
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package
|
||||
|
||||
public val x: kotlin.String? = null
|
||||
public fun foo(/*0*/ a: kotlin.String?): kotlin.Int?
|
||||
@@ -2673,6 +2673,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("NullableToNullable.kt")
|
||||
public void testNullableToNullable() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/cast/NullableToNullable.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("WhenErasedDisallowFromAny.kt")
|
||||
public void testWhenErasedDisallowFromAny() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/cast/WhenErasedDisallowFromAny.kt");
|
||||
|
||||
Reference in New Issue
Block a user