KT-3491 Wrong (or not obvious) warning
#KT-3491 Fixed
This commit is contained in:
+10
-4
@@ -711,7 +711,11 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
|
||||
private static boolean isKnownToBeNotNull(JetExpression expression, ExpressionTypingContext context) {
|
||||
JetType type = context.trace.get(EXPRESSION_TYPE, expression);
|
||||
assert type != null : "This method is only supposed to be called when the type is not null";
|
||||
DataFlowValue dataFlowValue = DataFlowValueFactory.INSTANCE.createDataFlowValue(expression, type, context.trace.getBindingContext());
|
||||
return isKnownToBeNotNull(expression, type, context);
|
||||
}
|
||||
|
||||
private static boolean isKnownToBeNotNull(JetExpression expression, JetType jetType, ExpressionTypingContext context) {
|
||||
DataFlowValue dataFlowValue = DataFlowValueFactory.INSTANCE.createDataFlowValue(expression, jetType, context.trace.getBindingContext());
|
||||
return !context.dataFlowInfo.getNullability(dataFlowValue).canBeNull();
|
||||
}
|
||||
|
||||
@@ -853,12 +857,14 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
|
||||
JetType leftType = leftTypeInfo.getType();
|
||||
dataFlowInfo = leftTypeInfo.getDataFlowInfo();
|
||||
|
||||
ExpressionTypingContext newContext = contextWithExpectedType.replaceDataFlowInfo(dataFlowInfo).replaceScope(context.scope);
|
||||
JetType rightType = right == null ? null : facade.getTypeInfo(right, newContext).getType();
|
||||
if (leftType != null) {
|
||||
if (!leftType.isNullable()) {
|
||||
if (isKnownToBeNotNull(left, leftType, context)) {
|
||||
context.trace.report(USELESS_ELVIS.on(left, leftType));
|
||||
}
|
||||
|
||||
ExpressionTypingContext newContext = contextWithExpectedType.replaceDataFlowInfo(dataFlowInfo).replaceScope(context.scope);
|
||||
JetType rightType = right == null ? null : facade.getTypeInfo(right, newContext).getType();
|
||||
|
||||
if (rightType != null) {
|
||||
DataFlowUtils.checkType(TypeUtils.makeNullableAsSpecified(leftType, rightType.isNullable()), left, contextWithExpectedType);
|
||||
return JetTypeInfo.create(TypeUtils.makeNullableAsSpecified(
|
||||
|
||||
@@ -4,6 +4,6 @@ fun foo() {
|
||||
val x: Int? = null
|
||||
|
||||
bar(x ?: 0)
|
||||
if (x != null) bar(x ?: x)
|
||||
if (x != null) bar(<!USELESS_ELVIS!>x<!> ?: x)
|
||||
bar(<!TYPE_MISMATCH!>x<!>)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
// For KT-3491
|
||||
|
||||
fun <T: Any?> test1(t: Any?): Any {
|
||||
return t as T ?: ""
|
||||
}
|
||||
|
||||
fun <T: Any> test2(t: Any?): Any {
|
||||
return <!USELESS_ELVIS!>t as T<!> ?: ""
|
||||
}
|
||||
|
||||
fun <T: Any?> test3(t: Any?): Any {
|
||||
if (t != null) {
|
||||
return <!USELESS_ELVIS!>t<!> ?: ""
|
||||
}
|
||||
|
||||
return 1
|
||||
}
|
||||
@@ -3055,6 +3055,11 @@ public class JetDiagnosticsTestGenerated extends AbstractDiagnosticsTestWithEage
|
||||
doTest("compiler/testData/diagnostics/tests/nullableTypes/safeCallOnTypeWithNullableUpperBound.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("uselessElvis.kt")
|
||||
public void testUselessElvis() throws Exception {
|
||||
doTest("compiler/testData/diagnostics/tests/nullableTypes/uselessElvis.kt");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@TestMetadata("compiler/testData/diagnostics/tests/objects")
|
||||
|
||||
Reference in New Issue
Block a user