"Redundant 'requireNotNull' or 'checkNotNull' call": don't remove first argument if function is used as expression

#KT-31404 Fixed
This commit is contained in:
Toshiaki Kameyama
2019-05-13 13:15:10 +09:00
committed by Dmitry Gridin
parent 574178882a
commit cf4471ba53
8 changed files with 47 additions and 2 deletions
@@ -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<KtCallExpression>() ?: 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()
}
}
}
@@ -0,0 +1,4 @@
// WITH_RUNTIME
fun test(i: Int) {
val a = <caret>requireNotNull(i)
}
@@ -0,0 +1,4 @@
// WITH_RUNTIME
fun test(i: Int) {
val a = i
}
@@ -0,0 +1,4 @@
// WITH_RUNTIME
fun test(i: Int) {
println(<caret>requireNotNull(i) { "" })
}
@@ -0,0 +1,4 @@
// WITH_RUNTIME
fun test(i: Int) {
println(i)
}
@@ -0,0 +1,4 @@
// WITH_RUNTIME
fun test(i: Int) {
"${<caret>checkNotNull(i)}"
}
@@ -0,0 +1,4 @@
// WITH_RUNTIME
fun test(i: Int) {
"${i}"
}
@@ -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")