Move variable declaration into when: don't report when property has 'return' or 'throw'
#KT-31999 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
d9d04fc556
commit
877e583a96
+6
@@ -21,6 +21,7 @@ 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.lexer.KtTokens
|
import org.jetbrains.kotlin.lexer.KtTokens
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.anyDescendantOfType
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.createSmartPointer
|
import org.jetbrains.kotlin.psi.psiUtil.createSmartPointer
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.siblings
|
import org.jetbrains.kotlin.psi.psiUtil.siblings
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
import org.jetbrains.kotlin.psi.psiUtil.startOffset
|
||||||
@@ -31,6 +32,7 @@ class MoveVariableDeclarationIntoWhenInspection : AbstractKotlinInspection(), Cl
|
|||||||
val subjectExpression = expression.subjectExpression ?: return
|
val subjectExpression = expression.subjectExpression ?: return
|
||||||
val property = expression.findDeclarationNear() ?: return
|
val property = expression.findDeclarationNear() ?: return
|
||||||
if (!property.isOneLiner()) return
|
if (!property.isOneLiner()) return
|
||||||
|
if (property.hasReturnOrThrowExpression()) return
|
||||||
|
|
||||||
val action = property.action(expression)
|
val action = property.action(expression)
|
||||||
if (action == Action.NOTHING) return
|
if (action == Action.NOTHING) return
|
||||||
@@ -75,6 +77,10 @@ private fun KtWhenExpression.findDeclarationNear(): KtProperty? {
|
|||||||
return previousProperty.takeIf { !it.isVar && it.hasInitializer() && it.nameIdentifier?.text == subjectExpression?.text }
|
return previousProperty.takeIf { !it.isVar && it.hasInitializer() && it.nameIdentifier?.text == subjectExpression?.text }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun KtProperty.hasReturnOrThrowExpression(): Boolean {
|
||||||
|
return this.initializer?.anyDescendantOfType<KtExpression> { it is KtReturnExpression || it is KtThrowExpression } == true
|
||||||
|
}
|
||||||
|
|
||||||
private tailrec fun KtExpression.previousPropertyFromParent(): KtProperty? {
|
private tailrec fun KtExpression.previousPropertyFromParent(): KtProperty? {
|
||||||
val parentExpression = parent as? KtExpression ?: return null
|
val parentExpression = parent as? KtExpression ?: return null
|
||||||
if (this != when (parentExpression) {
|
if (this != when (parentExpression) {
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
// PROBLEM: none
|
||||||
|
fun test(str: String?): String? {
|
||||||
|
val <caret>some = str ?: return null
|
||||||
|
return when (some) {
|
||||||
|
"some" -> some
|
||||||
|
else -> ""
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
// PROBLEM: none
|
||||||
|
fun test(str: String?): String? {
|
||||||
|
val <caret>some = if (str != null) str + str else return null
|
||||||
|
return when (some) {
|
||||||
|
"some" -> some
|
||||||
|
else -> ""
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
// PROBLEM: none
|
||||||
|
fun test(str: String?): String? {
|
||||||
|
val <caret>some = str ?: throw Exception()
|
||||||
|
return when (some) {
|
||||||
|
"some" -> some
|
||||||
|
else -> ""
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
// PROBLEM: none
|
||||||
|
fun test(str: String?): String? {
|
||||||
|
val <caret>some = if (str != null) str + str else throw Exception()
|
||||||
|
return when (some) {
|
||||||
|
"some" -> some
|
||||||
|
else -> ""
|
||||||
|
}
|
||||||
|
}
|
||||||
+20
@@ -6093,6 +6093,26 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
|||||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
|
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("hasReturn.kt")
|
||||||
|
public void testHasReturn() throws Exception {
|
||||||
|
runTest("idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/hasReturn.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("hasReturn2.kt")
|
||||||
|
public void testHasReturn2() throws Exception {
|
||||||
|
runTest("idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/hasReturn2.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("hasThrow.kt")
|
||||||
|
public void testHasThrow() throws Exception {
|
||||||
|
runTest("idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/hasThrow.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("hasThrow2.kt")
|
||||||
|
public void testHasThrow2() throws Exception {
|
||||||
|
runTest("idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/hasThrow2.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("inBinaryExpression.kt")
|
@TestMetadata("inBinaryExpression.kt")
|
||||||
public void testInBinaryExpression() throws Exception {
|
public void testInBinaryExpression() throws Exception {
|
||||||
runTest("idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/inBinaryExpression.kt");
|
runTest("idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/inBinaryExpression.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user