From bc27a192f24714fe1e796123c68582363a3eb7b6 Mon Sep 17 00:00:00 2001 From: Vladimir Dolzhenko Date: Mon, 17 Nov 2025 11:43:24 +0100 Subject: [PATCH] [kotlin] KTIJ-36447 Fix handling nested binary expression GitOrigin-RevId: bdf722a2bd6f916ba7e6b80004aac0890dc9397c --- .../k2/codeinsight/fixes/NoReturnValueFactory.kt | 15 +++++++-------- .../fixes/HighLevelQuickFixTestGenerated.java | 5 +++++ .../idea/quickfix/K1QuickFixTestGenerated.java | 5 +++++ .../nestedBinaryExpressions.kt | 10 ++++++++++ .../nestedBinaryExpressions.kt.after | 10 ++++++++++ 5 files changed, 37 insertions(+), 8 deletions(-) create mode 100644 plugins/kotlin/idea/tests/testData/quickfix/explicitlyIgnoreReturnValue/nestedBinaryExpressions.kt create mode 100644 plugins/kotlin/idea/tests/testData/quickfix/explicitlyIgnoreReturnValue/nestedBinaryExpressions.kt.after diff --git a/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/NoReturnValueFactory.kt b/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/NoReturnValueFactory.kt index a69ebdbf7037..f72319f2c82c 100644 --- a/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/NoReturnValueFactory.kt +++ b/plugins/kotlin/code-insight/fixes-k2/src/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/NoReturnValueFactory.kt @@ -4,12 +4,12 @@ package org.jetbrains.kotlin.idea.k2.codeinsight.fixes import com.intellij.modcommand.ActionContext import com.intellij.modcommand.ModPsiUpdater import com.intellij.modcommand.PsiUpdateModCommandAction -import com.intellij.openapi.diagnostic.Logger import com.intellij.psi.PsiElement import org.jetbrains.kotlin.analysis.api.fir.diagnostics.KaFirDiagnostic import org.jetbrains.kotlin.idea.base.resources.KotlinBundle import org.jetbrains.kotlin.idea.codeinsight.api.applicators.fixes.KotlinQuickFixFactory import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.utils.KotlinExceptionWithAttachments internal object NoReturnValueFactory { val noReturnValue = @@ -65,8 +65,9 @@ internal object NoReturnValueFactory { factory.createExpression("{$baseExpressionText}") } else -> { - LOG.error("Unknown parent class: ${parent?.javaClass?.name}. The result expression could be incorrect.") - factory.createExpression("{$baseExpressionText}") + throw KotlinExceptionWithAttachments("Unknown parent class: ${parent?.javaClass?.name}.") + .withPsiAttachment("element.kt", element) + .withPsiAttachment("file.kt", element.containingFile) } } return newExpression @@ -74,14 +75,12 @@ internal object NoReturnValueFactory { private fun findParentOrOuterMostParentheses(element: KtElement): PsiElement? { var parent: PsiElement? = element.parent - while (parent is KtParenthesizedExpression) { + while (parent is KtParenthesizedExpression || parent is KtBinaryExpression) { val parentOfParent = parent.parent - if (parentOfParent !is KtParenthesizedExpression) break + if (parentOfParent !is KtParenthesizedExpression && parentOfParent !is KtBinaryExpression) break parent = parentOfParent } return parent } } -} - -private val LOG = Logger.getInstance(NoReturnValueFactory::class.java) \ No newline at end of file +} \ No newline at end of file diff --git a/plugins/kotlin/code-insight/fixes-k2/tests/test/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/HighLevelQuickFixTestGenerated.java b/plugins/kotlin/code-insight/fixes-k2/tests/test/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/HighLevelQuickFixTestGenerated.java index 323fbaee2e6a..6b940adbe1a2 100644 --- a/plugins/kotlin/code-insight/fixes-k2/tests/test/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/HighLevelQuickFixTestGenerated.java +++ b/plugins/kotlin/code-insight/fixes-k2/tests/test/org/jetbrains/kotlin/idea/k2/codeinsight/fixes/HighLevelQuickFixTestGenerated.java @@ -8034,6 +8034,11 @@ public abstract class HighLevelQuickFixTestGenerated extends AbstractHighLevelQu runTest("../../../idea/tests/testData/quickfix/explicitlyIgnoreReturnValue/ignoredWithAnnotation.kt"); } + @TestMetadata("nestedBinaryExpressions.kt") + public void testNestedBinaryExpressions() throws Exception { + runTest("../../../idea/tests/testData/quickfix/explicitlyIgnoreReturnValue/nestedBinaryExpressions.kt"); + } + @TestMetadata("parentheses.kt") public void testParentheses() throws Exception { runTest("../../../idea/tests/testData/quickfix/explicitlyIgnoreReturnValue/parentheses.kt"); diff --git a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/quickfix/K1QuickFixTestGenerated.java b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/quickfix/K1QuickFixTestGenerated.java index 4de5c125165d..14fcb7a0e843 100644 --- a/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/quickfix/K1QuickFixTestGenerated.java +++ b/plugins/kotlin/idea/tests/test/org/jetbrains/kotlin/idea/quickfix/K1QuickFixTestGenerated.java @@ -9787,6 +9787,11 @@ public abstract class K1QuickFixTestGenerated extends AbstractK1QuickFixTest { runTest("testData/quickfix/explicitlyIgnoreReturnValue/ignoredWithAnnotation.kt"); } + @TestMetadata("nestedBinaryExpressions.kt") + public void testNestedBinaryExpressions() throws Exception { + runTest("testData/quickfix/explicitlyIgnoreReturnValue/nestedBinaryExpressions.kt"); + } + @TestMetadata("parentheses.kt") public void testParentheses() throws Exception { runTest("testData/quickfix/explicitlyIgnoreReturnValue/parentheses.kt"); diff --git a/plugins/kotlin/idea/tests/testData/quickfix/explicitlyIgnoreReturnValue/nestedBinaryExpressions.kt b/plugins/kotlin/idea/tests/testData/quickfix/explicitlyIgnoreReturnValue/nestedBinaryExpressions.kt new file mode 100644 index 000000000000..56d178807dae --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/quickfix/explicitlyIgnoreReturnValue/nestedBinaryExpressions.kt @@ -0,0 +1,10 @@ +// "Explicitly ignore return value" "true" +// LANGUAGE_VERSION: 2.2 +// COMPILER_ARGUMENTS: -Xreturn-value-checker=full +fun someFunction() { + someFunctionValue() ?: (someFunctionValue() ?: someFunctionValue() ?: error("failure")) +} +fun someFunctionValue(): Int? = 1 + +// IGNORE_K1 +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.k2.codeinsight.fixes.NoReturnValueFactory$UnderscoreValueFix \ No newline at end of file diff --git a/plugins/kotlin/idea/tests/testData/quickfix/explicitlyIgnoreReturnValue/nestedBinaryExpressions.kt.after b/plugins/kotlin/idea/tests/testData/quickfix/explicitlyIgnoreReturnValue/nestedBinaryExpressions.kt.after new file mode 100644 index 000000000000..dc9b7c2184b3 --- /dev/null +++ b/plugins/kotlin/idea/tests/testData/quickfix/explicitlyIgnoreReturnValue/nestedBinaryExpressions.kt.after @@ -0,0 +1,10 @@ +// "Explicitly ignore return value" "true" +// LANGUAGE_VERSION: 2.2 +// COMPILER_ARGUMENTS: -Xreturn-value-checker=full +fun someFunction() { + val _ = someFunctionValue() ?: (someFunctionValue() ?: someFunctionValue() ?: error("failure")) +} +fun someFunctionValue(): Int? = 1 + +// IGNORE_K1 +// FUS_K2_QUICKFIX_NAME: org.jetbrains.kotlin.idea.k2.codeinsight.fixes.NoReturnValueFactory$UnderscoreValueFix \ No newline at end of file