[FIR] Remove implicit coercion of ifs and whens to Unit
The rule of thumb is the following: If the `if` and `when` can be successfully replaced with `while`, then it is used as a statement, otherwise, it is used as an expression. #KT-59883
This commit is contained in:
committed by
Space Team
parent
2e66954d01
commit
931f2eab58
+1
-1
@@ -90,7 +90,7 @@ abstract class AbstractLightTreeRawFirBuilder(
|
||||
}
|
||||
}
|
||||
|
||||
private fun LighterASTNode.getLastChildExpression(): LighterASTNode? {
|
||||
fun LighterASTNode.getLastChildExpression(): LighterASTNode? {
|
||||
var result: LighterASTNode? = null
|
||||
forEachChildren {
|
||||
if (it.isExpression()) {
|
||||
|
||||
+12
-6
@@ -1402,13 +1402,19 @@ class LightTreeRawFirExpressionBuilder(
|
||||
parent = parent.getParent() ?: return true
|
||||
}
|
||||
val parentTokenType = parent.tokenType
|
||||
if (parentTokenType == BLOCK) return false
|
||||
if (parentTokenType == THEN || parentTokenType == ELSE || parentTokenType == WHEN_ENTRY) {
|
||||
return parent.getParent()?.usedAsExpression ?: true
|
||||
return when (parentTokenType) {
|
||||
BLOCK -> parent.getLastChildExpression() == this && parent.usedAsExpression
|
||||
TRY, CATCH -> parent.usedAsExpression
|
||||
THEN, ELSE, WHEN_ENTRY -> parent.getParent()?.usedAsExpression ?: true
|
||||
CLASS_INITIALIZER, SCRIPT_INITIALIZER, SECONDARY_CONSTRUCTOR, FUNCTION_LITERAL, FINALLY -> false
|
||||
FUN, PROPERTY_ACCESSOR -> parent.getChildrenAsArray().any { it?.tokenType == EQ }
|
||||
DOT_QUALIFIED_EXPRESSION -> parent.getFirstChild() == this
|
||||
BODY -> when (parent.getParent()?.tokenType) {
|
||||
FOR, WHILE, DO_WHILE -> false
|
||||
else -> true
|
||||
}
|
||||
else -> true
|
||||
}
|
||||
if (parentTokenType != BODY) return true
|
||||
val type = parent.getParent()?.tokenType ?: return true
|
||||
return !(type == FOR || type == WHILE || type == DO_WHILE)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user