diff --git a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/expression/FirExhaustiveWhenChecker.kt b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/expression/FirExhaustiveWhenChecker.kt index d94ddd98418..479df3aee88 100644 --- a/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/expression/FirExhaustiveWhenChecker.kt +++ b/compiler/fir/checkers/src/org/jetbrains/kotlin/fir/analysis/checkers/expression/FirExhaustiveWhenChecker.kt @@ -22,6 +22,7 @@ import org.jetbrains.kotlin.fir.declarations.utils.modality import org.jetbrains.kotlin.fir.expressions.ExhaustivenessStatus import org.jetbrains.kotlin.fir.expressions.FirWhenExpression import org.jetbrains.kotlin.fir.expressions.impl.FirElseIfTrueCondition +import org.jetbrains.kotlin.fir.expressions.impl.FirEmptyExpressionBlock import org.jetbrains.kotlin.fir.expressions.isExhaustive import org.jetbrains.kotlin.fir.languageVersionSettings import org.jetbrains.kotlin.fir.resolve.fullyExpandedType @@ -37,8 +38,23 @@ object FirExhaustiveWhenChecker : FirWhenExpressionChecker(MppCheckerKind.Common reportElseMisplaced(expression, reporter, context) } + private fun reportEmptyThenInExpression(whenExpression: FirWhenExpression, context: CheckerContext, reporter: DiagnosticReporter) { + val source = whenExpression.source ?: return + + if (source.isIfExpression && whenExpression.usedAsExpression) { + val thenBranch = whenExpression.branches.firstOrNull() + if (thenBranch == null || thenBranch.result is FirEmptyExpressionBlock) { + reporter.reportOn(source, FirErrors.INVALID_IF_AS_EXPRESSION, context) + } + } + } + private fun reportNotExhaustive(whenExpression: FirWhenExpression, context: CheckerContext, reporter: DiagnosticReporter) { - if (whenExpression.isExhaustive) return + if (whenExpression.isExhaustive) { + // whenExpression.isExhaustive is checked as otherwise the constraint is checked below + reportEmptyThenInExpression(whenExpression, context, reporter) + return + } val source = whenExpression.source ?: return diff --git a/compiler/testData/diagnostics/tests/FunctionReturnTypes.fir.kt b/compiler/testData/diagnostics/tests/FunctionReturnTypes.fir.kt index 783b1ace38d..8553a2e8600 100644 --- a/compiler/testData/diagnostics/tests/FunctionReturnTypes.fir.kt +++ b/compiler/testData/diagnostics/tests/FunctionReturnTypes.fir.kt @@ -72,7 +72,7 @@ fun blockReturnValueTypeMatch2() : Int { return if (1 > 2) 1 } fun blockReturnValueTypeMatch3() : Int { - return if (1 > 2) else 1 + return if (1 > 2) else 1 } fun blockReturnValueTypeMatch4() : Int { if (1 > 2) diff --git a/compiler/testData/diagnostics/tests/controlStructures/improperElseInExpression.fir.kt b/compiler/testData/diagnostics/tests/controlStructures/improperElseInExpression.fir.kt index cb7dceee010..cbf26a526f5 100644 --- a/compiler/testData/diagnostics/tests/controlStructures/improperElseInExpression.fir.kt +++ b/compiler/testData/diagnostics/tests/controlStructures/improperElseInExpression.fir.kt @@ -2,7 +2,7 @@ fun example() { val a = if (true) true else false - val b = if (true) else false + val b = if (true) else false val c = if (true) true val d = if (true) true else; val e = if (true) {} else false