Convert to expression body is forbidden on single non-exhaustive when statement with Unit result #KT-12502 Fixed

This commit is contained in:
Mikhail Glukhikh
2016-05-27 13:33:14 +03:00
parent 91ce8cc612
commit 2350caf177
7 changed files with 68 additions and 2 deletions
@@ -28,6 +28,7 @@ import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.anyDescendantOfType
import org.jetbrains.kotlin.psi.psiUtil.endOffset
import org.jetbrains.kotlin.psi.psiUtil.startOffset
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.utils.addToStdlib.check
class ConvertToExpressionBodyIntention : SelfTargetingOffsetIndependentIntention<KtDeclarationWithBody>(
@@ -102,8 +103,14 @@ class ConvertToExpressionBodyIntention : SelfTargetingOffsetIndependentIntention
else -> {
if (statement is KtBinaryExpression && statement.operationToken in KtTokens.ALL_ASSIGNMENTS) return null // assignment does not have value
val expressionType = statement.analyze().getType(statement) ?: return null
if (!KotlinBuiltIns.isUnit(expressionType) && !KotlinBuiltIns.isNothing(expressionType)) return null
val context = statement.analyze()
val expressionType = context.getType(statement) ?: return null
val isUnit = KotlinBuiltIns.isUnit(expressionType)
if (!isUnit && !KotlinBuiltIns.isNothing(expressionType)) return null
if (isUnit && statement is KtWhenExpression) {
if (statement.elseExpression == null && context.get(BindingContext.EXHAUSTIVE_WHEN, statement) != true) return null
}
return statement
}
}
@@ -0,0 +1,9 @@
enum class AccessMode { READ, WRITE, RW }
fun whenExpr(access: AccessMode) {
<caret>when (access) {
AccessMode.READ -> println("read")
AccessMode.WRITE -> println("write")
AccessMode.RW -> println("rw")
}
}
fun println(s: String) {}
@@ -0,0 +1,7 @@
enum class AccessMode { READ, WRITE, RW }
fun whenExpr(access: AccessMode) = when (access) {
AccessMode.READ -> println("read")
AccessMode.WRITE -> println("write")
AccessMode.RW -> println("rw")
}
fun println(s: String) {}
@@ -0,0 +1,9 @@
// IS_APPLICABLE: false
enum class AccessMode { READ, WRITE, RW }
fun whenExpr(access: AccessMode) {
<caret>when (access) {
AccessMode.READ -> println("read")
AccessMode.WRITE -> println("write")
}
}
fun println(s: String) {}
@@ -0,0 +1,9 @@
enum class AccessMode { READ, WRITE, RW }
fun whenExpr(access: AccessMode) {
<caret>when (access) {
AccessMode.READ -> println("read")
AccessMode.WRITE -> println("write")
else -> println("else")
}
}
fun println(s: String) {}
@@ -0,0 +1,7 @@
enum class AccessMode { READ, WRITE, RW }
fun whenExpr(access: AccessMode) = when (access) {
AccessMode.READ -> println("read")
AccessMode.WRITE -> println("write")
else -> println("else")
}
fun println(s: String) {}
@@ -4591,6 +4591,24 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
doTest(fileName);
}
@TestMetadata("whenUnitExhaustive.kt")
public void testWhenUnitExhaustive() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToExpressionBody/whenUnitExhaustive.kt");
doTest(fileName);
}
@TestMetadata("whenUnitNonExhaustive.kt")
public void testWhenUnitNonExhaustive() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToExpressionBody/whenUnitNonExhaustive.kt");
doTest(fileName);
}
@TestMetadata("whenUnitWithElse.kt")
public void testWhenUnitWithElse() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToExpressionBody/whenUnitWithElse.kt");
doTest(fileName);
}
@TestMetadata("while.kt")
public void testWhile() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/convertToExpressionBody/while.kt");