diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantRequireNotNullCallInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantRequireNotNullCallInspection.kt index 54ff96371be..9eecc6910fd 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantRequireNotNullCallInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantRequireNotNullCallInspection.kt @@ -24,6 +24,7 @@ import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType import org.jetbrains.kotlin.psi.psiUtil.referenceExpression import org.jetbrains.kotlin.resolve.BindingContext import org.jetbrains.kotlin.resolve.bindingContextUtil.getDataFlowInfoBefore +import org.jetbrains.kotlin.resolve.bindingContextUtil.isUsedAsExpression import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValueFactory import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode @@ -68,7 +69,12 @@ private class RemoveRequireNotNullCallFix(private val functionName: String) : Lo override fun applyFix(project: Project, descriptor: ProblemDescriptor) { val callExpression = descriptor.psiElement.getStrictParentOfType() ?: return - val qualifiedExpression = callExpression.getQualifiedExpressionForSelector() - (qualifiedExpression ?: callExpression).delete() + val argument = callExpression.valueArguments.firstOrNull()?.getArgumentExpression() ?: return + val target = callExpression.getQualifiedExpressionForSelector() ?: callExpression + if (callExpression.isUsedAsExpression(callExpression.analyze(BodyResolveMode.PARTIAL))) { + target.replace(argument) + } else { + target.delete() + } } } \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/redundantRequireNotNullCall/usedAsExpression.kt b/idea/testData/inspectionsLocal/redundantRequireNotNullCall/usedAsExpression.kt new file mode 100644 index 00000000000..3426b59c18d --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantRequireNotNullCall/usedAsExpression.kt @@ -0,0 +1,4 @@ +// WITH_RUNTIME +fun test(i: Int) { + val a = requireNotNull(i) +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/redundantRequireNotNullCall/usedAsExpression.kt.after b/idea/testData/inspectionsLocal/redundantRequireNotNullCall/usedAsExpression.kt.after new file mode 100644 index 00000000000..05cf7bb1be7 --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantRequireNotNullCall/usedAsExpression.kt.after @@ -0,0 +1,4 @@ +// WITH_RUNTIME +fun test(i: Int) { + val a = i +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/redundantRequireNotNullCall/usedAsExpression2.kt b/idea/testData/inspectionsLocal/redundantRequireNotNullCall/usedAsExpression2.kt new file mode 100644 index 00000000000..bad7177963d --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantRequireNotNullCall/usedAsExpression2.kt @@ -0,0 +1,4 @@ +// WITH_RUNTIME +fun test(i: Int) { + println(requireNotNull(i) { "" }) +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/redundantRequireNotNullCall/usedAsExpression2.kt.after b/idea/testData/inspectionsLocal/redundantRequireNotNullCall/usedAsExpression2.kt.after new file mode 100644 index 00000000000..fd9614c06f1 --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantRequireNotNullCall/usedAsExpression2.kt.after @@ -0,0 +1,4 @@ +// WITH_RUNTIME +fun test(i: Int) { + println(i) +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/redundantRequireNotNullCall/usedAsExpression3.kt b/idea/testData/inspectionsLocal/redundantRequireNotNullCall/usedAsExpression3.kt new file mode 100644 index 00000000000..93e760f0c3e --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantRequireNotNullCall/usedAsExpression3.kt @@ -0,0 +1,4 @@ +// WITH_RUNTIME +fun test(i: Int) { + "${checkNotNull(i)}" +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/redundantRequireNotNullCall/usedAsExpression3.kt.after b/idea/testData/inspectionsLocal/redundantRequireNotNullCall/usedAsExpression3.kt.after new file mode 100644 index 00000000000..cf6238dd680 --- /dev/null +++ b/idea/testData/inspectionsLocal/redundantRequireNotNullCall/usedAsExpression3.kt.after @@ -0,0 +1,4 @@ +// WITH_RUNTIME +fun test(i: Int) { + "${i}" +} \ 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 d1e734f1c45..ee975e9721c 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -5779,6 +5779,21 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { public void testRequire() throws Exception { runTest("idea/testData/inspectionsLocal/redundantRequireNotNullCall/require.kt"); } + + @TestMetadata("usedAsExpression.kt") + public void testUsedAsExpression() throws Exception { + runTest("idea/testData/inspectionsLocal/redundantRequireNotNullCall/usedAsExpression.kt"); + } + + @TestMetadata("usedAsExpression2.kt") + public void testUsedAsExpression2() throws Exception { + runTest("idea/testData/inspectionsLocal/redundantRequireNotNullCall/usedAsExpression2.kt"); + } + + @TestMetadata("usedAsExpression3.kt") + public void testUsedAsExpression3() throws Exception { + runTest("idea/testData/inspectionsLocal/redundantRequireNotNullCall/usedAsExpression3.kt"); + } } @TestMetadata("idea/testData/inspectionsLocal/redundantReturnLabel")