Fix false warning about check for instance of nullable type

#KT-12269 Fixed
This commit is contained in:
Mikhail Zarechenskiy
2017-04-05 16:37:02 +03:00
parent 0d6b7bb6a1
commit 90a8a164b4
6 changed files with 65 additions and 6 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -433,7 +433,7 @@ class PatternMatchingTypingVisitor internal constructor(facade: ExpressionTyping
context.trace.report(IS_ENUM_ENTRY.on(typeReferenceAfterIs))
}
if (!subjectType.isMarkedNullable && targetType.isMarkedNullable) {
if (!TypeUtils.isNullableType(subjectType) && targetType.isMarkedNullable) {
val element = typeReferenceAfterIs.typeElement
assert(element is KtNullableType) { "element must be instance of " + KtNullableType::class.java.name }
context.trace.report(Errors.USELESS_NULLABLE_CHECK.on(element as KtNullableType))
@@ -4,11 +4,11 @@ fun <T, S : T> test(x: T?, y: S, z: T) {
y is T
y is S
y is T<!USELESS_NULLABLE_CHECK!>?<!>
y is S<!USELESS_NULLABLE_CHECK!>?<!>
y is T?
y is S?
z is T
z is T<!USELESS_NULLABLE_CHECK!>?<!>
z is T?
<!UNCHECKED_CAST!>null as T<!>
null <!USELESS_CAST!>as T?<!>
@@ -0,0 +1,38 @@
// FILE: JavaClass.java
import org.jetbrains.annotations.Nullable
import org.jetbrains.annotations.NotNull
public class JavaClass {
static Integer foo() {
return 1;
}
@Nullable
static Integer fooN() {
return 1;
}
@NotNull
static Integer fooNN() {
return 1;
}
}
// FILE: test.kt
fun <T, S: Any> test(x1: T, x2: T?, y1: S, y2: S?) {
x1 is T?
x2 is T?
y1 is S<!USELESS_NULLABLE_CHECK!>?<!>
y2 is S?
val f1 = JavaClass.foo()
f1 is Int?
val f2 = JavaClass.fooN()
f2 is Int?
val f3 = JavaClass.fooNN()
f3 is Int<!USELESS_NULLABLE_CHECK!>?<!>
}
@@ -0,0 +1,15 @@
package
public fun </*0*/ T, /*1*/ S : kotlin.Any> test(/*0*/ x1: T, /*1*/ x2: T?, /*2*/ y1: S, /*3*/ y2: S?): kotlin.Unit
public open class JavaClass {
public constructor JavaClass()
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
// Static members
public/*package*/ open fun foo(): kotlin.Int!
@org.jetbrains.annotations.Nullable public/*package*/ open fun fooN(): kotlin.Int?
@org.jetbrains.annotations.NotNull public/*package*/ open fun fooNN(): kotlin.Int
}
@@ -1,5 +1,5 @@
fun <T : Any?> foo(x: T) {
if (x is String<!USELESS_NULLABLE_CHECK!>?<!>) {
if (x is String?) {
x<!UNSAFE_CALL!>.<!>length
if (x != null) {
@@ -2710,6 +2710,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("checkCastToNullableType.kt")
public void testCheckCastToNullableType() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/cast/checkCastToNullableType.kt");
doTest(fileName);
}
@TestMetadata("constants.kt")
public void testConstants() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/cast/constants.kt");