FIR checker: SENSELESS_(COMPARISON|NULL_IN_WHEN)

Currently DFA does not set "definitely equal to null" for access to variables that got assigned `null`. For example, FIR should mark the following line as SENSELESS_COMPARISON due to `s = null` above.
 https://github.com/JetBrains/kotlin/blob/d1531f9cdd5852352c0133198706125dc63b6007/compiler/testData/diagnostics/tests/smartCasts/alwaysNull.fir.kt#L6

The problem is at https://github.com/JetBrains/kotlin/blob/7e9f27436a77de1c76e3705da7aa1fbe8938336b/compiler/fir/resolve/src/org/jetbrains/kotlin/fir/resolve/dfa/FirDataFlowAnalyzer.kt#L1104

For null assignment, ideally the type should be `Nothing?`. This is
addressed in a followup commit instead.
This commit is contained in:
Tianyu Geng
2021-07-20 09:46:18 -07:00
committed by teamcityserver
parent 4726dcce40
commit c7272f6986
108 changed files with 1410 additions and 1485 deletions
@@ -6,24 +6,24 @@ fun foo() {
if (x != null) {
bar(x)
if (x != null) {
if (<!SENSELESS_COMPARISON!>x != null<!>) {
bar(x)
if (1 < 2) bar(x)
if (1 > 2) bar(x)
}
if (x == null) {
if (<!SENSELESS_COMPARISON!>x == null<!>) {
bar(x)
}
if (x == null) bar(x) else bar(x)
if (<!SENSELESS_COMPARISON!>x == null<!>) bar(x) else bar(x)
bar(bar(x))
} else if (x == null) {
} else if (<!SENSELESS_COMPARISON!>x == null<!>) {
bar(<!ARGUMENT_TYPE_MISMATCH!>x<!>)
if (x != null) {
if (<!SENSELESS_COMPARISON!>x != null<!>) {
bar(x)
if (x == null) bar(x)
if (x == null) bar(x) else bar(x)
if (<!SENSELESS_COMPARISON!>x == null<!>) bar(x)
if (<!SENSELESS_COMPARISON!>x == null<!>) bar(x) else bar(x)
bar(bar(x) + bar(x))
} else if (x == null) {
} else if (<!SENSELESS_COMPARISON!>x == null<!>) {
bar(<!ARGUMENT_TYPE_MISMATCH!>x<!>)
}
}
@@ -10,7 +10,7 @@ fun foo() {
bar(x)
for (q in a) {
bar(x)
if (x == null) bar(x)
if (<!SENSELESS_COMPARISON!>x == null<!>) bar(x)
}
}
@@ -26,5 +26,5 @@ fun foo() {
if (z != null) bar(z)
bar(<!ARGUMENT_TYPE_MISMATCH!>z<!>)
bar(z!!)
if (z != null) bar(z<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>)
if (<!SENSELESS_COMPARISON!>z != null<!>) bar(z<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>)
}
@@ -9,7 +9,7 @@ fun foo() {
2<!OVERLOAD_RESOLUTION_AMBIGUITY!>+<!><!SYNTAX!><!>
}
else {
if (x == null) return
if (<!SENSELESS_COMPARISON!>x == null<!>) return
2<!OVERLOAD_RESOLUTION_AMBIGUITY!>+<!><!SYNTAX!><!>
}
bar(x)
@@ -8,10 +8,10 @@ fun foo(): Int {
if (x != null) return x
val y: Int? = null
if (y == null) return <!RETURN_TYPE_MISMATCH!>if (y != null) y else y<!>
if (y == null) return <!RETURN_TYPE_MISMATCH!>if (<!SENSELESS_COMPARISON!>y != null<!>) y else y<!>
val z: Int? = null
if (z != null) return if (z == null) z else z
if (z != null) return if (<!SENSELESS_COMPARISON!>z == null<!>) z else z
return <!RETURN_TYPE_MISMATCH!>z<!>
}
@@ -11,7 +11,7 @@ fun foo() {
}
when (x) {
0 -> { if (x == null) return }
0 -> { if (<!SENSELESS_COMPARISON!>x == null<!>) return }
else -> { if (x == null) return }
}
bar(x)
@@ -85,8 +85,8 @@ fun foo5(i: MyInterface, a: Any) {
}
fun foo6(e1: E1?, e2: E2) {
E1.A == null
null == E1.A
<!SENSELESS_COMPARISON!>E1.A == null<!>
<!SENSELESS_COMPARISON!>null == E1.A<!>
e1 == null
null == e1
@@ -95,10 +95,10 @@ fun foo6(e1: E1?, e2: E2) {
<!EQUALITY_NOT_APPLICABLE_WARNING!>e1 == e2<!>
<!EQUALITY_NOT_APPLICABLE_WARNING!>e2 == e1<!>
e2 == null
null == e2
E1.A == null
null == E1.A
<!SENSELESS_COMPARISON!>e2 == null<!>
<!SENSELESS_COMPARISON!>null == e2<!>
<!SENSELESS_COMPARISON!>E1.A == null<!>
<!SENSELESS_COMPARISON!>null == E1.A<!>
}
fun foo7(e1: E1?, e2: E2?) {
@@ -85,8 +85,8 @@ fun foo5(i: MyInterface, a: Any) {
}
fun foo6(e1: E1?, e2: E2) {
E1.A == null
null == E1.A
<!SENSELESS_COMPARISON!>E1.A == null<!>
<!SENSELESS_COMPARISON!>null == E1.A<!>
e1 == null
null == e1
@@ -95,10 +95,10 @@ fun foo6(e1: E1?, e2: E2) {
<!INCOMPATIBLE_ENUM_COMPARISON_ERROR!>e1 == e2<!>
<!INCOMPATIBLE_ENUM_COMPARISON_ERROR!>e2 == e1<!>
e2 == null
null == e2
E1.A == null
null == E1.A
<!SENSELESS_COMPARISON!>e2 == null<!>
<!SENSELESS_COMPARISON!>null == e2<!>
<!SENSELESS_COMPARISON!>E1.A == null<!>
<!SENSELESS_COMPARISON!>null == E1.A<!>
}
fun foo7(e1: E1?, e2: E2?) {
@@ -9,7 +9,7 @@ fun CharSequence.bar4() {}
fun <T : CharSequence?> foo(x: T) {
if (x != null) {
if (x != null) {}
if (<!SENSELESS_COMPARISON!>x != null<!>) {}
x.length
x<!UNNECESSARY_SAFE_CALL!>?.<!>length
@@ -8,7 +8,7 @@ fun CharSequence.bar4() {}
fun <T : String?> T.foo() {
if (this != null) {
if (this != null) {}
if (<!SENSELESS_COMPARISON!>this != null<!>) {}
length
this<!UNNECESSARY_SAFE_CALL!>?.<!>length
@@ -8,7 +8,7 @@ fun <T : CharSequence?> foo(x: T) {
var y1: CharSequence = ""
var y2: String = ""
if (x != null) {
if (x != null) {}
if (<!SENSELESS_COMPARISON!>x != null<!>) {}
y1 = x
y2 = <!ASSIGNMENT_TYPE_MISMATCH!>x<!>
@@ -1,10 +0,0 @@
//KT-2457 Verify error when comparing not null value with null in when
package kt2457
fun foo(i: Int) : Int =
when (i) {
1 -> 1
null -> 1
else -> 1
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
//KT-2457 Verify error when comparing not null value with null in when
package kt2457
@@ -1,14 +0,0 @@
//KT-1680 Warn if non-null variable is compared to null
package kt1680
fun foo() {
val x = 1
if (x != null) {} // <-- need a warning here!
if (x == null) {}
if (null != x) {}
if (null == x) {}
val y : Int? = 1
if (y != null) {}
if (y == null) {}
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
//KT-1680 Warn if non-null variable is compared to null
package kt1680
@@ -13,7 +13,7 @@ fun A?.bar() {
}
fun A.baz() {
if (this == null) {
if (<!SENSELESS_COMPARISON!>this == null<!>) {
return
}
foo()
@@ -1,8 +0,0 @@
//KT-2223 Comparing non-null value with null might produce helpful warning
package kt2223
fun foo() {
val x: Int? = null
if (x == null) return
if (x == null) return
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
//KT-2223 Comparing non-null value with null might produce helpful warning
package kt2223
@@ -1,16 +0,0 @@
// The type checker used to think that T is not null no matter what the upper bound
fun <T, INDIRECT: T> nullableUpperBound(t: T, ind: INDIRECT) {
if (t == null) {} // was a warning
if (t != null) {} // was a warning
if (ind == null) {} // was a warning
if (ind != null) {} // was a warning
}
fun <T: Any, INDIRECT: T> notNullUpperBound(t: T, ind: INDIRECT) {
if (t == null) {} // still a warning
if (t != null) {} // still a warning
if (ind == null) {} // still a warning
if (ind != null) {} // still a warning
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// The type checker used to think that T is not null no matter what the upper bound
fun <T, INDIRECT: T> nullableUpperBound(t: T, ind: INDIRECT) {
@@ -1,17 +0,0 @@
// !LANGUAGE: +NewInference
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE -UNUSED_EXPRESSION
fun <D> makeDefinitelyNotNull(arg: D?): D = TODO()
fun <N : Number?> test(arg: N) {
makeDefinitelyNotNull(arg) <!USELESS_ELVIS!>?: 1<!>
makeDefinitelyNotNull(arg)<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>
makeDefinitelyNotNull(arg)<!UNNECESSARY_SAFE_CALL!>?.<!>toInt()
val nullImposible = when (val dnn = makeDefinitelyNotNull(arg)) {
null -> false
else -> true
}
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !LANGUAGE: +NewInference
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE -UNUSED_EXPRESSION
@@ -1,22 +0,0 @@
// FILE: J.java
import org.jetbrains.annotations.*;
import java.util.*;
public class J {
@Nullable
public List<String> n() { return null; }
}
// FILE: k.kt
fun list(j: J): Any {
val a = j.n()!!
a<!UNNECESSARY_SAFE_CALL!>?.<!>get(0)
if (a == null) {}
a<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>
a.get(0)
return a
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// FILE: J.java
import org.jetbrains.annotations.*;
@@ -24,10 +24,10 @@ fun test() {
val a: Any? = null
if (platformNN != null) {}
if (null != platformNN) {}
if (platformNN == null) {}
if (null == platformNN) {}
if (<!SENSELESS_COMPARISON!>platformNN != null<!>) {}
if (<!SENSELESS_COMPARISON!>null != platformNN<!>) {}
if (<!SENSELESS_COMPARISON!>platformNN == null<!>) {}
if (<!SENSELESS_COMPARISON!>null == platformNN<!>) {}
if (a != null && platformNN != a) {}
@@ -24,10 +24,10 @@ fun test() {
val a: Any? = null
if (platformNN !== null) {}
if (null !== platformNN) {}
if (platformNN === null) {}
if (null === platformNN) {}
if (<!SENSELESS_COMPARISON!>platformNN !== null<!>) {}
if (<!SENSELESS_COMPARISON!>null !== platformNN<!>) {}
if (<!SENSELESS_COMPARISON!>platformNN === null<!>) {}
if (<!SENSELESS_COMPARISON!>null === platformNN<!>) {}
if (platformN !== null) {}
if (platformN === null) {}
@@ -1,15 +0,0 @@
fun readLine() = "x"
fun foo() {
var line = ""
while (line != null) {
line = readLine()
if (line != null) {
bar()
}
}
}
fun bar() {}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
fun readLine() = "x"
fun foo() {
@@ -1,23 +1,23 @@
fun testEquals(x: Int) {
if (x == null) {}
if (x == (null)) {}
if (x == foo@ null) {}
if (<!SENSELESS_COMPARISON!>x == null<!>) {}
if (<!SENSELESS_COMPARISON!>x == (null)<!>) {}
if (<!SENSELESS_COMPARISON!>x == foo@ null<!>) {}
}
fun testEqualsFlipped(x: Int) {
if (null == x) {}
if ((null) == x) {}
if (foo@ null == x) {}
if (<!SENSELESS_COMPARISON!>null == x<!>) {}
if (<!SENSELESS_COMPARISON!>(null) == x<!>) {}
if (<!SENSELESS_COMPARISON!>foo@ null == x<!>) {}
}
fun testNotEquals(x: Int) {
if (x != null) {}
if (x != (null)) {}
if (x != foo@ null) {}
if (<!SENSELESS_COMPARISON!>x != null<!>) {}
if (<!SENSELESS_COMPARISON!>x != (null)<!>) {}
if (<!SENSELESS_COMPARISON!>x != foo@ null<!>) {}
}
fun testNotEqualsFlipped(x: Int) {
if (null != x) {}
if ((null) != x) {}
if (foo@ null != x) {}
}
if (<!SENSELESS_COMPARISON!>null != x<!>) {}
if (<!SENSELESS_COMPARISON!>(null) != x<!>) {}
if (<!SENSELESS_COMPARISON!>foo@ null != x<!>) {}
}
@@ -3,7 +3,7 @@ fun foo(): String {
s = null
s?.length
s<!UNSAFE_CALL!>.<!>length
if (s == null) return s!!
if (<!SENSELESS_COMPARISON!>s == null<!>) return s!!
var t: String? = "y"
if (t == null) t = "x"
var x: Int? = null
@@ -15,7 +15,7 @@ object Impl : SomeSubClass {
fun g(a: SomeClass?) {
var b = (a as? SomeSubClass)?.foo
b = "Hello"
if (b != null) {
if (<!SENSELESS_COMPARISON!>b != null<!>) {
// 'a' cannot be cast to SomeSubClass!
a<!UNSAFE_CALL!>.<!>hashCode()
a.<!UNRESOLVED_REFERENCE!>foo<!>
@@ -24,7 +24,7 @@ fun g(a: SomeClass?) {
}
var c = a as? SomeSubClass
c = Impl
if (c != null) {
if (<!SENSELESS_COMPARISON!>c != null<!>) {
// 'a' cannot be cast to SomeSubClass
a<!UNSAFE_CALL!>.<!>hashCode()
a.<!UNRESOLVED_REFERENCE!>foo<!>
@@ -1,6 +1,6 @@
fun bar(x: Int?): Int {
if (x != null) return -1
if (x == null) return -2
if (<!SENSELESS_COMPARISON!>x == null<!>) return -2
// Should be unreachable
return 2 + 2
}
}
@@ -7,7 +7,7 @@ fun foo(arg: Int?) {
x.hashCode()
x = null
}
if (x != null) x = 42
if (<!SENSELESS_COMPARISON!>x != null<!>) x = 42
// Unsafe because of lambda
x<!UNSAFE_CALL!>.<!>hashCode()
}
@@ -4,7 +4,7 @@ fun String.next(): String {
fun list(start: String) {
var e: Any? = start
if (e==null) return
if (<!SENSELESS_COMPARISON!>e==null<!>) return
while (e is String) {
// Smart cast due to the loop condition
if (e.length == 0)
@@ -3,7 +3,7 @@ fun f() {
var s: String?
s = "a"
var s1 = "" // String ?
if (s != null) { // Redundant
if (<!SENSELESS_COMPARISON!>s != null<!>) { // Redundant
s1.length
// We can do smartcast here and below
s1 = s.toString() // return String?
@@ -18,4 +18,4 @@ fun f() {
val s3 = s.toString()
s3.length
}
}
}
@@ -11,9 +11,9 @@ public fun foo(qq: String?): Int {
// p = r, r = q and q is not null
p.length
} while (!x())
} while (r == null) // r = q and q is not null
} while (<!SENSELESS_COMPARISON!>r == null<!>) // r = q and q is not null
if (!x()) break
}
// Smart cast is possible
return q.length
}
}
@@ -6,7 +6,7 @@ abstract class KotlinClass : JavaInterface1, JavaInterface2 {
fun foo(k: KotlinClass) {
useString(k.getSomething())
useString(k.something)
if (k.something == null) return
if (<!SENSELESS_COMPARISON!>k.something == null<!>) return
k.setSomething(1)
k.something = <!ASSIGNMENT_TYPE_MISMATCH!>1<!>
@@ -1,25 +0,0 @@
// FILE: KotlinFile.kt
abstract class KotlinClass : JavaInterface1, JavaInterface2 {
override fun getSomething(): String = ""
}
fun foo(k: KotlinClass) {
useString(k.getSomething())
useString(k.something)
if (k.something == null) return
k.setSomething("")
k.something = ""
}
fun useString(i: String) {}
// FILE: JavaInterface1.java
public interface JavaInterface1 {
String getSomething();
}
// FILE: JavaInterface2.java
public interface JavaInterface2 {
void setSomething(String value);
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// FILE: KotlinFile.kt
abstract class KotlinClass : JavaInterface1, JavaInterface2 {
override fun getSomething(): String = ""
@@ -1,28 +0,0 @@
// FILE: KotlinFile.kt
abstract class KotlinClass : JavaInterface3 {
override fun getSomething(): String = ""
}
fun foo(k: KotlinClass) {
useString(k.getSomething())
useString(k.something)
if (k.something == null) return
k.setSomething("")
k.something = ""
}
fun useString(i: String) {}
// FILE: JavaInterface1.java
public interface JavaInterface1 {
String getSomething();
}
// FILE: JavaInterface2.java
public interface JavaInterface2 {
void setSomething(String value);
}
// FILE: JavaInterface3.java
public interface JavaInterface3 extends JavaInterface1, JavaInterface2 {}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// FILE: KotlinFile.kt
abstract class KotlinClass : JavaInterface3 {
override fun getSomething(): String = ""
@@ -1,29 +0,0 @@
// FILE: KotlinFile.kt
abstract class KotlinClass : JavaInterface3 {
override fun getSomething(): String = ""
}
fun foo(k: KotlinClass) {
useString(k.getSomething())
useString(k.something)
if (k.something == null) return
k.setSomething("")
k.something = ""
}
fun useString(i: String) {}
// FILE: JavaInterface1.java
public interface JavaInterface1 {
String getSomething();
}
// FILE: JavaInterface2.java
public interface JavaInterface2 {
String getSomething();
void setSomething(String value);
}
// FILE: JavaInterface3.java
public interface JavaInterface3 extends JavaInterface1, JavaInterface2 {}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// FILE: KotlinFile.kt
abstract class KotlinClass : JavaInterface3 {
override fun getSomething(): String = ""
@@ -3,7 +3,7 @@
// exhaustive
fun test1(n: Nothing) = when (n) { }
fun test2(n: Nothing?) = when (n) {
null -> {}
<!SENSELESS_NULL_IN_WHEN!>null<!> -> {}
}
// not exhaustive
@@ -18,7 +18,7 @@ fun identityEqualsWithVariables(x: Any?, y: Any?) {
fun equalConstants() {
contract {
<!ERROR_IN_CONTRACT_DESCRIPTION!>returns() implies (null == null)<!>
<!ERROR_IN_CONTRACT_DESCRIPTION!>returns() implies (<!SENSELESS_COMPARISON!>null == null<!>)<!>
}
}
@@ -27,4 +27,4 @@ fun equalNullWithCall() {
contract {
<!ERROR_IN_CONTRACT_DESCRIPTION!>returns() implies (get() == null)<!>
}
}
}
@@ -6,7 +6,7 @@ import kotlin.contracts.*
fun Any?.foo(): Boolean {
contract {
<!ERROR_IN_CONTRACT_DESCRIPTION!>returns(true) implies (this != null)<!>
<!ERROR_IN_CONTRACT_DESCRIPTION!>returns(true) implies (<!SENSELESS_COMPARISON!>this != null<!>)<!>
}
return this != null
}
}
@@ -39,7 +39,7 @@ fun notNullWhenNotNull (x: Int?): Int? {
fun testNotNullWhenNotNull (x: Int?) {
if (notNullWhenNotNull(x) == null) {
x == null
<!SENSELESS_COMPARISON!>x == null<!>
}
else {
x<!UNSAFE_CALL!>.<!>dec()
@@ -49,7 +49,7 @@ fun testNotNullWhenNotNull (x: Int?) {
x<!UNSAFE_CALL!>.<!>dec()
}
else {
x == null
<!SENSELESS_COMPARISON!>x == null<!>
}
x<!UNSAFE_CALL!>.<!>dec()
@@ -60,7 +60,7 @@ fun testAtLeastOnceFlow(x: Any?) {
do {
myAssert(x is String)
x.length
} while (x != null)
} while (<!SENSELESS_COMPARISON!>x != null<!>)
x.length
}
@@ -5,7 +5,7 @@ fun main(x1: Double?, range: ClosedRange<Double>) {
}
when {
x1 == null -> throw Exception()
<!SENSELESS_COMPARISON!>x1 == null<!> -> throw Exception()
x1 in range -> {}
}
}