diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/SafeAccessToIfThenIntention.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/SafeAccessToIfThenIntention.kt index f855fff30f2..bbd88693e39 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/SafeAccessToIfThenIntention.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/branchedTransformations/intentions/SafeAccessToIfThenIntention.kt @@ -20,11 +20,14 @@ import com.intellij.codeInsight.intention.LowPriorityAction import com.intellij.openapi.editor.Editor import com.intellij.openapi.util.TextRange import org.jetbrains.kotlin.idea.caches.resolve.analyze +import org.jetbrains.kotlin.idea.core.replaced import org.jetbrains.kotlin.idea.intentions.SelfTargetingRangeIntention import org.jetbrains.kotlin.idea.intentions.branchedTransformations.convertToIfNotNullExpression import org.jetbrains.kotlin.idea.intentions.branchedTransformations.introduceValueForCondition import org.jetbrains.kotlin.idea.intentions.branchedTransformations.isStable +import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.psi.psiUtil.findDescendantOfType import org.jetbrains.kotlin.resolve.bindingContextUtil.isUsedAsStatement class SafeAccessToIfThenIntention : SelfTargetingRangeIntention(KtSafeQualifiedExpression::class.java, "Replace safe access expression with 'if' expression"), LowPriorityAction { @@ -43,12 +46,25 @@ class SafeAccessToIfThenIntention : SelfTargetingRangeIntention.bar = Bar() +} \ No newline at end of file diff --git a/idea/testData/intentions/branched/safeAccessToIfThen/assignment.kt.after b/idea/testData/intentions/branched/safeAccessToIfThen/assignment.kt.after new file mode 100644 index 00000000000..fc12c510e33 --- /dev/null +++ b/idea/testData/intentions/branched/safeAccessToIfThen/assignment.kt.after @@ -0,0 +1,9 @@ +class Foo { + var bar: Bar? = null +} +class Bar { + var baz = 1 +} +fun test(foo: Foo?) { + if (foo != null) foo.bar = Bar() +} \ No newline at end of file diff --git a/idea/testData/intentions/branched/safeAccessToIfThen/assignment2.kt b/idea/testData/intentions/branched/safeAccessToIfThen/assignment2.kt new file mode 100644 index 00000000000..d641c88d581 --- /dev/null +++ b/idea/testData/intentions/branched/safeAccessToIfThen/assignment2.kt @@ -0,0 +1,9 @@ +class Foo { + var bar: Bar? = null +} +class Bar { + var baz = 1 +} +fun test(foo: Foo?) { + foo?.bar?.baz = 2 +} \ No newline at end of file diff --git a/idea/testData/intentions/branched/safeAccessToIfThen/assignment2.kt.after b/idea/testData/intentions/branched/safeAccessToIfThen/assignment2.kt.after new file mode 100644 index 00000000000..2af135c411c --- /dev/null +++ b/idea/testData/intentions/branched/safeAccessToIfThen/assignment2.kt.after @@ -0,0 +1,9 @@ +class Foo { + var bar: Bar? = null +} +class Bar { + var baz = 1 +} +fun test(foo: Foo?) { + (if (foo != null) foo.bar else null)?.baz = 2 +} \ No newline at end of file diff --git a/idea/testData/intentions/branched/safeAccessToIfThen/assignment3.kt b/idea/testData/intentions/branched/safeAccessToIfThen/assignment3.kt new file mode 100644 index 00000000000..a67d8b47afa --- /dev/null +++ b/idea/testData/intentions/branched/safeAccessToIfThen/assignment3.kt @@ -0,0 +1,9 @@ +class Foo { + var bar: Bar? = null +} +class Bar { + var baz = 1 +} +fun test(foo: Foo?) { + foo?.bar?.baz = 2 +} \ No newline at end of file diff --git a/idea/testData/intentions/branched/safeAccessToIfThen/assignment3.kt.after b/idea/testData/intentions/branched/safeAccessToIfThen/assignment3.kt.after new file mode 100644 index 00000000000..d88a4bda1fa --- /dev/null +++ b/idea/testData/intentions/branched/safeAccessToIfThen/assignment3.kt.after @@ -0,0 +1,10 @@ +class Foo { + var bar: Bar? = null +} +class Bar { + var baz = 1 +} +fun test(foo: Foo?) { + val bar = foo?.bar + if (bar != null) bar.baz = 2 +} \ No newline at end of file diff --git a/idea/testData/intentions/branched/safeAccessToIfThen/equality.kt b/idea/testData/intentions/branched/safeAccessToIfThen/equality.kt new file mode 100644 index 00000000000..5c89cf7fa72 --- /dev/null +++ b/idea/testData/intentions/branched/safeAccessToIfThen/equality.kt @@ -0,0 +1,9 @@ +class Foo { + var bar: Bar? = null +} +class Bar { + var baz = 1 +} +fun test(foo: Foo?) { + foo?.bar == null +} \ No newline at end of file diff --git a/idea/testData/intentions/branched/safeAccessToIfThen/equality.kt.after b/idea/testData/intentions/branched/safeAccessToIfThen/equality.kt.after new file mode 100644 index 00000000000..2d2d50d5527 --- /dev/null +++ b/idea/testData/intentions/branched/safeAccessToIfThen/equality.kt.after @@ -0,0 +1,9 @@ +class Foo { + var bar: Bar? = null +} +class Bar { + var baz = 1 +} +fun test(foo: Foo?) { + (if (foo != null) foo.bar else null) == null +} \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java index 8b4b54ec681..dbf998e52e2 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/intentions/IntentionTestGenerated.java @@ -2344,6 +2344,24 @@ public class IntentionTestGenerated extends AbstractIntentionTest { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/branched/safeAccessToIfThen"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true); } + @TestMetadata("assignment.kt") + public void testAssignment() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/safeAccessToIfThen/assignment.kt"); + doTest(fileName); + } + + @TestMetadata("assignment2.kt") + public void testAssignment2() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/safeAccessToIfThen/assignment2.kt"); + doTest(fileName); + } + + @TestMetadata("assignment3.kt") + public void testAssignment3() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/safeAccessToIfThen/assignment3.kt"); + doTest(fileName); + } + @TestMetadata("binaryExpressionLhs.kt") public void testBinaryExpressionLhs() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/safeAccessToIfThen/binaryExpressionLhs.kt"); @@ -2368,6 +2386,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest { doTest(fileName); } + @TestMetadata("equality.kt") + public void testEquality() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/safeAccessToIfThen/equality.kt"); + doTest(fileName); + } + @TestMetadata("localValAsReceiver.kt") public void testLocalValAsReceiver() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/branched/safeAccessToIfThen/localValAsReceiver.kt");