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:
committed by
teamcityserver
parent
4726dcce40
commit
c7272f6986
-10
@@ -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
@@ -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
|
||||
|
||||
|
||||
-16
@@ -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
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user