any() and other find operations to work with checks for not null and is instance

This commit is contained in:
Valentin Kipyatkov
2016-10-14 16:58:40 +03:00
parent 49ef880f3a
commit 35ed689b9f
6 changed files with 34 additions and 4 deletions
@@ -20,7 +20,7 @@ import org.jetbrains.kotlin.descriptors.VariableDescriptor
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptor
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.isNullExpression
import org.jetbrains.kotlin.idea.intentions.loopToCallChain.*
import org.jetbrains.kotlin.idea.intentions.loopToCallChain.sequence.FilterTransformation
import org.jetbrains.kotlin.idea.intentions.loopToCallChain.sequence.FilterTransformationBase
import org.jetbrains.kotlin.idea.intentions.negate
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
@@ -61,7 +61,7 @@ object FindTransformationMatcher : TransformationMatcher {
return matchWithFilterBefore(state, null)
}
fun matchWithFilterBefore(state: MatchingState, filterTransformation: FilterTransformation?): TransformationMatch.Result? {
fun matchWithFilterBefore(state: MatchingState, filterTransformation: FilterTransformationBase?): TransformationMatch.Result? {
matchReturn(state, filterTransformation)?.let { return it }
when (state.statements.size) {
@@ -98,7 +98,7 @@ object FindTransformationMatcher : TransformationMatcher {
return TransformationMatch.Result(transformation)
}
private fun matchReturn(state: MatchingState, filterTransformation: FilterTransformation?): TransformationMatch.Result? {
private fun matchReturn(state: MatchingState, filterTransformation: FilterTransformationBase?): TransformationMatch.Result? {
val returnInLoop = state.statements.singleOrNull() as? KtReturnExpression ?: return null
val returnAfterLoop = state.outerLoop.nextStatement() as? KtReturnExpression ?: return null
if (returnInLoop.getLabelName() != returnAfterLoop.getLabelName()) return null
@@ -216,7 +216,7 @@ object FindTransformationMatcher : TransformationMatcher {
loop: KtForExpression,
inputVariable: KtCallableDeclaration,
indexVariable: KtCallableDeclaration?,
filterTransformation: FilterTransformation?,
filterTransformation: FilterTransformationBase?,
valueIfFound: KtExpression,
valueIfNotFound: KtExpression,
findFirst: Boolean
@@ -80,6 +80,9 @@ abstract class FilterTransformationBase : SequenceTransformation {
currentState = nextState
}
}
if (transformation is FilterTransformationBase) {
FindTransformationMatcher.matchWithFilterBefore(currentState, transformation)
?.let { return it }
}
@@ -0,0 +1,9 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'any{}'"
// IS_APPLICABLE_2: false
fun foo(list: List<Any?>): Boolean {
<caret>for (a in list) {
if (a != null) return true
}
return false
}
@@ -0,0 +1,6 @@
// WITH_RUNTIME
// INTENTION_TEXT: "Replace with 'any{}'"
// IS_APPLICABLE_2: false
fun foo(list: List<Any?>): Boolean {
<caret>return list.any { it != null }
}
@@ -121,6 +121,12 @@ public class IntentionTest2Generated extends AbstractIntentionTest2 {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/loopToCallChain/any"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
}
@TestMetadata("anyNotNull.kt")
public void testAnyNotNull() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/any/anyNotNull.kt");
doTest(fileName);
}
@TestMetadata("ifAssign.kt")
public void testIfAssign() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/any/ifAssign.kt");
@@ -8029,6 +8029,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/intentions/loopToCallChain/any"), Pattern.compile("^([\\w\\-_]+)\\.kt$"), true);
}
@TestMetadata("anyNotNull.kt")
public void testAnyNotNull() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/any/anyNotNull.kt");
doTest(fileName);
}
@TestMetadata("ifAssign.kt")
public void testIfAssign() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/loopToCallChain/any/ifAssign.kt");