FIR checker: warn useless as and is

This commit is contained in:
Jinseong Jeon
2021-04-28 22:32:51 -07:00
committed by TeamCityServer
parent 19d939c36e
commit e2dc21da90
141 changed files with 417 additions and 543 deletions
@@ -17,7 +17,7 @@ fun String?.gav() {}
fun bar(s: String?) {
if (s != null) return
s.gav()
s as? String
s as String?
s <!USELESS_CAST!>as? String<!>
s <!USELESS_CAST!>as String?<!>
s as String
}
@@ -14,7 +14,7 @@ fun g(a: SomeClass?) {
a.hashCode()
a.foo
(a as? SomeSubClass)<!UNSAFE_CALL!>.<!>foo
(a as SomeSubClass).foo
(a <!USELESS_CAST!>as SomeSubClass<!>).foo
}
val b = (a as? SomeSubClass)?.foo
if (b != null) {
@@ -22,7 +22,7 @@ fun g(a: SomeClass?) {
a.hashCode()
a.foo
(a as? SomeSubClass)<!UNSAFE_CALL!>.<!>foo
(a as SomeSubClass).foo
(a <!USELESS_CAST!>as SomeSubClass<!>).foo
}
val c = a as? SomeSubClass
if (c != null) {
@@ -33,4 +33,4 @@ fun g(a: SomeClass?) {
c.hashCode()
c.foo
}
}
}
@@ -14,7 +14,7 @@ fun g(a: SomeClass?) {
a.hashCode()
a.foo
(a as? SomeSubClass)<!UNSAFE_CALL!>.<!>foo
(a as SomeSubClass).foo
(a <!USELESS_CAST!>as SomeSubClass<!>).foo
}
val b = (a as? SomeSubClass)?.foo
if (b != null) {
@@ -22,7 +22,7 @@ fun g(a: SomeClass?) {
a.hashCode()
a.foo
(a as? SomeSubClass)<!UNSAFE_CALL!>.<!>foo
(a as SomeSubClass).foo
(a <!USELESS_CAST!>as SomeSubClass<!>).foo
}
val c = a as? SomeSubClass
if (c != null) {
@@ -33,4 +33,4 @@ fun g(a: SomeClass?) {
c.hashCode()
c.foo
}
}
}
@@ -1,25 +0,0 @@
// !LANGUAGE: +SafeCastCheckBoundSmartCasts
// See KT-20752
class Unstable {
val first: String? get() = null
}
class StringList {
fun remove(s: String) = s
}
fun StringList.remove(s: String?) = s ?: ""
fun foo(list: StringList, arg: Unstable) {
list.remove(arg.first)
if (arg.first as? String != null) {
// Should be still resolved to extension, without smart cast or smart cast impossible
list.remove(arg.first)
}
val s = arg.first as? String
if (s != null) {
// Should be still resolved to extension, without smart cast or smart cast impossible
list.remove(arg.first)
}
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// !LANGUAGE: +SafeCastCheckBoundSmartCasts
// See KT-20752
@@ -7,7 +7,7 @@ fun <T> T?.let(f: (T) -> Unit) {
}
fun test(your: Your?) {
(your?.foo() as? Any)?.let {}
(your?.foo() <!USELESS_CAST!>as? Any<!>)?.let {}
// strange smart cast to 'Your' at this point
your<!UNSAFE_CALL!>.<!>hashCode()
}
}
@@ -1,15 +0,0 @@
// Type inference failed after smart cast
interface A<T>
interface B<T> : A<T>
fun <T> foo(b: A<T>) = b
fun <T> test(a: A<T>) {
if (a is Any) {
// Error:(9, 9) Kotlin: Type inference failed: fun <T> foo(b: A<T>): kotlin.Unit
// cannot be applied to (A<T>)
foo(a)
}
foo(a) // ok
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
// Type inference failed after smart cast
interface A<T>
@@ -19,10 +19,10 @@ fun foo(a: A) {
fun foo2(a: A) {
if (a is C) {
if (a is B) {
if (<!USELESS_IS_CHECK!>a is B<!>) {
val t = when (a) {
is CC -> "CC"
}
}
}
}
}
@@ -12,7 +12,7 @@ object DD : D()
fun foo1(a: A) {
if (a is B) {
if (a is D) {
if (a is C) {
if (<!USELESS_IS_CHECK!>a is C<!>) {
val t =
when (a) {
is DD -> "DD"
@@ -25,7 +25,7 @@ fun foo1(a: A) {
fun foo2(a: A) {
if (a is B) {
if (a is D) {
if (a is C) {
if (<!USELESS_IS_CHECK!>a is C<!>) {
val t =
when (a) {
is DD -> "DD"
@@ -33,4 +33,4 @@ fun foo2(a: A) {
}
}
}
}
}
@@ -1,28 +0,0 @@
//KT-5455 Need warning about redundant type cast
fun foo(o: Any): Int {
if (o is String) {
return (o as String).length
}
return -1
}
open class A {
fun foo() {}
}
class B: A()
fun test(a: Any?) {
if (a is B) {
(a as A).foo()
}
}
fun test1(a: B) {
(a as A?)?.foo()
}
fun test2(b: B?) {
if (b != null) {
(b as A).foo()
}
}
@@ -1,3 +1,4 @@
// FIR_IDENTICAL
//KT-5455 Need warning about redundant type cast
fun foo(o: Any): Int {
if (o is String) {
@@ -1,13 +1,13 @@
// See also KT-10992: we should have no errors for all unsafe hashCode() calls
fun foo(arg: Any?) {
val x = arg as? Any ?: return
val x = arg <!USELESS_CAST!>as? Any<!> ?: return
arg.hashCode()
x.hashCode()
}
fun bar(arg: Any?) {
arg as? Any ?: return
arg <!USELESS_CAST!>as? Any<!> ?: return
arg.hashCode()
}