From 6c9321753988cdc7ee2b3e09d43a50a7cd9d67b1 Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Wed, 5 Feb 2020 17:50:39 +0900 Subject: [PATCH] Lift assignment out: don't report when expression is used as expression #KT-36357 Fixed --- .../LiftReturnOrAssignmentInspection.kt | 4 ++++ .../liftOut/ifToAssignment/usedAsExpression.kt | 8 ++++++++ .../liftOut/tryToAssignment/usedAsExpression.kt | 8 ++++++++ .../liftOut/whenToAssignment/usedAsExpression.kt | 7 +++++++ .../inspections/LocalInspectionTestGenerated.java | 15 +++++++++++++++ 5 files changed, 42 insertions(+) create mode 100644 idea/testData/inspectionsLocal/liftOut/ifToAssignment/usedAsExpression.kt create mode 100644 idea/testData/inspectionsLocal/liftOut/tryToAssignment/usedAsExpression.kt create mode 100644 idea/testData/inspectionsLocal/liftOut/whenToAssignment/usedAsExpression.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/LiftReturnOrAssignmentInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/LiftReturnOrAssignmentInspection.kt index 9f016104956..59b3b7dfd2b 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/LiftReturnOrAssignmentInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/LiftReturnOrAssignmentInspection.kt @@ -21,12 +21,15 @@ import com.intellij.codeInspection.ProblemHighlightType.GENERIC_ERROR_OR_WARNING import com.intellij.codeInspection.ProblemHighlightType.INFORMATION import com.intellij.openapi.project.Project import com.intellij.psi.PsiElement +import org.jetbrains.kotlin.idea.caches.resolve.analyze import org.jetbrains.kotlin.idea.intentions.branchedTransformations.BranchedFoldingUtils import org.jetbrains.kotlin.idea.intentions.branchedTransformations.isElseIf import org.jetbrains.kotlin.idea.intentions.branchedTransformations.lineCount import org.jetbrains.kotlin.psi.* import org.jetbrains.kotlin.psi.psiUtil.anyDescendantOfType import org.jetbrains.kotlin.psi.psiUtil.startOffset +import org.jetbrains.kotlin.resolve.bindingContextUtil.isUsedAsExpression +import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode class LiftReturnOrAssignmentInspection @JvmOverloads constructor(private val skipLongExpressions: Boolean = true) : AbstractKotlinInspection() { @@ -35,6 +38,7 @@ class LiftReturnOrAssignmentInspection @JvmOverloads constructor(private val ski object : KtVisitorVoid() { override fun visitExpression(expression: KtExpression) { val states = getState(expression, skipLongExpressions) ?: return + if (expression.isUsedAsExpression(expression.analyze(BodyResolveMode.PARTIAL_WITH_CFA))) return states.forEach { state -> registerProblem( expression, diff --git a/idea/testData/inspectionsLocal/liftOut/ifToAssignment/usedAsExpression.kt b/idea/testData/inspectionsLocal/liftOut/ifToAssignment/usedAsExpression.kt new file mode 100644 index 00000000000..55b9b87ba46 --- /dev/null +++ b/idea/testData/inspectionsLocal/liftOut/ifToAssignment/usedAsExpression.kt @@ -0,0 +1,8 @@ +// PROBLEM: none +var a = 5 + +fun foo() = if (true) { + a = 6 +} else { + a = 8 +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/liftOut/tryToAssignment/usedAsExpression.kt b/idea/testData/inspectionsLocal/liftOut/tryToAssignment/usedAsExpression.kt new file mode 100644 index 00000000000..533674e8318 --- /dev/null +++ b/idea/testData/inspectionsLocal/liftOut/tryToAssignment/usedAsExpression.kt @@ -0,0 +1,8 @@ +// PROBLEM: none +var a = 5 + +fun foo() = try { + a = 6 +} catch (e: Exception) { + a = 8 +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/liftOut/whenToAssignment/usedAsExpression.kt b/idea/testData/inspectionsLocal/liftOut/whenToAssignment/usedAsExpression.kt new file mode 100644 index 00000000000..cd5816b4294 --- /dev/null +++ b/idea/testData/inspectionsLocal/liftOut/whenToAssignment/usedAsExpression.kt @@ -0,0 +1,7 @@ +// PROBLEM: none +var a = 5 + +fun foo() = when { + true -> a = 6 + else -> a = 8 +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java index 63d8391dbd5..a5dca070b87 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -5389,6 +5389,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { public void testTypeMismatchMutableList2_ni() throws Exception { runTest("idea/testData/inspectionsLocal/liftOut/ifToAssignment/typeMismatchMutableList2_ni.kt"); } + + @TestMetadata("usedAsExpression.kt") + public void testUsedAsExpression() throws Exception { + runTest("idea/testData/inspectionsLocal/liftOut/ifToAssignment/usedAsExpression.kt"); + } } @TestMetadata("idea/testData/inspectionsLocal/liftOut/ifToReturn") @@ -5516,6 +5521,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { runTest("idea/testData/inspectionsLocal/liftOut/tryToAssignment/lambda.kt"); } + @TestMetadata("usedAsExpression.kt") + public void testUsedAsExpression() throws Exception { + runTest("idea/testData/inspectionsLocal/liftOut/tryToAssignment/usedAsExpression.kt"); + } + @TestMetadata("withUnmatchedAssignments.kt") public void testWithUnmatchedAssignments() throws Exception { runTest("idea/testData/inspectionsLocal/liftOut/tryToAssignment/withUnmatchedAssignments.kt"); @@ -5647,6 +5657,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { runTest("idea/testData/inspectionsLocal/liftOut/whenToAssignment/simpleWhenWithoutTerminatingAssignment.kt"); } + @TestMetadata("usedAsExpression.kt") + public void testUsedAsExpression() throws Exception { + runTest("idea/testData/inspectionsLocal/liftOut/whenToAssignment/usedAsExpression.kt"); + } + @TestMetadata("whenHasMissingCase.kt") public void testWhenHasMissingCase() throws Exception { runTest("idea/testData/inspectionsLocal/liftOut/whenToAssignment/whenHasMissingCase.kt");