Suggest to add !! as a quick-fix for type mistmach

This commit is contained in:
Nikolay Krasko
2015-10-26 18:08:18 +03:00
parent f3e6fab68e
commit b8c72ec4e9
10 changed files with 102 additions and 2 deletions
@@ -43,8 +43,7 @@ public class RemoveExclExclCallFix(val psiElement: PsiElement) : ExclExclCallFix
override fun getText(): String = JetBundle.message("remove.unnecessary.non.null.assertion")
override fun isAvailable(project: Project, editor: Editor?, file: PsiFile): Boolean
= super<ExclExclCallFix>.isAvailable(project, editor, file) &&
getExclExclPostfixExpression() != null
= super.isAvailable(project, editor, file) && getExclExclPostfixExpression() != null
override fun invoke(project: Project, editor: Editor?, file: PsiFile?) {
if (!FileModificationService.getInstance().prepareFileForWrite(file)) return
@@ -88,6 +87,9 @@ public class AddExclExclCallFix(val psiElement: PsiElement) : ExclExclCallFix()
return sibling
}
}
else if (psiElement is KtExpression) {
return psiElement
}
return null
}
@@ -97,6 +97,13 @@ public class QuickFixFactoryForTypeMismatchError extends JetIntentionActionsFact
actions.add(new CastExpressionFix(expression, expectedType));
}
if (!expectedType.isMarkedNullable() && org.jetbrains.kotlin.types.TypeUtils.isNullableType(expressionType)) {
KotlinType nullableExpected = TypeUtilsKt.makeNullable(expectedType);
if (TypeUtilsKt.isSubtypeOf(expressionType, nullableExpected)) {
actions.add(new AddExclExclCallFix(expression));
}
}
// Property initializer type mismatch property type:
KtProperty property = PsiTreeUtil.getParentOfType(expression, KtProperty.class);
if (property != null) {
@@ -0,0 +1,7 @@
// "Add non-null asserted (!!) call" "true"
fun test() {
val s: String? = null
other(<caret>s)
}
fun other(s: String) {}
@@ -0,0 +1,7 @@
// "Add non-null asserted (!!) call" "true"
fun test() {
val s: String? = null
other(<caret>s!!)
}
fun other(s: String) {}
@@ -0,0 +1,9 @@
// "Add non-null asserted (!!) call" "false"
// ACTION: Change parameter 's' type of function 'other' to 'String?'
// ERROR: <html>ype mismatch.<table><tr><td>Required:</td><td>kotlin.Int</td></tr><tr><td>Found:</td><td>kotlin.String?</td></tr></table></html>
fun test() {
val s: String? = ""
other(<caret>s)
}
fun other(s: Int) {}
@@ -0,0 +1,8 @@
// "Add non-null asserted (!!) call" "true"
interface Some
fun <T: Some?> test(t: T) {
other(<caret>t)
}
fun other(s: Any) {}
@@ -0,0 +1,8 @@
// "Add non-null asserted (!!) call" "true"
interface Some
fun <T: Some?> test(t: T) {
other(t!!)
}
fun other(s: Any) {}
@@ -0,0 +1,14 @@
// "Add non-null asserted (!!) call" "true"
interface A<out T>
class B<out T>: A<T>
open class C
open class D: C()
fun test() {
val s: B<D>? = B()
other(<caret>s)
}
fun other(s: A<C>) {}
@@ -0,0 +1,14 @@
// "Add non-null asserted (!!) call" "true"
interface A<out T>
class B<out T>: A<T>
open class C
open class D: C()
fun test() {
val s: B<D>? = B()
other(<caret>s!!)
}
fun other(s: A<C>) {}
@@ -6161,6 +6161,30 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
doTest(fileName);
}
@TestMetadata("addExclExclToRemoveNullability.kt")
public void testAddExclExclToRemoveNullability() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/typeMismatch/addExclExclToRemoveNullability.kt");
doTest(fileName);
}
@TestMetadata("addExclExclToRemoveNullabilityDisabledWhenItCannotHelp.kt")
public void testAddExclExclToRemoveNullabilityDisabledWhenItCannotHelp() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/typeMismatch/addExclExclToRemoveNullabilityDisabledWhenItCannotHelp.kt");
doTest(fileName);
}
@TestMetadata("addExclExclToRemoveNullabilityForGeneric.kt")
public void testAddExclExclToRemoveNullabilityForGeneric() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/typeMismatch/addExclExclToRemoveNullabilityForGeneric.kt");
doTest(fileName);
}
@TestMetadata("addExclExclToRemoveNullabilityForSubclass.kt")
public void testAddExclExclToRemoveNullabilityForSubclass() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/quickfix/typeMismatch/addExclExclToRemoveNullabilityForSubclass.kt");
doTest(fileName);
}
public void testAllFilesPresentInTypeMismatch() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/quickfix/typeMismatch"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
}