Lift assignment out: do not report when assignment variables are different

#KT-38649 Fixed
This commit is contained in:
Toshiaki Kameyama
2020-04-30 13:38:30 +09:00
committed by Yan Zhulanow
parent 498c40548b
commit 5efbbdea57
3 changed files with 27 additions and 1 deletions
@@ -21,6 +21,7 @@ import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.core.replaced
import org.jetbrains.kotlin.idea.intentions.branches
import org.jetbrains.kotlin.idea.references.mainReference
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.anyDescendantOfType
@@ -66,7 +67,9 @@ object BranchedFoldingUtils {
): Boolean {
val left = this.left ?: return false
val otherLeft = other.left ?: return false
if (left.text != otherLeft.text || operationToken != other.operationToken) return false
if (left.text != otherLeft.text || operationToken != other.operationToken ||
left.mainReference?.resolve() != otherLeft.mainReference?.resolve()
) return false
val rightType = other.rightType() ?: return false
return rightType.constructor == rightTypeConstructor || (operationToken == KtTokens.EQ && rightType.isSubtypeOf(leftType))
}
@@ -0,0 +1,18 @@
// PROBLEM: none
// WITH_RUNTIME
class A {
val list: MutableList<String> = mutableListOf()
}
class B {
val list: MutableList<String> = mutableListOf()
}
fun Any.add(s: String) {
<caret>when (this) {
is A -> list += s
is B -> list += s
else -> throw IllegalStateException()
}
}
@@ -5707,6 +5707,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
runTest("idea/testData/inspectionsLocal/liftOut/whenToAssignment/cascadeWhen.kt");
}
@TestMetadata("differentVariablesWithSame.kt")
public void testDifferentVariablesWithSame() throws Exception {
runTest("idea/testData/inspectionsLocal/liftOut/whenToAssignment/differentVariablesWithSame.kt");
}
@TestMetadata("innerWhenTransformed.kt")
public void testInnerWhenTransformed() throws Exception {
runTest("idea/testData/inspectionsLocal/liftOut/whenToAssignment/innerWhenTransformed.kt");