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
+2
-2
@@ -23,8 +23,8 @@ fun case_3(value_1: String?, value_2: Boolean): Boolean {
|
||||
|
||||
// TESTCASE NUMBER: 4
|
||||
fun case_4(value_1: Nothing?, value_2: Boolean?): Boolean? {
|
||||
contract { <!ERROR_IN_CONTRACT_DESCRIPTION!>returns(null) implies (value_1 == null || value_2 != null || value_2 == false)<!> }
|
||||
return if (value_1 == null || value_2 != null || value_2 == false) null else true
|
||||
contract { <!ERROR_IN_CONTRACT_DESCRIPTION!>returns(null) implies (<!SENSELESS_COMPARISON!>value_1 == null<!> || value_2 != null || value_2 == false)<!> }
|
||||
return if (<!SENSELESS_COMPARISON!>value_1 == null<!> || value_2 != null || value_2 == false) null else true
|
||||
}
|
||||
|
||||
// TESTCASE NUMBER: 5
|
||||
|
||||
+2
-2
@@ -5,7 +5,7 @@ import kotlin.contracts.*
|
||||
// TESTCASE NUMBER: 1
|
||||
fun Any?.case_1(): 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
|
||||
}
|
||||
@@ -21,7 +21,7 @@ fun Any?.case_2(): Boolean {
|
||||
// TESTCASE NUMBER: 3
|
||||
fun <T> T?.case_3(): Boolean {
|
||||
contract {
|
||||
<!ERROR_IN_CONTRACT_DESCRIPTION!>returnsNotNull() implies (this != null)<!>
|
||||
<!ERROR_IN_CONTRACT_DESCRIPTION!>returnsNotNull() implies (<!SENSELESS_COMPARISON!>this != null<!>)<!>
|
||||
}
|
||||
return this != null
|
||||
}
|
||||
|
||||
+1
-1
@@ -66,7 +66,7 @@ fun case_11(value_1: Any?): Boolean? {
|
||||
// TESTCASE NUMBER: 12
|
||||
fun Char.case_12() {
|
||||
contract { returns() implies (this@case_12 == null) }
|
||||
if (this@case_12 != null) throw Exception()
|
||||
if (<!SENSELESS_COMPARISON!>this@case_12 != null<!>) throw Exception()
|
||||
}
|
||||
|
||||
// TESTCASE NUMBER: 13
|
||||
|
||||
Reference in New Issue
Block a user