stdlib: Suppress/fix warnings in stdlib

This patch fixes redundant safe calls/casts/type projections
and suppresses some casts which are actually safe but reported
as unsafe.
This commit is contained in:
Ilya Matveev
2017-04-18 14:13:44 +07:00
committed by ilmat192
parent fcece669ec
commit b9b8c6e192
8 changed files with 27 additions and 12 deletions
+10 -11
View File
@@ -5058,6 +5058,7 @@ public inline fun <T> Array<out T>.single(predicate: (T) -> Boolean): T {
} }
} }
if (!found) throw NoSuchElementException("Array contains no element matching the predicate.") if (!found) throw NoSuchElementException("Array contains no element matching the predicate.")
@Suppress("UNCHECKED_CAST")
return single as T return single as T
} }
@@ -10390,7 +10391,7 @@ public inline fun CharArray.copyOf(): CharArray {
*/ */
@kotlin.internal.InlineOnly @kotlin.internal.InlineOnly
public inline fun <T> Array<T>.copyOf(newSize: Int): Array<T?> { public inline fun <T> Array<T>.copyOf(newSize: Int): Array<T?> {
return copyOfNulls(newSize) as Array<T?> return copyOfNulls(newSize)
} }
/** /**
@@ -11443,8 +11444,6 @@ public inline fun <T> Array<out T>.subarrayContentToString(offset: Int, length:
public infix fun <T> Array<out T>.contentDeepEquals(other: Array<out T>): Boolean { public infix fun <T> Array<out T>.contentDeepEquals(other: Array<out T>): Boolean {
if (this === other) if (this === other)
return true return true
if (other == null)
return false
if (size != other.size) if (size != other.size)
return false return false
@@ -11758,7 +11757,7 @@ public fun <T> Array<out T>.contentHashCode(): Int {
public fun ByteArray.contentHashCode(): Int { public fun ByteArray.contentHashCode(): Int {
var result = 1 var result = 1
for (element in this) for (element in this)
result = 31 * result + (element?.hashCode() ?: 0) result = 31 * result + element.hashCode()
return result return result
} }
@@ -11769,7 +11768,7 @@ public fun ByteArray.contentHashCode(): Int {
public fun ShortArray.contentHashCode(): Int { public fun ShortArray.contentHashCode(): Int {
var result = 1 var result = 1
for (element in this) for (element in this)
result = 31 * result + (element?.hashCode() ?: 0) result = 31 * result + element.hashCode()
return result return result
} }
@@ -11780,7 +11779,7 @@ public fun ShortArray.contentHashCode(): Int {
public fun IntArray.contentHashCode(): Int { public fun IntArray.contentHashCode(): Int {
var result = 1 var result = 1
for (element in this) for (element in this)
result = 31 * result + (element?.hashCode() ?: 0) result = 31 * result + element.hashCode()
return result return result
} }
@@ -11791,7 +11790,7 @@ public fun IntArray.contentHashCode(): Int {
public fun LongArray.contentHashCode(): Int { public fun LongArray.contentHashCode(): Int {
var result = 1 var result = 1
for (element in this) for (element in this)
result = 31 * result + (element?.hashCode() ?: 0) result = 31 * result + element.hashCode()
return result return result
} }
@@ -11802,7 +11801,7 @@ public fun LongArray.contentHashCode(): Int {
public fun FloatArray.contentHashCode(): Int { public fun FloatArray.contentHashCode(): Int {
var result = 1 var result = 1
for (element in this) for (element in this)
result = 31 * result + (element?.hashCode() ?: 0) result = 31 * result + element.hashCode()
return result return result
} }
@@ -11813,7 +11812,7 @@ public fun FloatArray.contentHashCode(): Int {
public fun DoubleArray.contentHashCode(): Int { public fun DoubleArray.contentHashCode(): Int {
var result = 1 var result = 1
for (element in this) for (element in this)
result = 31 * result + (element?.hashCode() ?: 0) result = 31 * result + element.hashCode()
return result return result
} }
@@ -11824,7 +11823,7 @@ public fun DoubleArray.contentHashCode(): Int {
public fun BooleanArray.contentHashCode(): Int { public fun BooleanArray.contentHashCode(): Int {
var result = 1 var result = 1
for (element in this) for (element in this)
result = 31 * result + (element?.hashCode() ?: 0) result = 31 * result + element.hashCode()
return result return result
} }
@@ -11835,7 +11834,7 @@ public fun BooleanArray.contentHashCode(): Int {
public fun CharArray.contentHashCode(): Int { public fun CharArray.contentHashCode(): Int {
var result = 1 var result = 1
for (element in this) for (element in this)
result = 31 * result + (element?.hashCode() ?: 0) result = 31 * result + element.hashCode()
return result return result
} }
@@ -44,6 +44,7 @@ public abstract class AbstractIterator<T>: Iterator<T> {
override fun next(): T { override fun next(): T {
if (!hasNext()) throw NoSuchElementException() if (!hasNext()) throw NoSuchElementException()
state = State.NotReady state = State.NotReady
@Suppress("UNCHECKED_CAST")
return nextValue as T return nextValue as T
} }
@@ -69,6 +69,7 @@ internal fun <E, T> collectionToArray(collection: Collection<E>, array: Array<T>
} }
var i = 0 var i = 0
for (v in collection) { for (v in collection) {
@Suppress("UNCHECKED_CAST")
toArray[i] = v as T toArray[i] = v as T
i++ i++
} }
@@ -796,6 +796,7 @@ public inline fun <T> Iterable<T>.last(predicate: (T) -> Boolean): T {
} }
} }
if (!found) throw NoSuchElementException("Collection contains no element matching the predicate.") if (!found) throw NoSuchElementException("Collection contains no element matching the predicate.")
@Suppress("UNCHECKED_CAST")
return last as T return last as T
} }
@@ -929,6 +930,7 @@ public inline fun <T> Iterable<T>.single(predicate: (T) -> Boolean): T {
} }
} }
if (!found) throw NoSuchElementException("Collection contains no element matching the predicate.") if (!found) throw NoSuchElementException("Collection contains no element matching the predicate.")
@Suppress("UNCHECKED_CAST")
return single as T return single as T
} }
@@ -109,6 +109,7 @@ public inline fun <T, K, R, M : MutableMap<in K, R>> Grouping<T, K>.aggregateTo(
* @return a [Map] associating the key of each group with the result of accumulating the group elements. * @return a [Map] associating the key of each group with the result of accumulating the group elements.
*/ */
@SinceKotlin("1.1") @SinceKotlin("1.1")
@Suppress("UNCHECKED_CAST")
public inline fun <T, K, R> Grouping<T, K>.fold( public inline fun <T, K, R> Grouping<T, K>.fold(
initialValueSelector: (key: K, element: T) -> R, initialValueSelector: (key: K, element: T) -> R,
operation: (key: K, accumulator: R, element: T) -> R operation: (key: K, accumulator: R, element: T) -> R
@@ -137,6 +138,7 @@ public inline fun <T, K, R> Grouping<T, K>.fold(
* @return the [destination] map associating the key of each group with the result of accumulating the group elements. * @return the [destination] map associating the key of each group with the result of accumulating the group elements.
*/ */
@SinceKotlin("1.1") @SinceKotlin("1.1")
@Suppress("UNCHECKED_CAST")
public inline fun <T, K, R, M : MutableMap<in K, R>> Grouping<T, K>.foldTo( public inline fun <T, K, R, M : MutableMap<in K, R>> Grouping<T, K>.foldTo(
destination: M, destination: M,
initialValueSelector: (key: K, element: T) -> R, initialValueSelector: (key: K, element: T) -> R,
@@ -157,6 +159,7 @@ public inline fun <T, K, R, M : MutableMap<in K, R>> Grouping<T, K>.foldTo(
* @return a [Map] associating the key of each group with the result of accumulating the group elements. * @return a [Map] associating the key of each group with the result of accumulating the group elements.
*/ */
@SinceKotlin("1.1") @SinceKotlin("1.1")
@Suppress("UNCHECKED_CAST")
public inline fun <T, K, R> Grouping<T, K>.fold( public inline fun <T, K, R> Grouping<T, K>.fold(
initialValue: R, initialValue: R,
operation: (accumulator: R, element: T) -> R operation: (accumulator: R, element: T) -> R
@@ -179,6 +182,7 @@ public inline fun <T, K, R> Grouping<T, K>.fold(
* @return the [destination] map associating the key of each group with the result of accumulating the group elements. * @return the [destination] map associating the key of each group with the result of accumulating the group elements.
*/ */
@SinceKotlin("1.1") @SinceKotlin("1.1")
@Suppress("UNCHECKED_CAST")
public inline fun <T, K, R, M : MutableMap<in K, R>> Grouping<T, K>.foldTo( public inline fun <T, K, R, M : MutableMap<in K, R>> Grouping<T, K>.foldTo(
destination: M, destination: M,
initialValue: R, initialValue: R,
@@ -202,6 +206,7 @@ public inline fun <T, K, R, M : MutableMap<in K, R>> Grouping<T, K>.foldTo(
* @return a [Map] associating the key of each group with the result of accumulating the group elements. * @return a [Map] associating the key of each group with the result of accumulating the group elements.
*/ */
@SinceKotlin("1.1") @SinceKotlin("1.1")
@Suppress("UNCHECKED_CAST")
public inline fun <S, T : S, K> Grouping<T, K>.reduce( public inline fun <S, T : S, K> Grouping<T, K>.reduce(
operation: (key: K, accumulator: S, element: T) -> S operation: (key: K, accumulator: S, element: T) -> S
): Map<K, S> = ): Map<K, S> =
@@ -227,6 +232,7 @@ public inline fun <S, T : S, K> Grouping<T, K>.reduce(
* @return the [destination] map associating the key of each group with the result of accumulating the group elements. * @return the [destination] map associating the key of each group with the result of accumulating the group elements.
*/ */
@SinceKotlin("1.1") @SinceKotlin("1.1")
@Suppress("UNCHECKED_CAST")
public inline fun <S, T : S, K, M : MutableMap<in K, S>> Grouping<T, K>.reduceTo( public inline fun <S, T : S, K, M : MutableMap<in K, S>> Grouping<T, K>.reduceTo(
destination: M, destination: M,
operation: (key: K, accumulator: S, element: T) -> S operation: (key: K, accumulator: S, element: T) -> S
@@ -45,7 +45,7 @@ class HashMap<K, V> private constructor(
INITIAL_MAX_PROBE_DISTANCE, INITIAL_MAX_PROBE_DISTANCE,
0) 0)
constructor(m: Map<out K, out V>) : this(m.size) { constructor(m: Map<out K, V>) : this(m.size) {
putAll(m) putAll(m)
} }
@@ -224,6 +224,7 @@ internal inline fun <K, V> Map<K, V>.getOrElseNullable(key: K, defaultValue: ()
if (value == null && !containsKey(key)) { if (value == null && !containsKey(key)) {
return defaultValue() return defaultValue()
} else { } else {
@Suppress("UNCHECKED_CAST")
return value as V return value as V
} }
} }
@@ -132,6 +132,7 @@ internal class FilteringSequence<T>(private val sequence: Sequence<T>,
val result = nextItem val result = nextItem
nextItem = null nextItem = null
nextState = -1 nextState = -1
@Suppress("UNCHECKED_CAST")
return result as T return result as T
} }
@@ -388,6 +389,7 @@ constructor(private val sequence: Sequence<T>,
calcNext() // will change nextState calcNext() // will change nextState
if (nextState == 0) if (nextState == 0)
throw NoSuchElementException() throw NoSuchElementException()
@Suppress("UNCHECKED_CAST")
val result = nextItem as T val result = nextItem as T
// Clean next to avoid keeping reference on yielded instance // Clean next to avoid keeping reference on yielded instance
@@ -474,6 +476,7 @@ constructor(private val sequence: Sequence<T>,
drop() drop()
if (dropState == 1) { if (dropState == 1) {
@Suppress("UNCHECKED_CAST")
val result = nextItem as T val result = nextItem as T
nextItem = null nextItem = null
dropState = 0 dropState = 0
@@ -787,6 +790,7 @@ public inline fun <T> Sequence<T>.last(predicate: (T) -> Boolean): T {
} }
} }
if (!found) throw NoSuchElementException("Sequence contains no element matching the predicate.") if (!found) throw NoSuchElementException("Sequence contains no element matching the predicate.")
@Suppress("UNCHECKED_CAST")
return last as T return last as T
} }
@@ -857,6 +861,7 @@ public inline fun <T> Sequence<T>.single(predicate: (T) -> Boolean): T {
} }
} }
if (!found) throw NoSuchElementException("Sequence contains no element matching the predicate.") if (!found) throw NoSuchElementException("Sequence contains no element matching the predicate.")
@Suppress("UNCHECKED_CAST")
return single as T return single as T
} }