From e7b4927b6c17f3633c7a17b07f03fd7dd3a7e568 Mon Sep 17 00:00:00 2001 From: pyos Date: Tue, 8 Nov 2022 14:26:49 +0100 Subject: [PATCH] FIR: don't emit SENSELESS_NULL_IN_WHEN when value is always null This case (value is always null) contradicts the error message which says "Expression under 'when' is never equal to null". --- .../expression/FirEqualityCompatibilityChecker.kt | 5 +++-- .../testData/diagnostics/tests/when/whenOnNothing.fir.kt | 2 +- .../linked/expressions/when-expression/p-6/pos/5.2.fir.kt | 2 +- .../testData/diagnostics/notLinked/dfa/pos/37.fir.kt | 2 +- .../testData/diagnostics/notLinked/dfa/pos/62.fir.kt | 8 ++++---- 5 files changed, 10 insertions(+), 9 deletions(-) diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/expression/FirEqualityCompatibilityChecker.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/expression/FirEqualityCompatibilityChecker.kt index 71673337567..d342477f7df 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/expression/FirEqualityCompatibilityChecker.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/expression/FirEqualityCompatibilityChecker.kt @@ -131,8 +131,9 @@ object FirEqualityCompatibilityChecker : FirEqualityOperatorCallChecker() { } } // We only report `SENSELESS_NULL_IN_WHEN` if `lType = type` because `lType` is the type of the when subject. This diagnostic is - // only intended for cases where the branch condition contains a null. - if (expression.source?.elementType != KtNodeTypes.BINARY_EXPRESSION && type === lType) { + // only intended for cases where the branch condition contains a null. Also, the error message for SENSELESS_NULL_IN_WHEN + // says the value is *never* equal to null, so we can't report it if the value is *always* equal to null. + if (expression.source?.elementType != KtNodeTypes.BINARY_EXPRESSION && type === lType && !compareResult) { reporter.reportOn(expression.source, FirErrors.SENSELESS_NULL_IN_WHEN, context) } else { reporter.reportOn(expression.source, FirErrors.SENSELESS_COMPARISON, expression, compareResult, context) diff --git a/compiler/testData/diagnostics/tests/when/whenOnNothing.fir.kt b/compiler/testData/diagnostics/tests/when/whenOnNothing.fir.kt index 2dcf15888c9..0e87c6eecf2 100644 --- a/compiler/testData/diagnostics/tests/when/whenOnNothing.fir.kt +++ b/compiler/testData/diagnostics/tests/when/whenOnNothing.fir.kt @@ -3,7 +3,7 @@ // exhaustive fun test1(n: Nothing) = when (n) { } fun test2(n: Nothing?) = when (n) { - null -> {} + null -> {} } // not exhaustive diff --git a/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/p-6/pos/5.2.fir.kt b/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/p-6/pos/5.2.fir.kt index ccab631076a..39c07ac0443 100644 --- a/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/p-6/pos/5.2.fir.kt +++ b/compiler/tests-spec/testData/diagnostics/linked/expressions/when-expression/p-6/pos/5.2.fir.kt @@ -218,7 +218,7 @@ fun case_23(value_1: Nothing) { // TESTCASE NUMBER: 24 fun case_24(value_1: Nothing?) = when (value_1) { throw Exception(), return "" -> "" - null, return return return "", throw throw throw Exception() -> "" + null, return return return "", throw throw throw Exception() -> "" else -> "" } diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/dfa/pos/37.fir.kt b/compiler/tests-spec/testData/diagnostics/notLinked/dfa/pos/37.fir.kt index 1867a5575a9..43ebaef81f5 100644 --- a/compiler/tests-spec/testData/diagnostics/notLinked/dfa/pos/37.fir.kt +++ b/compiler/tests-spec/testData/diagnostics/notLinked/dfa/pos/37.fir.kt @@ -199,7 +199,7 @@ fun case_17(x: Boolean?, y: Boolean?) { else -> if (true) if (true) if (true) if (true) if (true) x!! else x!! else x!! else x!! else x!! else x!! } false -> x!! - null -> if (true) if (true) if (true) if (true) if (true) x!! else x!! else x!! else x!! else x!! else x!! + null -> if (true) if (true) if (true) if (true) if (true) x!! else x!! else x!! else x!! else x!! else x!! } else x!! else x!! else x!! else x!! else x!! } break@loop diff --git a/compiler/tests-spec/testData/diagnostics/notLinked/dfa/pos/62.fir.kt b/compiler/tests-spec/testData/diagnostics/notLinked/dfa/pos/62.fir.kt index 154474d078b..6e923ed3720 100644 --- a/compiler/tests-spec/testData/diagnostics/notLinked/dfa/pos/62.fir.kt +++ b/compiler/tests-spec/testData/diagnostics/notLinked/dfa/pos/62.fir.kt @@ -154,7 +154,7 @@ fun case_10(x: Any): String { fun case_11(x: Any?): String? { if (x is Nothing?) { return when(x) { - null -> null + null -> null } } return "" @@ -168,7 +168,7 @@ fun case_11(x: Any?): String? { fun case_12(x: Any?): String? { if (x == null) { return when(x) { - null -> null + null -> null } } return "" @@ -182,7 +182,7 @@ fun case_12(x: Any?): String? { fun case_13(x: Any?): String? { if (x === null) { return when(x) { - null -> null + null -> null } } return "" @@ -196,7 +196,7 @@ fun case_13(x: Any?): String? { fun case_14(x: Any?): String? { x as Nothing? return when(x) { - null -> null + null -> null } }