Don't suggest cast when !! is possible #KT-18033 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
af3a123c15
commit
0fd42bc747
@@ -32,6 +32,7 @@ import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
|
||||
import org.jetbrains.kotlin.types.KotlinType
|
||||
import org.jetbrains.kotlin.types.asFlexibleType
|
||||
import org.jetbrains.kotlin.types.typeUtil.isSubtypeOf
|
||||
import org.jetbrains.kotlin.types.typeUtil.makeNullable
|
||||
|
||||
class CastExpressionFix(element: KtExpression, type: KotlinType) : KotlinQuickFixAction<KtExpression>(element) {
|
||||
private val typeSourceCode = IdeDescriptorRenderers.SOURCE_CODE.renderType(type)
|
||||
@@ -39,6 +40,7 @@ class CastExpressionFix(element: KtExpression, type: KotlinType) : KotlinQuickFi
|
||||
private val upOrDownCast: Boolean = run {
|
||||
val expressionType = element.analyze(BodyResolveMode.PARTIAL).getType(element)
|
||||
expressionType != null && (type.isSubtypeOf(expressionType) || expressionType.isSubtypeOf(type))
|
||||
&& expressionType != type.makeNullable() //covered by AddExclExclCallFix
|
||||
}
|
||||
|
||||
override fun getFamilyName() = "Cast expression"
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
// "Cast expression 's' to 'String'" "false"
|
||||
// ACTION: Add 'toString()' call
|
||||
// ACTION: Add non-null asserted (!!) call
|
||||
// ACTION: Change parameter 's' type of function 'bar' to 'String?'
|
||||
// ACTION: Convert to expression body
|
||||
// ACTION: Create function 'bar'
|
||||
// ACTION: Surround with null check
|
||||
// ACTION: Wrap with '?.let { ... }' call
|
||||
// ERROR: Type mismatch: inferred type is String? but String was expected
|
||||
|
||||
fun foo(s: String?) {
|
||||
bar(<caret>s)
|
||||
}
|
||||
|
||||
fun bar(s: String){}
|
||||
@@ -10291,6 +10291,12 @@ public class QuickFixTestGenerated extends AbstractQuickFixTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("preferExclExcl.kt")
|
||||
public void testPreferExclExcl() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/typeMismatch/casts/preferExclExcl.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("smartcastImpossible1.kt")
|
||||
public void testSmartcastImpossible1() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/quickfix/typeMismatch/casts/smartcastImpossible1.kt");
|
||||
|
||||
Reference in New Issue
Block a user