"Add non-null asserted (!!) call": Fix for array access expression (KT-27071)
^KT-27071 Fixed
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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]
|
||||
}
|
||||
@@ -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]!!
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user