From d08d8af407c3e93b2a935142fa8344e289a4f8ab Mon Sep 17 00:00:00 2001 From: Mikhail Glukhikh Date: Tue, 23 Aug 2016 16:25:04 +0300 Subject: [PATCH] USELESS_ELVIS_RIGHT_IS_NULL : build fix (add to different files in DiagnosticsTestGenerated), do not report if USELESS_ELVIS already exists (cherry picked from commit c71b656) --- .../types/expressions/BasicExpressionTypingVisitor.java | 6 +++--- .../tests/controlFlowAnalysis/elvisNotProcessed.kt | 4 ++-- .../tests/resolve/invoke/errors/unsafeCallWithInvoke.kt | 2 +- .../diagnostics/tests/smartCasts/elvisExprNotNull.kt | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/BasicExpressionTypingVisitor.java b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/BasicExpressionTypingVisitor.java index 8ac182d2d81..8b7e5ab79ca 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/BasicExpressionTypingVisitor.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/types/expressions/BasicExpressionTypingVisitor.java @@ -1190,6 +1190,9 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor { if (leftType != null && isKnownToBeNotNull(left, leftType, context)) { context.trace.report(USELESS_ELVIS.on(expression, leftType)); } + else if (KtPsiUtil.isNullConstant(right)) { + context.trace.report(USELESS_ELVIS_RIGHT_IS_NULL.on(expression)); + } KotlinTypeInfo rightTypeInfo = BindingContextUtils.getRecordedTypeInfo(right, context.trace.getBindingContext()); if (rightTypeInfo == null && ArgumentTypeResolver.isFunctionLiteralArgument(right, context)) { // the type is computed later in call completer according to the '?:' semantics as a function @@ -1198,9 +1201,6 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor { assert rightTypeInfo != null : "Right expression was not processed: " + expression; boolean loopBreakContinuePossible = leftTypeInfo.getJumpOutPossible() || rightTypeInfo.getJumpOutPossible(); KotlinType rightType = rightTypeInfo.getType(); - if (rightType != null && KtPsiUtil.isNullConstant(right)) { - context.trace.report(USELESS_ELVIS_RIGHT_IS_NULL.on(expression)); - } // Only left argument DFA is taken into account here: we cannot be sure that right argument is joined // (we merge it with right DFA if right argument contains no jump outside) diff --git a/compiler/testData/diagnostics/tests/controlFlowAnalysis/elvisNotProcessed.kt b/compiler/testData/diagnostics/tests/controlFlowAnalysis/elvisNotProcessed.kt index 0928e1713c1..7af302ef80c 100644 --- a/compiler/testData/diagnostics/tests/controlFlowAnalysis/elvisNotProcessed.kt +++ b/compiler/testData/diagnostics/tests/controlFlowAnalysis/elvisNotProcessed.kt @@ -27,9 +27,9 @@ val b = null ?: ( l() ?: false) val bb = null ?: ( l() ?: null!!) -val bbb = null ?: ( l() ?: null) +val bbb = null ?: ( l() ?: null) -val bbbb = ( l() ?: null) ?: ( l() ?: null) +val bbbb = ( l() ?: null) ?: ( l() ?: null) fun f(x : Long?): Long { var a = x ?: (fun() {} ?: fun() {}) diff --git a/compiler/testData/diagnostics/tests/resolve/invoke/errors/unsafeCallWithInvoke.kt b/compiler/testData/diagnostics/tests/resolve/invoke/errors/unsafeCallWithInvoke.kt index 042551c1249..2d0bde629ee 100644 --- a/compiler/testData/diagnostics/tests/resolve/invoke/errors/unsafeCallWithInvoke.kt +++ b/compiler/testData/diagnostics/tests/resolve/invoke/errors/unsafeCallWithInvoke.kt @@ -5,5 +5,5 @@ operator fun String.invoke(i: Int) {} fun foo(s: String?) { s(1) - (s ?: null)(1) + (s ?: null)(1) } diff --git a/compiler/testData/diagnostics/tests/smartCasts/elvisExprNotNull.kt b/compiler/testData/diagnostics/tests/smartCasts/elvisExprNotNull.kt index 2a91f6b5f40..3c6ccd65658 100644 --- a/compiler/testData/diagnostics/tests/smartCasts/elvisExprNotNull.kt +++ b/compiler/testData/diagnostics/tests/smartCasts/elvisExprNotNull.kt @@ -30,7 +30,7 @@ fun baz(s: String?, r: String?): String { } fun withNull(s: String?): String { - val t = s ?: null + val t = s ?: null // Error: nullable return t } \ No newline at end of file