Fix "surround with null check": place check correctly for unsafe call
So #KT-16928 Fixed
This commit is contained in:
@@ -57,6 +57,11 @@ class SurroundWithNullCheckFix(
|
|||||||
|
|
||||||
companion object : KotlinSingleIntentionActionFactory() {
|
companion object : KotlinSingleIntentionActionFactory() {
|
||||||
|
|
||||||
|
private fun KtExpression.hasAcceptableParent() = with (parent) {
|
||||||
|
this is KtBlockExpression || this.parent is KtIfExpression ||
|
||||||
|
this is KtWhenEntry || this.parent is KtLoopExpression
|
||||||
|
}
|
||||||
|
|
||||||
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
|
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
|
||||||
val element = diagnostic.psiElement
|
val element = diagnostic.psiElement
|
||||||
val expressionParent = element.getParentOfType<KtExpression>(strict = element is KtOperationReferenceExpression) ?: return null
|
val expressionParent = element.getParentOfType<KtExpression>(strict = element is KtOperationReferenceExpression) ?: return null
|
||||||
@@ -74,7 +79,7 @@ class SurroundWithNullCheckFix(
|
|||||||
if (!nullableExpression.isStable(context)) return null
|
if (!nullableExpression.isStable(context)) return null
|
||||||
|
|
||||||
val expressionTarget = expressionParent.getParentOfTypesAndPredicate(strict = false, parentClasses = KtExpression::class.java) {
|
val expressionTarget = expressionParent.getParentOfTypesAndPredicate(strict = false, parentClasses = KtExpression::class.java) {
|
||||||
!it.isUsedAsExpression(context)
|
!it.isUsedAsExpression(context) && it.hasAcceptableParent()
|
||||||
} ?: return null
|
} ?: return null
|
||||||
// Surround declaration (even of local variable) with null check is generally a bad idea
|
// Surround declaration (even of local variable) with null check is generally a bad idea
|
||||||
if (expressionTarget is KtDeclaration) return null
|
if (expressionTarget is KtDeclaration) return null
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
// "Surround with null check" "true"
|
||||||
|
|
||||||
|
fun foo(p: String?) {
|
||||||
|
Util.f1(Util.f2(p<caret>.length), 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
object Util {
|
||||||
|
fun f1(o: Any, p: Int): Any = o
|
||||||
|
fun f2(o: Any): Any = o
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
// "Surround with null check" "true"
|
||||||
|
|
||||||
|
fun foo(p: String?) {
|
||||||
|
if (p != null) {
|
||||||
|
Util.f1(Util.f2(p.length), 0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
object Util {
|
||||||
|
fun f1(o: Any, p: Int): Any = o
|
||||||
|
fun f2(o: Any): Any = o
|
||||||
|
}
|
||||||
@@ -9126,6 +9126,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
|||||||
doTest(fileName);
|
doTest(fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("objectNestedQualifiers.kt")
|
||||||
|
public void testObjectNestedQualifiers() throws Exception {
|
||||||
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/surroundWithNullCheck/objectNestedQualifiers.kt");
|
||||||
|
doTest(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("objectQualifier.kt")
|
@TestMetadata("objectQualifier.kt")
|
||||||
public void testObjectQualifier() throws Exception {
|
public void testObjectQualifier() throws Exception {
|
||||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/surroundWithNullCheck/objectQualifier.kt");
|
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/surroundWithNullCheck/objectQualifier.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user