Do not show warning about useless elvis for error function types

#KT-17214 Fixed
 #KT-12112 Fixed
This commit is contained in:
Mikhail Zarechenskiy
2017-04-03 02:57:03 +03:00
parent 3cdf6c898a
commit 1e56815b3b
4 changed files with 12 additions and 2 deletions
@@ -892,7 +892,7 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
return baseTypeInfo;
}
DataFlowInfo dataFlowInfo = baseTypeInfo.getDataFlowInfo();
if (isKnownToBeNotNull(baseExpression, baseType, context) && (!baseType.isError() || ErrorUtils.isUninferredParameter(baseType))) {
if (isKnownToBeNotNull(baseExpression, baseType, context)) {
context.trace.report(UNNECESSARY_NOT_NULL_ASSERTION.on(operationSign, TypeUtils.makeNotNullable(baseType)));
}
else {
@@ -935,12 +935,16 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
return facade.getTypeInfo(baseExpression, context, isStatement);
}
// Returns `true` if warnings should be reported for left-hand side of elvis and not-null (!!) assertion
private static boolean isKnownToBeNotNull(
@NotNull KtExpression expression,
@Nullable KotlinType ktType,
@NotNull ExpressionTypingContext context
) {
if (ktType == null) return false;
if (ktType.isError() && !ErrorUtils.isUninferredParameter(ktType)) return false;
if (!TypeUtils.isNullableType(ktType)) return true;
DataFlowValue dataFlowValue = createDataFlowValue(expression, ktType, context);
@@ -35,10 +35,14 @@ fun test() {
takeNotNull(dependOn(dependOn(x)) <!USELESS_ELVIS!>?: ""<!>)
takeNotNull(dependOn(dependOn(x) <!USELESS_CAST!>as? String<!>) ?: "")
}
takeNotNull(bar()!!)
}
inline fun <reified T : Any> reifiedNull(): T? = null
fun testFrom13648() {
takeNotNull(reifiedNull() ?: "")
}
}
fun bar() = <!UNRESOLVED_REFERENCE!>unresolved<!>
@@ -1,5 +1,6 @@
package
public fun bar(): [ERROR : Error function type]
public fun </*0*/ T> dependOn(/*0*/ x: T): T
public fun </*0*/ T> notNull(): T
public fun </*0*/ T> nullable(): T?
@@ -61,6 +61,7 @@ fun test() {
takeNotNull(J.getNAny() ?: J())
val x = <!UNRESOLVED_REFERENCE!>unresolved<!> ?: null
<!UNREACHABLE_CODE!>val y =<!> <!UNRESOLVED_REFERENCE!>unresolved<!>.<!DEBUG_INFO_ELEMENT_WITH_ERROR_TYPE!>foo<!> ?: return
}
fun takeNotNull(s: J) {}