Do not unbox nullable Result, since before usage it is coerced

#KT-46505
This commit is contained in:
Ilmir Usmanov
2021-05-10 13:07:23 +02:00
parent 656c2496a6
commit 640d263ae1
19 changed files with 254 additions and 27 deletions
@@ -6,17 +6,23 @@ object Foo {
}
fun bar(value: Value?) {
res = value?.value as String
res = value?.value as String?
}
}
var res = "FAIL"
var res: String? = "FAIL"
fun box(): String {
Value("OK").let(Foo::foo)
if (res != "OK") return "FAIL 1: $res"
res = "FAIL"
res = "FAIL 2"
Value("OK").let(Foo::bar)
return res
if (res != "OK") return "FAIL 3: $res"
res = "FAIL 4"
null.let(Foo::bar)
if (res != null) return "FAIL 5: $res"
return "OK"
}
@@ -6,17 +6,23 @@ object Foo {
}
fun bar(value: Value?) {
res = value?.value as String
res = value?.value as String?
}
}
var res = "FAIL"
var res: String? = "FAIL"
fun box(): String {
Value("OK").let(Foo::foo)
if (res != "OK") return "FAIL 1: $res"
res = "FAIL"
res = "FAIL 2"
Value("OK").let(Foo::bar)
return res
if (res != "OK") return "FAIL 3: $res"
res = "FAIL 4"
null.let(Foo::bar)
if (res != null) return "FAIL 5: $res"
return "OK"
}
@@ -6,11 +6,11 @@ object Foo {
}
fun bar(value: Value?) {
res = value?.value!!
res = value?.value
}
}
var res = 0
var res: Int? = 0
fun box(): String {
Value(42).let(Foo::foo)
@@ -19,6 +19,10 @@ fun box(): String {
Value(42).let(Foo::bar)
if (res != 42) return "FAIL 2 $res"
res = 0
null.let(Foo::bar)
if (res != null) return "FAIL 3: $res"
return "OK"
}
@@ -2,15 +2,15 @@ inline class Value(val value: Int?)
object Foo {
fun foo(value: Value) {
res = value.value!!
res = value.value
}
fun bar(value: Value?) {
res = value?.value!!
res = value?.value
}
}
var res = 0
var res: Int? = 0
fun box(): String {
Value(42).let(Foo::foo)
@@ -19,6 +19,10 @@ fun box(): String {
Value(42).let(Foo::bar)
if (res != 42) return "FAIL 2 $res"
res = 0
null.let(Foo::bar)
if (res != null) return "FAIL 3: $res"
return "OK"
}
@@ -4,21 +4,26 @@
object Foo {
fun foo(result: Result<String>) {
res = result.getOrNull()!!
res = result.getOrNull()
}
fun bar(result: Result<String>?) {
res = result?.getOrNull()!!
res = result?.getOrNull()
}
}
var res = "FAIL"
var res: String? = "FAIL"
fun box(): String {
Result.success("OK").let(Foo::foo)
if (res != "OK") return "FAIL 1 $res"
res = "FAIL"
res = "FAIL 2"
Result.success("OK").let(Foo::bar)
return res
if (res != "OK") return "FAIL 3 $res"
res = "FAIL 4"
null.let(Foo::bar)
if (res != null) return "FAIL 5: $res"
return "OK"
}
@@ -6,17 +6,22 @@ object Foo {
}
fun bar(value: Value?) {
res = value?.value!!
res = value?.value
}
}
var res = "FAIL"
var res: String? = "FAIL"
fun box(): String {
Value("OK").let(Foo::foo)
if (res != "OK") return "FAIL 1: $res"
res = "FAIL"
res = "FAIL 2"
Value("OK").let(Foo::bar)
return res
if (res != "OK") return "FAIL 3: $res"
res = "FAIL 4"
null.let(Foo::bar)
if (res != null) return "FAIL 3: $res"
return "OK"
}
@@ -2,21 +2,26 @@ inline class Value(val value: String?)
object Foo {
fun foo(value: Value) {
res = value.value!!
res = value.value
}
fun bar(value: Value?) {
res = value?.value!!
res = value?.value
}
}
var res = "FAIL"
var res: String? = "FAIL"
fun box(): String {
Value("OK").let(Foo::foo)
if (res != "OK") return "FAIL 1: $res"
res = "FAIL"
res = "FAIL 2"
Value("OK").let(Foo::bar)
return res
if (res != "OK") return "FAIL 3: $res"
res = "FAIL 4"
null.let(Foo::bar)
if (res != null) return "FAIL 3: $res"
return "OK"
}