diff --git a/libraries/stdlib/src/generated/_Arrays.kt b/libraries/stdlib/src/generated/_Arrays.kt index 07bd5328ba1..a7b091514fc 100644 --- a/libraries/stdlib/src/generated/_Arrays.kt +++ b/libraries/stdlib/src/generated/_Arrays.kt @@ -10823,6 +10823,7 @@ public fun Array.requireNoNulls(): Array { throw IllegalArgumentException("null element found in $this.") } } + @Suppress("CAST_NEVER_SUCCEEDS") return this as Array } @@ -12531,6 +12532,7 @@ public inline fun > Array<*>.filterIsInst */ @kotlin.jvm.JvmVersion public fun , R> Array<*>.filterIsInstanceTo(destination: C, klass: Class): C { + @Suppress("UNCHECKED_CAST") for (element in this) if (klass.isInstance(element)) destination.add(element as R) return destination } @@ -13002,6 +13004,7 @@ public fun ByteArray.toTypedArray(): Array { val result = arrayOfNulls(size) for (index in indices) result[index] = this[index] + @Suppress("CAST_NEVER_SUCCEEDS") return result as Array } @@ -13013,6 +13016,7 @@ public fun ShortArray.toTypedArray(): Array { val result = arrayOfNulls(size) for (index in indices) result[index] = this[index] + @Suppress("CAST_NEVER_SUCCEEDS") return result as Array } @@ -13024,6 +13028,7 @@ public fun IntArray.toTypedArray(): Array { val result = arrayOfNulls(size) for (index in indices) result[index] = this[index] + @Suppress("CAST_NEVER_SUCCEEDS") return result as Array } @@ -13035,6 +13040,7 @@ public fun LongArray.toTypedArray(): Array { val result = arrayOfNulls(size) for (index in indices) result[index] = this[index] + @Suppress("CAST_NEVER_SUCCEEDS") return result as Array } @@ -13046,6 +13052,7 @@ public fun FloatArray.toTypedArray(): Array { val result = arrayOfNulls(size) for (index in indices) result[index] = this[index] + @Suppress("CAST_NEVER_SUCCEEDS") return result as Array } @@ -13057,6 +13064,7 @@ public fun DoubleArray.toTypedArray(): Array { val result = arrayOfNulls(size) for (index in indices) result[index] = this[index] + @Suppress("CAST_NEVER_SUCCEEDS") return result as Array } @@ -13068,6 +13076,7 @@ public fun BooleanArray.toTypedArray(): Array { val result = arrayOfNulls(size) for (index in indices) result[index] = this[index] + @Suppress("CAST_NEVER_SUCCEEDS") return result as Array } @@ -13079,6 +13088,7 @@ public fun CharArray.toTypedArray(): Array { val result = arrayOfNulls(size) for (index in indices) result[index] = this[index] + @Suppress("CAST_NEVER_SUCCEEDS") return result as Array } diff --git a/libraries/stdlib/src/generated/_Collections.kt b/libraries/stdlib/src/generated/_Collections.kt index a9cf105f2cb..3c0c9bc0391 100644 --- a/libraries/stdlib/src/generated/_Collections.kt +++ b/libraries/stdlib/src/generated/_Collections.kt @@ -799,6 +799,7 @@ public fun > MutableList.sortDescending(): Unit { public fun > Iterable.sorted(): List { if (this is Collection) { if (size <= 1) return this.toMutableList() + @Suppress("CAST_NEVER_SUCCEEDS") return (toTypedArray>() as Array).apply { sort() }.asList() } return toMutableList().apply { sort() } @@ -831,6 +832,7 @@ public fun > Iterable.sortedDescending(): List { public fun Iterable.sortedWith(comparator: Comparator): List { if (this is Collection) { if (size <= 1) return this.toMutableList() + @Suppress("CAST_NEVER_SUCCEEDS") return (toTypedArray() as Array).apply { sortWith(comparator) }.asList() } return toMutableList().apply { sortWith(comparator) } @@ -1593,6 +1595,7 @@ public fun Iterable.requireNoNulls(): Iterable { throw IllegalArgumentException("null element found in $this.") } } + @Suppress("UNCHECKED_CAST") return this as Iterable } @@ -1605,6 +1608,7 @@ public fun List.requireNoNulls(): List { throw IllegalArgumentException("null element found in $this.") } } + @Suppress("UNCHECKED_CAST") return this as List } @@ -1896,6 +1900,7 @@ public inline fun > Iterable<*>.filterIsI */ @kotlin.jvm.JvmVersion public fun , R> Iterable<*>.filterIsInstanceTo(destination: C, klass: Class): C { + @Suppress("UNCHECKED_CAST") for (element in this) if (klass.isInstance(element)) destination.add(element as R) return destination } diff --git a/libraries/stdlib/src/generated/_Sequences.kt b/libraries/stdlib/src/generated/_Sequences.kt index 1c1c0e7dee4..5a56d284fde 100644 --- a/libraries/stdlib/src/generated/_Sequences.kt +++ b/libraries/stdlib/src/generated/_Sequences.kt @@ -343,6 +343,7 @@ public fun Sequence.filterNot(predicate: (T) -> Boolean): Sequence { * Returns a sequence containing all elements that are not `null`. */ public fun Sequence.filterNotNull(): Sequence { + @Suppress("UNCHECKED_CAST") return filterNot { it == null } as Sequence } @@ -1179,6 +1180,7 @@ public inline fun Sequence.asSequence(): Sequence { */ @kotlin.jvm.JvmVersion public inline fun Sequence<*>.filterIsInstance(): Sequence<@kotlin.internal.NoInfer R> { + @Suppress("UNCHECKED_CAST") return filter { it is R } as Sequence } @@ -1187,6 +1189,7 @@ public inline fun Sequence<*>.filterIsInstance(): Sequence<@kotlin.i */ @kotlin.jvm.JvmVersion public fun Sequence<*>.filterIsInstance(klass: Class): Sequence { + @Suppress("UNCHECKED_CAST") return filter { klass.isInstance(it) } as Sequence } @@ -1204,6 +1207,7 @@ public inline fun > Sequence<*>.filterIsI */ @kotlin.jvm.JvmVersion public fun , R> Sequence<*>.filterIsInstanceTo(destination: C, klass: Class): C { + @Suppress("UNCHECKED_CAST") for (element in this) if (klass.isInstance(element)) destination.add(element as R) return destination } diff --git a/libraries/stdlib/src/kotlin/collections/ArraysJVM.kt b/libraries/stdlib/src/kotlin/collections/ArraysJVM.kt index a1823894dff..858cb784044 100644 --- a/libraries/stdlib/src/kotlin/collections/ArraysJVM.kt +++ b/libraries/stdlib/src/kotlin/collections/ArraysJVM.kt @@ -18,7 +18,9 @@ public inline fun ByteArray.toString(charset: Charset): String = String(this, ch * Allocates an array of runtime type `T` having its size equal to the size of this collection * and populates the array with the elements of this collection. */ +@Suppress("UNCHECKED_CAST") public inline fun Collection.toTypedArray(): Array { + @Suppress("PLATFORM_CLASS_MAPPED_TO_KOTLIN") val thisCollection = this as java.util.Collection return thisCollection.toArray(arrayOfNulls(thisCollection.size())) as Array } @@ -28,5 +30,6 @@ public inline fun Array?.orEmpty(): Array = this ?: ar /** Internal unsafe construction of array based on reference array type */ internal fun arrayOfNulls(reference: Array, size: Int): Array { + @Suppress("UNCHECKED_CAST") return java.lang.reflect.Array.newInstance(reference.javaClass.componentType, size) as Array } diff --git a/libraries/stdlib/src/kotlin/collections/MapAccessors.kt b/libraries/stdlib/src/kotlin/collections/MapAccessors.kt index a533dcb80a0..2e54db5aa1f 100644 --- a/libraries/stdlib/src/kotlin/collections/MapAccessors.kt +++ b/libraries/stdlib/src/kotlin/collections/MapAccessors.kt @@ -13,9 +13,8 @@ import kotlin.internal.Exact * @throws NoSuchElementException when the map doesn't contain value for the property name and doesn't provide an implicit default (see [withDefault]). */ @kotlin.internal.InlineOnly -@Suppress("NON_PUBLIC_CALL_FROM_PUBLIC_INLINE") public inline operator fun Map.getValue(thisRef: Any?, property: KProperty<*>): V1 - = getOrImplicitDefault(property.name) as V1 + = @Suppress("UNCHECKED_CAST", "NON_PUBLIC_CALL_FROM_PUBLIC_INLINE") (getOrImplicitDefault(property.name) as V1) /** * Returns the value of the property for the given object from this mutable map. @@ -27,9 +26,8 @@ public inline operator fun Map.getValue(thisRef: */ @kotlin.jvm.JvmName("getVar") @kotlin.internal.InlineOnly -@Suppress("NON_PUBLIC_CALL_FROM_PUBLIC_INLINE") public inline operator fun MutableMap.getValue(thisRef: Any?, property: KProperty<*>): V - = getOrImplicitDefault(property.name) as V + = @Suppress("UNCHECKED_CAST", "NON_PUBLIC_CALL_FROM_PUBLIC_INLINE") (getOrImplicitDefault(property.name) as V) /** * Stores the value of the property for the given object in this mutable map. diff --git a/libraries/stdlib/src/kotlin/collections/Maps.kt b/libraries/stdlib/src/kotlin/collections/Maps.kt index e68546a6e7b..c4f87e8a7b3 100644 --- a/libraries/stdlib/src/kotlin/collections/Maps.kt +++ b/libraries/stdlib/src/kotlin/collections/Maps.kt @@ -25,7 +25,7 @@ private object EmptyMap : Map, Serializable { } /** Returns an empty read-only map of specified type. The returned map is serializable (JVM). */ -public fun emptyMap(): Map = EmptyMap as Map +public fun emptyMap(): Map = @Suppress("CAST_NEVER_SUCCEEDS") (EmptyMap as Map) /** * Returns a new read-only map with the specified contents, given as a list of pairs @@ -115,7 +115,8 @@ public inline operator fun <@kotlin.internal.OnlyInputTypes K, V> Map. * Returns the value corresponding to the given [key], or `null` if such a key is not present in the map. */ @kotlin.internal.InlineOnly -public inline operator fun <@kotlin.internal.OnlyInputTypes K, V> Map.get(key: K): V? = (this as Map).get(key) +public inline operator fun <@kotlin.internal.OnlyInputTypes K, V> Map.get(key: K): V? + = @Suppress("UNCHECKED_CAST") (this as Map).get(key) /** * Returns `true` if the map contains the specified [key]. @@ -123,7 +124,8 @@ public inline operator fun <@kotlin.internal.OnlyInputTypes K, V> Map. * Allows to overcome type-safety restriction of `containsKey` that requires to pass a key of type `K`. */ @kotlin.internal.InlineOnly -public inline fun <@kotlin.internal.OnlyInputTypes K> Map.containsKey(key: K): Boolean = (this as Map).containsKey(key) +public inline fun <@kotlin.internal.OnlyInputTypes K> Map.containsKey(key: K): Boolean + = @Suppress("UNCHECKED_CAST") (this as Map).containsKey(key) /** * Returns `true` if the map maps one or more keys to the specified [value]. @@ -142,7 +144,8 @@ public inline fun Map.containsValue * Allows to overcome type-safety restriction of `remove` that requires to pass a key of type `K`. */ @kotlin.internal.InlineOnly -public inline fun <@kotlin.internal.OnlyInputTypes K, V> MutableMap.remove(key: K): V? = (this as MutableMap).remove(key) +public inline fun <@kotlin.internal.OnlyInputTypes K, V> MutableMap.remove(key: K): V? + = @Suppress("UNCHECKED_CAST") (this as MutableMap).remove(key) /** * Returns the key component of the map entry. diff --git a/libraries/stdlib/src/kotlin/collections/MutableCollections.kt b/libraries/stdlib/src/kotlin/collections/MutableCollections.kt index 3ce4f56f3ca..83525fbac18 100644 --- a/libraries/stdlib/src/kotlin/collections/MutableCollections.kt +++ b/libraries/stdlib/src/kotlin/collections/MutableCollections.kt @@ -14,7 +14,8 @@ import java.util.* * @return `true` if the element has been successfully removed; `false` if it was not present in the collection. */ @kotlin.internal.InlineOnly -public inline fun <@kotlin.internal.OnlyInputTypes T> MutableCollection.remove(element: T): Boolean = (this as MutableCollection).remove(element) +public inline fun <@kotlin.internal.OnlyInputTypes T> MutableCollection.remove(element: T): Boolean + = @Suppress("UNCHECKED_CAST") (this as MutableCollection).remove(element) /** * Removes all of this collection's elements that are also contained in the specified collection. @@ -24,7 +25,8 @@ public inline fun <@kotlin.internal.OnlyInputTypes T> MutableCollection.r * @return `true` if any of the specified elements was removed from the collection, `false` if the collection was not modified. */ @kotlin.internal.InlineOnly -public inline fun <@kotlin.internal.OnlyInputTypes T> MutableCollection.removeAll(elements: Collection): Boolean = (this as MutableCollection).removeAll(elements) +public inline fun <@kotlin.internal.OnlyInputTypes T> MutableCollection.removeAll(elements: Collection): Boolean + = @Suppress("UNCHECKED_CAST") (this as MutableCollection).removeAll(elements) /** * Retains only the elements in this collection that are contained in the specified collection. @@ -34,7 +36,8 @@ public inline fun <@kotlin.internal.OnlyInputTypes T> MutableCollection.r * @return `true` if any element was removed from the collection, `false` if the collection was not modified. */ @kotlin.internal.InlineOnly -public inline fun <@kotlin.internal.OnlyInputTypes T> MutableCollection.retainAll(elements: Collection): Boolean = (this as MutableCollection).retainAll(elements) +public inline fun <@kotlin.internal.OnlyInputTypes T> MutableCollection.retainAll(elements: Collection): Boolean + = @Suppress("UNCHECKED_CAST") (this as MutableCollection).retainAll(elements) /** * Adds the specified [element] to this mutable collection. diff --git a/libraries/stdlib/src/kotlin/comparisons/Comparisons.kt b/libraries/stdlib/src/kotlin/comparisons/Comparisons.kt index 6980849eb14..391fe56ddf9 100644 --- a/libraries/stdlib/src/kotlin/comparisons/Comparisons.kt +++ b/libraries/stdlib/src/kotlin/comparisons/Comparisons.kt @@ -74,6 +74,7 @@ public fun > compareValues(a: T?, b: T?): Int { if (a == null) return -1 if (b == null) return 1 + @Suppress("UNCHECKED_CAST") return (a as Comparable).compareTo(b) } @@ -279,18 +280,18 @@ public inline fun > nullsLast(): Comparator = nullsLast(nat /** * Returns a comparator that compares [Comparable] objects in natural order. */ -public fun > naturalOrder(): Comparator = NaturalOrderComparator as Comparator +public fun > naturalOrder(): Comparator = @Suppress("CAST_NEVER_SUCCEEDS") (NaturalOrderComparator as Comparator) /** * Returns a comparator that compares [Comparable] objects in reversed natural order. */ -public fun > reverseOrder(): Comparator = ReverseOrderComparator as Comparator +public fun > reverseOrder(): Comparator = @Suppress("CAST_NEVER_SUCCEEDS") (ReverseOrderComparator as Comparator) /** Returns a comparator that imposes the reverse ordering of this comparator. */ public fun Comparator.reversed(): Comparator = when (this) { is ReversedComparator -> this.comparator - NaturalOrderComparator -> ReverseOrderComparator as Comparator - ReverseOrderComparator -> NaturalOrderComparator as Comparator + NaturalOrderComparator -> @Suppress("CAST_NEVER_SUCCEEDS") (ReverseOrderComparator as Comparator) + ReverseOrderComparator -> @Suppress("CAST_NEVER_SUCCEEDS") (NaturalOrderComparator as Comparator) else -> ReversedComparator(this) } diff --git a/libraries/stdlib/src/kotlin/jvm/internal/unsafe/monitor.kt b/libraries/stdlib/src/kotlin/jvm/internal/unsafe/monitor.kt index 600bb5d17b7..ef2343740fe 100644 --- a/libraries/stdlib/src/kotlin/jvm/internal/unsafe/monitor.kt +++ b/libraries/stdlib/src/kotlin/jvm/internal/unsafe/monitor.kt @@ -17,7 +17,7 @@ package kotlin.jvm.internal.unsafe @kotlin.internal.InlineExposed -internal fun monitorEnter(monitor: Any): Unit = throw UnsupportedOperationException("This function can only be used privately") +internal fun monitorEnter(@Suppress("UNUSED_PARAMETER") monitor: Any): Unit = throw UnsupportedOperationException("This function can only be used privately") @kotlin.internal.InlineExposed -internal fun monitorExit(monitor: Any): Unit = throw UnsupportedOperationException("This function can only be used privately") +internal fun monitorExit(@Suppress("UNUSED_PARAMETER") monitor: Any): Unit = throw UnsupportedOperationException("This function can only be used privately") diff --git a/libraries/stdlib/src/kotlin/util/Lazy.kt b/libraries/stdlib/src/kotlin/util/Lazy.kt index 89d0650a2ed..31d05537d90 100644 --- a/libraries/stdlib/src/kotlin/util/Lazy.kt +++ b/libraries/stdlib/src/kotlin/util/Lazy.kt @@ -118,13 +118,14 @@ private class SynchronizedLazyImpl(initializer: () -> T, lock: Any? = nul get() { val _v1 = _value if (_v1 !== UNINITIALIZED_VALUE) { + @Suppress("UNCHECKED_CAST") return _v1 as T } return synchronized(lock) { val _v2 = _value if (_v2 !== UNINITIALIZED_VALUE) { - _v2 as T + @Suppress("UNCHECKED_CAST") (_v2 as T) } else { val typedValue = initializer!!() @@ -153,6 +154,7 @@ internal class UnsafeLazyImpl(initializer: () -> T) : Lazy, Serializab _value = initializer!!() initializer = null } + @Suppress("UNCHECKED_CAST") return _value as T } @@ -190,6 +192,7 @@ private class SafePublicationLazyImpl(initializer: () -> T) : Lazy, Se } } } + @Suppress("UNCHECKED_CAST") return _value as T } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Filtering.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Filtering.kt index abffa760adb..d69f70867b8 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Filtering.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Filtering.kt @@ -529,6 +529,7 @@ fun filtering(): List { returns(Sequences) { "Sequence" } body(Sequences) { """ + @Suppress("UNCHECKED_CAST") return filterNot { it == null } as Sequence """ } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Guards.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Guards.kt index 61fb702b8b4..57b18566c04 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Guards.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Guards.kt @@ -13,13 +13,14 @@ fun guards(): List { typeParam("T : Any") toNullableT = true returns("SELF") - body { + body { f -> """ for (element in this) { if (element == null) { throw IllegalArgumentException("null element found in $THIS.") } } + @Suppress("${if (f == InvariantArraysOfObjects) "CAST_NEVER_SUCCEEDS" else "UNCHECKED_CAST"}") return this as SELF """ } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Ordering.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Ordering.kt index da89071410c..e87b479d915 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Ordering.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Ordering.kt @@ -102,6 +102,7 @@ fun ordering(): List { """ if (this is Collection) { if (size <= 1) return this.toMutableList() + @Suppress("CAST_NEVER_SUCCEEDS") return (toTypedArray>() as Array).apply { sort() }.asList() } return toMutableList().apply { sort() } @@ -231,6 +232,7 @@ fun ordering(): List { """ if (this is Collection) { if (size <= 1) return this.toMutableList() + @Suppress("CAST_NEVER_SUCCEEDS") return (toTypedArray() as Array).apply { sortWith(comparator) }.asList() } return toMutableList().apply { sortWith(comparator) } diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJVM.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJVM.kt index 036dcecbdae..7aae0af1ca2 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJVM.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/SpecialJVM.kt @@ -202,6 +202,7 @@ fun specialJVM(): List { exclude(ArraysOfPrimitives, Strings) body { """ + @Suppress("UNCHECKED_CAST") for (element in this) if (klass.isInstance(element)) destination.add(element as R) return destination """ @@ -224,6 +225,7 @@ fun specialJVM(): List { returns(Sequences) { "Sequence" } body(Sequences) { """ + @Suppress("UNCHECKED_CAST") return filter { klass.isInstance(it) } as Sequence """ } @@ -264,6 +266,7 @@ fun specialJVM(): List { receiverAsterisk(true) body(Sequences) { """ + @Suppress("UNCHECKED_CAST") return filter { it is R } as Sequence """ } @@ -307,6 +310,7 @@ fun specialJVM(): List { val result = arrayOfNulls(size) for (index in indices) result[index] = this[index] + @Suppress("CAST_NEVER_SUCCEEDS") return result as Array """ }