MoveVariableDeclarationIntoWhenInspection shouldn't report a multi-line variable

#KT-30457 Fixed
This commit is contained in:
Dmitry Gridin
2019-04-23 21:40:20 +07:00
parent d456ae58b6
commit 2ae30de980
3 changed files with 19 additions and 0 deletions
@@ -13,6 +13,7 @@ import com.intellij.openapi.project.Project
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiElementVisitor
import com.intellij.psi.SmartPsiElementPointer
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.isOneLiner
import org.jetbrains.kotlin.idea.intentions.loopToCallChain.countUsages
import org.jetbrains.kotlin.idea.intentions.loopToCallChain.previousStatement
import org.jetbrains.kotlin.psi.*
@@ -23,6 +24,8 @@ class MoveVariableDeclarationIntoWhenInspection : AbstractKotlinInspection(), Cl
whenExpressionVisitor(fun(expression: KtWhenExpression) {
val subjectExpression = expression.subjectExpression ?: return
val property = expression.findDeclarationNear() ?: return
if (!property.isOneLiner()) return
val action = property.action(expression)
if (action == Action.NOTHING) return
@@ -0,0 +1,11 @@
// PROBLEM: none
fun foo(): Int {
val a<caret> = if (true) true
else false
return when (a) {
true -> 42
else -> null
} ?: 55
}
@@ -4525,6 +4525,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
runTest("idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/notApplicableInBinaryExpression.kt");
}
@TestMetadata("notApplicableMultyLine.kt")
public void testNotApplicableMultyLine() throws Exception {
runTest("idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/notApplicableMultyLine.kt");
}
@TestMetadata("notApplicableOtherName.kt")
public void testNotApplicableOtherName() throws Exception {
runTest("idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/notApplicableOtherName.kt");