From fc4d95460d5c258e6bb8612ecd91aaf32259ba4b Mon Sep 17 00:00:00 2001 From: Roman Elizarov Date: Mon, 9 Nov 2020 11:26:56 +0300 Subject: [PATCH 1/2] Add lint warnings on SharedFlow operations that never complete Fixes #2340 Fixes #2368 --- .../common/src/flow/operators/Lint.kt | 62 ++++++++++++++++++- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/kotlinx-coroutines-core/common/src/flow/operators/Lint.kt b/kotlinx-coroutines-core/common/src/flow/operators/Lint.kt index 7a70fbf7f2..bd5960709e 100644 --- a/kotlinx-coroutines-core/common/src/flow/operators/Lint.kt +++ b/kotlinx-coroutines-core/common/src/flow/operators/Lint.kt @@ -2,12 +2,13 @@ * Copyright 2016-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. */ -@file:Suppress("unused") +@file:Suppress("unused", "INVISIBLE_REFERENCE", "INVISIBLE_MEMBER") package kotlinx.coroutines.flow import kotlinx.coroutines.* import kotlin.coroutines.* +import kotlin.internal.InlineOnly /** * Applying [cancellable][Flow.cancellable] to a [SharedFlow] has no effect. @@ -79,4 +80,61 @@ public fun FlowCollector<*>.cancel(cause: CancellationException? = null): Unit = replaceWith = ReplaceWith("currentCoroutineContext()") ) public val FlowCollector<*>.coroutineContext: CoroutineContext - get() = noImpl() \ No newline at end of file + get() = noImpl() + +@Deprecated( + message = "SharedFlow never completes, so this operator has no effect.", + level = DeprecationLevel.WARNING, + replaceWith = ReplaceWith("this") +) +@InlineOnly +public inline fun SharedFlow.catch(noinline action: suspend FlowCollector.(cause: Throwable) -> Unit): Flow = + (this as Flow).catch(action) + +@Deprecated( + message = "SharedFlow never completes, so this operator has no effect.", + level = DeprecationLevel.WARNING, + replaceWith = ReplaceWith("this") +) +@InlineOnly +public inline fun SharedFlow.retry( + retries: Long = Long.MAX_VALUE, + noinline predicate: suspend (cause: Throwable) -> Boolean = { true } +): Flow = + (this as Flow).retry(retries, predicate) + +@Deprecated( + message = "SharedFlow never completes, so this operator has no effect.", + level = DeprecationLevel.WARNING, + replaceWith = ReplaceWith("this") +) +@InlineOnly +public inline fun SharedFlow.retryWhen(noinline predicate: suspend FlowCollector.(cause: Throwable, attempt: Long) -> Boolean): Flow = + (this as Flow).retryWhen(predicate) + +@Deprecated( + message = "SharedFlow never completes, so this terminal operation never completes.", + level = DeprecationLevel.WARNING, + replaceWith = ReplaceWith("awaitCancellation()") +) +@InlineOnly +public suspend inline fun SharedFlow.toList(): List = + (this as Flow).toList() + +@Deprecated( + message = "SharedFlow never completes, so this terminal operation never completes.", + level = DeprecationLevel.WARNING, + replaceWith = ReplaceWith("awaitCancellation()") +) +@InlineOnly +public suspend inline fun SharedFlow.toSet(): Set = + (this as Flow).toSet() + +@Deprecated( + message = "SharedFlow never completes, so this terminal operation never completes.", + level = DeprecationLevel.WARNING, + replaceWith = ReplaceWith("awaitCancellation()") +) +@InlineOnly +public suspend inline fun SharedFlow.count(): Int = + (this as Flow).count() From 188ca593672e4e1298cc002ba6dff68ccb0d6335 Mon Sep 17 00:00:00 2001 From: Roman Elizarov Date: Mon, 16 Nov 2020 22:22:38 +0300 Subject: [PATCH 2/2] ~ remove awaitCancellation replacements --- .../common/src/flow/operators/Lint.kt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/kotlinx-coroutines-core/common/src/flow/operators/Lint.kt b/kotlinx-coroutines-core/common/src/flow/operators/Lint.kt index bd5960709e..9aa240d8a9 100644 --- a/kotlinx-coroutines-core/common/src/flow/operators/Lint.kt +++ b/kotlinx-coroutines-core/common/src/flow/operators/Lint.kt @@ -112,28 +112,28 @@ public inline fun SharedFlow.retry( public inline fun SharedFlow.retryWhen(noinline predicate: suspend FlowCollector.(cause: Throwable, attempt: Long) -> Boolean): Flow = (this as Flow).retryWhen(predicate) +@Suppress("DeprecatedCallableAddReplaceWith") @Deprecated( message = "SharedFlow never completes, so this terminal operation never completes.", - level = DeprecationLevel.WARNING, - replaceWith = ReplaceWith("awaitCancellation()") + level = DeprecationLevel.WARNING ) @InlineOnly public suspend inline fun SharedFlow.toList(): List = (this as Flow).toList() +@Suppress("DeprecatedCallableAddReplaceWith") @Deprecated( message = "SharedFlow never completes, so this terminal operation never completes.", - level = DeprecationLevel.WARNING, - replaceWith = ReplaceWith("awaitCancellation()") + level = DeprecationLevel.WARNING ) @InlineOnly public suspend inline fun SharedFlow.toSet(): Set = (this as Flow).toSet() +@Suppress("DeprecatedCallableAddReplaceWith") @Deprecated( message = "SharedFlow never completes, so this terminal operation never completes.", - level = DeprecationLevel.WARNING, - replaceWith = ReplaceWith("awaitCancellation()") + level = DeprecationLevel.WARNING ) @InlineOnly public suspend inline fun SharedFlow.count(): Int =