Explicit this inspection: suggest also for synthetic Java property
So #KT-22089 Fixed
This commit is contained in:
committed by
Mikhail Glukhikh
parent
6b37e40f99
commit
4038d4d507
@@ -23,11 +23,13 @@ import com.intellij.codeInspection.ProblemsHolder
|
|||||||
import com.intellij.openapi.project.Project
|
import com.intellij.openapi.project.Project
|
||||||
import org.jetbrains.kotlin.config.LanguageFeature.CallableReferencesToClassMembersWithEmptyLHS
|
import org.jetbrains.kotlin.config.LanguageFeature.CallableReferencesToClassMembersWithEmptyLHS
|
||||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||||
|
import org.jetbrains.kotlin.idea.intentions.getCallableDescriptor
|
||||||
import org.jetbrains.kotlin.idea.project.languageVersionSettings
|
import org.jetbrains.kotlin.idea.project.languageVersionSettings
|
||||||
import org.jetbrains.kotlin.idea.util.*
|
import org.jetbrains.kotlin.idea.util.*
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
import org.jetbrains.kotlin.psi.psiUtil.getChildOfType
|
import org.jetbrains.kotlin.psi.psiUtil.getChildOfType
|
||||||
import org.jetbrains.kotlin.resolve.BindingContext
|
import org.jetbrains.kotlin.resolve.BindingContext
|
||||||
|
import org.jetbrains.kotlin.synthetic.SyntheticJavaPropertyDescriptor
|
||||||
|
|
||||||
class ExplicitThisInspection : AbstractKotlinInspection() {
|
class ExplicitThisInspection : AbstractKotlinInspection() {
|
||||||
|
|
||||||
@@ -52,15 +54,16 @@ class ExplicitThisInspection : AbstractKotlinInspection() {
|
|||||||
val context = expression.analyze()
|
val context = expression.analyze()
|
||||||
val scope = expression.getResolutionScope(context) ?: return
|
val scope = expression.getResolutionScope(context) ?: return
|
||||||
|
|
||||||
val referenceExpression = reference as? KtNameReferenceExpression
|
val referenceExpression = reference as? KtNameReferenceExpression ?: reference.getChildOfType() ?: return
|
||||||
?: reference.getChildOfType()
|
|
||||||
?: return
|
|
||||||
|
|
||||||
//we avoid overload-related problems by enforcing that there is only one candidate
|
//we avoid overload-related problems by enforcing that there is only one candidate
|
||||||
val name = referenceExpression.getReferencedNameAsName()
|
val name = referenceExpression.getReferencedNameAsName()
|
||||||
val candidates = scope.getAllAccessibleVariables(name) + scope.getAllAccessibleFunctions(name)
|
val candidates = scope.getAllAccessibleVariables(name) + scope.getAllAccessibleFunctions(name)
|
||||||
if (candidates.size != 1) return
|
if (referenceExpression.getCallableDescriptor() is SyntheticJavaPropertyDescriptor) {
|
||||||
|
if (candidates.map { it.containingDeclaration }.distinct().size != 1) return
|
||||||
|
} else {
|
||||||
|
if (candidates.size != 1) return
|
||||||
|
}
|
||||||
|
|
||||||
val receiverType = context[BindingContext.EXPRESSION_TYPE_INFO, thisExpression]?.type ?: return
|
val receiverType = context[BindingContext.EXPRESSION_TYPE_INFO, thisExpression]?.type ?: return
|
||||||
val expressionFactory = scope.getFactoryForImplicitReceiverWithSubtypeOf(receiverType) ?: return
|
val expressionFactory = scope.getFactoryForImplicitReceiverWithSubtypeOf(receiverType) ?: return
|
||||||
|
|||||||
@@ -0,0 +1,11 @@
|
|||||||
|
public class Foo {
|
||||||
|
private String s;
|
||||||
|
|
||||||
|
public String getS() {
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setS(String s) {
|
||||||
|
this.s = s;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
|
||||||
|
fun test1() {
|
||||||
|
Foo().apply {
|
||||||
|
<caret>this.s = ""
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
|
||||||
|
fun test1() {
|
||||||
|
Foo().apply {
|
||||||
|
<caret>s = ""
|
||||||
|
}
|
||||||
|
}
|
||||||
+23
@@ -0,0 +1,23 @@
|
|||||||
|
public class Foo {
|
||||||
|
private boolean isB;
|
||||||
|
|
||||||
|
public boolean isB() {
|
||||||
|
return isB;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setB(boolean b) {
|
||||||
|
isB = b;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Bar {
|
||||||
|
private boolean isB;
|
||||||
|
|
||||||
|
public boolean isB() {
|
||||||
|
return isB;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setB(boolean b) {
|
||||||
|
isB = b;
|
||||||
|
}
|
||||||
|
}
|
||||||
+10
@@ -0,0 +1,10 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// PROBLEM: none
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
Foo().apply {
|
||||||
|
Bar().run {
|
||||||
|
<caret>this@apply.isB = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Vendored
+11
@@ -0,0 +1,11 @@
|
|||||||
|
public class Foo {
|
||||||
|
private boolean isB;
|
||||||
|
|
||||||
|
public boolean isB() {
|
||||||
|
return isB;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setB(boolean b) {
|
||||||
|
isB = b;
|
||||||
|
}
|
||||||
|
}
|
||||||
Vendored
+10
@@ -0,0 +1,10 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// PROBLEM: none
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
Foo().apply {
|
||||||
|
Foo().run {
|
||||||
|
<caret>this@apply.isB = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Vendored
+11
@@ -0,0 +1,11 @@
|
|||||||
|
public class Foo {
|
||||||
|
private boolean isB;
|
||||||
|
|
||||||
|
public boolean isB() {
|
||||||
|
return isB;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setB(boolean b) {
|
||||||
|
isB = b;
|
||||||
|
}
|
||||||
|
}
|
||||||
+7
@@ -0,0 +1,7 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
Foo().apply {
|
||||||
|
<caret>this.isB = true
|
||||||
|
}
|
||||||
|
}
|
||||||
Vendored
+7
@@ -0,0 +1,7 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
Foo().apply {
|
||||||
|
<caret>isB = true
|
||||||
|
}
|
||||||
|
}
|
||||||
+11
@@ -0,0 +1,11 @@
|
|||||||
|
public class Foo {
|
||||||
|
private boolean isB;
|
||||||
|
|
||||||
|
public boolean isB() {
|
||||||
|
return isB;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setB(boolean b) {
|
||||||
|
isB = b;
|
||||||
|
}
|
||||||
|
}
|
||||||
+11
@@ -0,0 +1,11 @@
|
|||||||
|
// WITH_RUNTIME
|
||||||
|
// PROBLEM: none
|
||||||
|
|
||||||
|
fun test() {
|
||||||
|
|
||||||
|
val isB = true
|
||||||
|
|
||||||
|
Foo().apply {
|
||||||
|
<caret>this.isB = true
|
||||||
|
}
|
||||||
|
}
|
||||||
+25
@@ -1629,6 +1629,31 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
|||||||
runTest("idea/testData/inspectionsLocal/explicitThis/propertyReference.kt");
|
runTest("idea/testData/inspectionsLocal/explicitThis/propertyReference.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("syntheticJavaProperty.kt")
|
||||||
|
public void testSyntheticJavaProperty() throws Exception {
|
||||||
|
runTest("idea/testData/inspectionsLocal/explicitThis/syntheticJavaProperty.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("syntheticJavaProperty_nestedReceiver.kt")
|
||||||
|
public void testSyntheticJavaProperty_nestedReceiver() throws Exception {
|
||||||
|
runTest("idea/testData/inspectionsLocal/explicitThis/syntheticJavaProperty_nestedReceiver.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("syntheticJavaProperty_sameClassNestedReceiver.kt")
|
||||||
|
public void testSyntheticJavaProperty_sameClassNestedReceiver() throws Exception {
|
||||||
|
runTest("idea/testData/inspectionsLocal/explicitThis/syntheticJavaProperty_sameClassNestedReceiver.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("syntheticJavaProperty_sameNameJavaProperty.kt")
|
||||||
|
public void testSyntheticJavaProperty_sameNameJavaProperty() throws Exception {
|
||||||
|
runTest("idea/testData/inspectionsLocal/explicitThis/syntheticJavaProperty_sameNameJavaProperty.kt");
|
||||||
|
}
|
||||||
|
|
||||||
|
@TestMetadata("syntheticJavaProperty_sameNameVariable.kt")
|
||||||
|
public void testSyntheticJavaProperty_sameNameVariable() throws Exception {
|
||||||
|
runTest("idea/testData/inspectionsLocal/explicitThis/syntheticJavaProperty_sameNameVariable.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("variableWithSameName.kt")
|
@TestMetadata("variableWithSameName.kt")
|
||||||
public void testVariableWithSameName() throws Exception {
|
public void testVariableWithSameName() throws Exception {
|
||||||
runTest("idea/testData/inspectionsLocal/explicitThis/variableWithSameName.kt");
|
runTest("idea/testData/inspectionsLocal/explicitThis/variableWithSameName.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user