[FE 1.0] Prohibit using non exhaustive if and when in rhs of elvis expression

^KT-44705
^KT-49349 Fixed
This commit is contained in:
Dmitriy Novozhilov
2021-10-21 12:49:36 +03:00
committed by teamcityserver
parent 06a26a5a74
commit a2b8493f47
16 changed files with 192 additions and 9 deletions
@@ -56,8 +56,8 @@ fun g6(): Unit = <!TYPE_MISMATCH!><!NO_ELSE_IN_WHEN!>when<!> { true -> 42 }<!>
fun foo1(x: String?) {
"" + <!INVALID_IF_AS_EXPRESSION!>if<!> (true) 42
w@while (true) {
x ?: if (true) break
x ?: when { true -> break@w }
x ?: <!INVALID_IF_AS_EXPRESSION_WARNING!>if<!> (true) break
x ?: <!NO_ELSE_IN_WHEN_WARNING!>when<!> { true -> break@w }
}
}
@@ -0,0 +1,30 @@
// FIR_IDENTICAL
// LANGUAGE: +ProhibitNonExhaustiveIfInRhsOfElvis
// DIAGNOSTICS: -USELESS_ELVIS
// ISSUE: KT-44705
fun test_1(x: String?, y: String?) {
while (true) {
x ?: <!INVALID_IF_AS_EXPRESSION!>if<!> (y == null) break
}
}
fun test_2(x: String?, y: String?) {
while (true) {
val z = x ?: <!INVALID_IF_AS_EXPRESSION!>if<!> (y == null) break
}
}
fun test_3(x: String?, y: String?) {
while (true) {
x ?: <!NO_ELSE_IN_WHEN!>when<!> { true -> break }
x ?: <!NO_ELSE_IN_WHEN!>when<!> (y) { "hello" -> break }
}
}
fun test_4(x: String?, y: String?) {
while (true) {
val a = x ?: <!NO_ELSE_IN_WHEN!>when<!> { true -> break }
val b = x ?: <!NO_ELSE_IN_WHEN!>when<!> (y) { "hello" -> break }
}
}
@@ -0,0 +1,7 @@
package
public fun test_1(/*0*/ x: kotlin.String?, /*1*/ y: kotlin.String?): kotlin.Unit
public fun test_2(/*0*/ x: kotlin.String?, /*1*/ y: kotlin.String?): kotlin.Unit
public fun test_3(/*0*/ x: kotlin.String?, /*1*/ y: kotlin.String?): kotlin.Unit
public fun test_4(/*0*/ x: kotlin.String?, /*1*/ y: kotlin.String?): kotlin.Unit
@@ -0,0 +1,29 @@
// LANGUAGE: -ProhibitNonExhaustiveIfInRhsOfElvis
// DIAGNOSTICS: -USELESS_ELVIS
// ISSUE: KT-44705
fun test_1(x: String?, y: String?) {
while (true) {
x ?: <!INVALID_IF_AS_EXPRESSION!>if<!> (y == null) break
}
}
fun test_2(x: String?, y: String?) {
while (true) {
val z = x ?: <!INVALID_IF_AS_EXPRESSION!>if<!> (y == null) break
}
}
fun test_3(x: String?, y: String?) {
while (true) {
x ?: <!NO_ELSE_IN_WHEN!>when<!> { true -> break }
x ?: <!NO_ELSE_IN_WHEN!>when<!> (y) { "hello" -> break }
}
}
fun test_4(x: String?, y: String?) {
while (true) {
val a = x ?: <!NO_ELSE_IN_WHEN!>when<!> { true -> break }
val b = x ?: <!NO_ELSE_IN_WHEN!>when<!> (y) { "hello" -> break }
}
}
@@ -0,0 +1,29 @@
// LANGUAGE: -ProhibitNonExhaustiveIfInRhsOfElvis
// DIAGNOSTICS: -USELESS_ELVIS
// ISSUE: KT-44705
fun test_1(x: String?, y: String?) {
while (true) {
x ?: <!INVALID_IF_AS_EXPRESSION_WARNING!>if<!> (y == null) break
}
}
fun test_2(x: String?, y: String?) {
while (true) {
val z = x ?: <!INVALID_IF_AS_EXPRESSION!>if<!> (y == null) break
}
}
fun test_3(x: String?, y: String?) {
while (true) {
x ?: <!NO_ELSE_IN_WHEN_WARNING!>when<!> { true -> break }
x ?: <!NO_ELSE_IN_WHEN_WARNING!>when<!> (y) { "hello" -> break }
}
}
fun test_4(x: String?, y: String?) {
while (true) {
val a = x ?: <!NO_ELSE_IN_WHEN!>when<!> { true -> break }
val b = x ?: <!NO_ELSE_IN_WHEN!>when<!> (y) { "hello" -> break }
}
}
@@ -0,0 +1,7 @@
package
public fun test_1(/*0*/ x: kotlin.String?, /*1*/ y: kotlin.String?): kotlin.Unit
public fun test_2(/*0*/ x: kotlin.String?, /*1*/ y: kotlin.String?): kotlin.Unit
public fun test_3(/*0*/ x: kotlin.String?, /*1*/ y: kotlin.String?): kotlin.Unit
public fun test_4(/*0*/ x: kotlin.String?, /*1*/ y: kotlin.String?): kotlin.Unit
@@ -1,6 +1,6 @@
public fun foo(x: String?, y: String?): Int {
while (true) {
x ?: if (y == null) break
x ?: <!INVALID_IF_AS_EXPRESSION_WARNING!>if<!> (y == null) break
// y is nullable if x != null
y<!UNSAFE_CALL!>.<!>length
}