From ebd72df058d3d0c3ff114e9ff54a534a398c6760 Mon Sep 17 00:00:00 2001 From: Nikolay Krasko Date: Tue, 9 Apr 2013 14:30:22 +0400 Subject: [PATCH] KT-3491 Wrong (or not obvious) warning #KT-3491 Fixed --- .../BasicExpressionTypingVisitor.java | 14 ++++++++++---- .../tests/dataFlowInfoTraversal/Elvis.kt | 4 ++-- .../tests/nullableTypes/uselessElvis.kt | 17 +++++++++++++++++ .../checkers/JetDiagnosticsTestGenerated.java | 5 +++++ 4 files changed, 34 insertions(+), 6 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/nullableTypes/uselessElvis.kt diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/BasicExpressionTypingVisitor.java b/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/BasicExpressionTypingVisitor.java index 21e32b3091e..7a29fdb62bf 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/BasicExpressionTypingVisitor.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/BasicExpressionTypingVisitor.java @@ -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( diff --git a/compiler/testData/diagnostics/tests/dataFlowInfoTraversal/Elvis.kt b/compiler/testData/diagnostics/tests/dataFlowInfoTraversal/Elvis.kt index fcfdbb7b7b7..99f3f3068ae 100644 --- a/compiler/testData/diagnostics/tests/dataFlowInfoTraversal/Elvis.kt +++ b/compiler/testData/diagnostics/tests/dataFlowInfoTraversal/Elvis.kt @@ -4,6 +4,6 @@ fun foo() { val x: Int? = null bar(x ?: 0) - if (x != null) bar(x ?: x) + if (x != null) bar(x ?: x) bar(x) -} +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/nullableTypes/uselessElvis.kt b/compiler/testData/diagnostics/tests/nullableTypes/uselessElvis.kt new file mode 100644 index 00000000000..86cf6a5aee6 --- /dev/null +++ b/compiler/testData/diagnostics/tests/nullableTypes/uselessElvis.kt @@ -0,0 +1,17 @@ +// For KT-3491 + +fun test1(t: Any?): Any { + return t as T ?: "" +} + +fun test2(t: Any?): Any { + return t as T ?: "" +} + +fun test3(t: Any?): Any { + if (t != null) { + return t ?: "" + } + + return 1 +} \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java index cc775049764..8b17fe7f893 100644 --- a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java @@ -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")