Join declaration & assignment: treat assignment with this. correctly #KT-16000 Fixed

This commit is contained in:
Mikhail Glukhikh
2017-02-01 18:07:43 +03:00
parent 84f324e04f
commit ea6e3c828d
3 changed files with 28 additions and 1 deletions
@@ -104,7 +104,15 @@ class JoinDeclarationAndAssignmentIntention : SelfTargetingOffsetIndependentInte
val assignments = mutableListOf<KtBinaryExpression>()
fun process(binaryExpr: KtBinaryExpression) {
if (binaryExpr.operationToken != KtTokens.EQ) return
val leftReference = binaryExpr.left as? KtNameReferenceExpression ?: return
val left = binaryExpr.left
val leftReference = when (left) {
is KtNameReferenceExpression ->
left
is KtDotQualifiedExpression ->
if (left.receiverExpression is KtThisExpression) left.selectorExpression as? KtNameReferenceExpression else null
else ->
null
} ?: return
if (leftReference.getReferencedName() != property.name) return
assignments += binaryExpr
}
@@ -0,0 +1,13 @@
// IS_APPLICABLE: false
class My {
val x: Int<caret>
constructor(x: Int) {
this.x = x
}
constructor() {
x = 42
}
}
@@ -8916,6 +8916,12 @@ public class IntentionTestGenerated extends AbstractIntentionTest {
doTest(fileName);
}
@TestMetadata("multipleConstructorsWithThis.kt")
public void testMultipleConstructorsWithThis() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/joinDeclarationAndAssignment/multipleConstructorsWithThis.kt");
doTest(fileName);
}
@TestMetadata("propertyReassignment.kt")
public void testPropertyReassignment() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("idea/testData/intentions/joinDeclarationAndAssignment/propertyReassignment.kt");