"Add non-null asserted (!!) call": Fix for array access expression (KT-27071)

^KT-27071 Fixed
This commit is contained in:
Natalia Selezneva
2018-09-26 18:08:03 +09:00
parent 90da274eaa
commit 1b715ab253
10 changed files with 73 additions and 2 deletions
@@ -27,6 +27,7 @@ import org.jetbrains.kotlin.descriptors.ClassifierDescriptor
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.diagnostics.Errors.UNSAFE_CALL
import org.jetbrains.kotlin.idea.KotlinBundle
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.caches.resolve.findModuleDescriptor
@@ -114,7 +115,7 @@ class AddExclExclCallFix(psiElement: PsiElement, val checkImplicitReceivers: Boo
return (psiElement.prevSibling as? KtExpression).expressionForCall()
}
return when (psiElement) {
is KtArrayAccessExpression -> psiElement.arrayExpression.expressionForCall()
is KtArrayAccessExpression -> psiElement.expressionForCall()
is KtOperationReferenceExpression -> {
val parent = psiElement.parent
when (parent) {
@@ -152,7 +153,13 @@ class AddExclExclCallFix(psiElement: PsiElement, val checkImplicitReceivers: Boo
}
companion object : KotlinSingleIntentionActionFactory() {
override fun createAction(diagnostic: Diagnostic): IntentionAction = AddExclExclCallFix(diagnostic.psiElement)
override fun createAction(diagnostic: Diagnostic): IntentionAction {
val psiElement = diagnostic.psiElement
if (diagnostic.factory == UNSAFE_CALL && psiElement is KtArrayAccessExpression) {
psiElement.arrayExpression?.let { return AddExclExclCallFix(it) }
}
return AddExclExclCallFix(psiElement)
}
}
}
+5
View File
@@ -0,0 +1,5 @@
// "Add non-null asserted (!!) call" "true"
fun foo(a: Array<String>?): String {
return <caret>a[0]
}
+5
View File
@@ -0,0 +1,5 @@
// "Add non-null asserted (!!) call" "true"
fun foo(a: Array<String>?): String {
return a!![0]
}
+5
View File
@@ -0,0 +1,5 @@
// "Add non-null asserted (!!) call" "true"
fun foo(a: Array<String?>): String {
return <caret>a[0]
}
@@ -0,0 +1,5 @@
// "Add non-null asserted (!!) call" "true"
fun foo(a: Array<String?>): String {
return a[0]!!
}
+5
View File
@@ -0,0 +1,5 @@
// "Add non-null asserted (!!) call" "true"
fun foo(a: Array<String?>) {
a[0]<caret>.length
}
@@ -0,0 +1,5 @@
// "Add non-null asserted (!!) call" "true"
fun foo(a: Array<String?>) {
a[0]!!.length
}
+7
View File
@@ -0,0 +1,7 @@
// "Add non-null asserted (!!) call" "true"
// SHOULD_BE_AVAILABLE_AFTER_EXECUTION
// ERROR: Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type Array<String?>?
fun foo(a: Array<String?>?): String {
return <caret>a[0]
}
@@ -0,0 +1,7 @@
// "Add non-null asserted (!!) call" "true"
// SHOULD_BE_AVAILABLE_AFTER_EXECUTION
// ERROR: Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type Array<String?>?
fun foo(a: Array<String?>?): String {
return a[0]!!
}
@@ -516,6 +516,26 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/addExclExclCall"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), TargetBackend.ANY, true);
}
@TestMetadata("array.kt")
public void testArray() throws Exception {
runTest("idea/testData/quickfix/addExclExclCall/array.kt");
}
@TestMetadata("array2.kt")
public void testArray2() throws Exception {
runTest("idea/testData/quickfix/addExclExclCall/array2.kt");
}
@TestMetadata("array3.kt")
public void testArray3() throws Exception {
runTest("idea/testData/quickfix/addExclExclCall/array3.kt");
}
@TestMetadata("array4.kt")
public void testArray4() throws Exception {
runTest("idea/testData/quickfix/addExclExclCall/array4.kt");
}
@TestMetadata("implicit.kt")
public void testImplicit() throws Exception {
runTest("idea/testData/quickfix/addExclExclCall/implicit.kt");