From 8fd16f4a39bb2d3e17523956be57e00fc6b42e0e Mon Sep 17 00:00:00 2001 From: Roman Golyshev Date: Tue, 14 Jul 2020 19:38:57 +0300 Subject: [PATCH] Add contract to `checkWithAttachment` - This way it is more convenient to use with null checks --- .../jetbrains/kotlin/utils/KotlinExceptionWithAttachments.kt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/compiler/util/src/org/jetbrains/kotlin/utils/KotlinExceptionWithAttachments.kt b/compiler/util/src/org/jetbrains/kotlin/utils/KotlinExceptionWithAttachments.kt index 8abbcc9a365..0ad3b36d97f 100644 --- a/compiler/util/src/org/jetbrains/kotlin/utils/KotlinExceptionWithAttachments.kt +++ b/compiler/util/src/org/jetbrains/kotlin/utils/KotlinExceptionWithAttachments.kt @@ -7,6 +7,8 @@ package org.jetbrains.kotlin.utils import com.intellij.openapi.diagnostic.Attachment import com.intellij.openapi.diagnostic.ExceptionWithAttachments +import kotlin.contracts.ExperimentalContracts +import kotlin.contracts.contract open class KotlinExceptionWithAttachments : RuntimeException, ExceptionWithAttachments { private val attachments = mutableListOf() @@ -27,7 +29,10 @@ open class KotlinExceptionWithAttachments : RuntimeException, ExceptionWithAttac } } +@OptIn(ExperimentalContracts::class) inline fun checkWithAttachment(value: Boolean, lazyMessage: () -> String, attachments: (KotlinExceptionWithAttachments) -> Unit = {}) { + contract { returns() implies(value) } + if (!value) { val e = KotlinExceptionWithAttachments(lazyMessage()) attachments(e)