MoveVariableDeclarationIntoWhenInspection shouldn't report a multi-line variable
#KT-30457 Fixed
This commit is contained in:
+3
@@ -13,6 +13,7 @@ import com.intellij.openapi.project.Project
|
|||||||
import com.intellij.openapi.util.TextRange
|
import com.intellij.openapi.util.TextRange
|
||||||
import com.intellij.psi.PsiElementVisitor
|
import com.intellij.psi.PsiElementVisitor
|
||||||
import com.intellij.psi.SmartPsiElementPointer
|
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.countUsages
|
||||||
import org.jetbrains.kotlin.idea.intentions.loopToCallChain.previousStatement
|
import org.jetbrains.kotlin.idea.intentions.loopToCallChain.previousStatement
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
@@ -23,6 +24,8 @@ class MoveVariableDeclarationIntoWhenInspection : AbstractKotlinInspection(), Cl
|
|||||||
whenExpressionVisitor(fun(expression: KtWhenExpression) {
|
whenExpressionVisitor(fun(expression: KtWhenExpression) {
|
||||||
val subjectExpression = expression.subjectExpression ?: return
|
val subjectExpression = expression.subjectExpression ?: return
|
||||||
val property = expression.findDeclarationNear() ?: return
|
val property = expression.findDeclarationNear() ?: return
|
||||||
|
if (!property.isOneLiner()) return
|
||||||
|
|
||||||
val action = property.action(expression)
|
val action = property.action(expression)
|
||||||
if (action == Action.NOTHING) return
|
if (action == Action.NOTHING) return
|
||||||
|
|
||||||
|
|||||||
+11
@@ -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
|
||||||
|
}
|
||||||
+5
@@ -4525,6 +4525,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
|||||||
runTest("idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/notApplicableInBinaryExpression.kt");
|
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")
|
@TestMetadata("notApplicableOtherName.kt")
|
||||||
public void testNotApplicableOtherName() throws Exception {
|
public void testNotApplicableOtherName() throws Exception {
|
||||||
runTest("idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/notApplicableOtherName.kt");
|
runTest("idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/notApplicableOtherName.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user