Move variable declaration into when: don't report when property has 'break' or 'continue'
#KT-31999
This commit is contained in:
committed by
Mikhail Glukhikh
parent
877e583a96
commit
998adfb098
+3
-5
@@ -32,7 +32,9 @@ class MoveVariableDeclarationIntoWhenInspection : AbstractKotlinInspection(), Cl
|
||||
val subjectExpression = expression.subjectExpression ?: return
|
||||
val property = expression.findDeclarationNear() ?: return
|
||||
if (!property.isOneLiner()) return
|
||||
if (property.hasReturnOrThrowExpression()) return
|
||||
if (property.initializer?.anyDescendantOfType<KtExpression> {
|
||||
it is KtThrowExpression || it is KtReturnExpression || it is KtBreakExpression || it is KtContinueExpression
|
||||
} == true) return
|
||||
|
||||
val action = property.action(expression)
|
||||
if (action == Action.NOTHING) return
|
||||
@@ -77,10 +79,6 @@ private fun KtWhenExpression.findDeclarationNear(): KtProperty? {
|
||||
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? {
|
||||
val parentExpression = parent as? KtExpression ?: return null
|
||||
if (this != when (parentExpression) {
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
// PROBLEM: none
|
||||
fun test() {
|
||||
for (i in 1..10) {
|
||||
val <caret>some = foo(i) ?: break
|
||||
when (some) {
|
||||
"some" -> some
|
||||
else -> ""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun foo(i: Int): String? = ""
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
// PROBLEM: none
|
||||
fun test() {
|
||||
for (i in 1..10) {
|
||||
val <caret>some = foo(i) ?: continue
|
||||
when (some) {
|
||||
"some" -> some
|
||||
else -> ""
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun foo(i: Int): String? = ""
|
||||
+10
@@ -6093,6 +6093,16 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
|
||||
}
|
||||
|
||||
@TestMetadata("hasBreak.kt")
|
||||
public void testHasBreak() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/hasBreak.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("hasContinue.kt")
|
||||
public void testHasContinue() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/hasContinue.kt");
|
||||
}
|
||||
|
||||
@TestMetadata("hasReturn.kt")
|
||||
public void testHasReturn() throws Exception {
|
||||
runTest("idea/testData/inspectionsLocal/moveVariableDeclarationIntoWhen/hasReturn.kt");
|
||||
|
||||
Reference in New Issue
Block a user