Redundant companion reference: fix false positive with overridden Java getter (#2935)

#KT-33771 Fixed
This commit is contained in:
Toshiaki Kameyama
2020-01-30 21:55:47 +09:00
committed by GitHub
parent 9f7af4b58d
commit 2f19ad7bdc
13 changed files with 62 additions and 24 deletions
@@ -31,7 +31,6 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.getSuperClassNotAny
import org.jetbrains.kotlin.resolve.descriptorUtil.getSuperInterfaces
import org.jetbrains.kotlin.resolve.scopes.utils.findFunction
import org.jetbrains.kotlin.resolve.scopes.utils.findVariable
import org.jetbrains.kotlin.types.typeUtil.isUnit
import org.jetbrains.kotlin.types.typeUtil.makeNotNullable
class RedundantCompanionReferenceInspection : AbstractKotlinInspection() {
@@ -72,16 +71,9 @@ class RedundantCompanionReferenceInspection : AbstractKotlinInspection() {
val type = selectorDescriptor.type
val javaGetter = containingClassDescriptor.findMemberFunction(
Name.identifier(JvmAbi.getterName(name.asString()))
) as? JavaMethodDescriptor
)?.takeIf { f -> f is JavaMethodDescriptor || f.overriddenDescriptors.any { it is JavaMethodDescriptor } }
if (javaGetter?.valueParameters?.isEmpty() == true && javaGetter.returnType?.makeNotNullable() == type) return false
val javaSetter = containingClassDescriptor.findMemberFunction(
Name.identifier(JvmAbi.setterName(name.asString()))
) as? JavaMethodDescriptor
if (javaSetter?.valueParameters?.singleOrNull()?.type?.makeNotNullable() == type
&& javaSetter.returnType?.makeNotNullable()?.isUnit() == true
) return false
val variable = reference.getResolutionScope().findVariable(name, NoLookupLocation.FROM_IDE)
if (variable != null && variable.isLocalOrExtension(containingClassDescriptor)) return false
}
@@ -1,5 +0,0 @@
public class Bar {
public String setBar(String bar) {
return ""
}
}
@@ -1,5 +0,0 @@
public class Bar {
public String setBar(String bar) {
return ""
}
}
@@ -0,0 +1,5 @@
public class Bar {
public String getBar() {
return "";
}
}
@@ -0,0 +1,11 @@
// PROBLEM: none
class Foo : Bar() {
override fun getBar(): String {
return <caret>Companion.bar
}
companion object {
val bar: String = "bar"
}
}
@@ -1,4 +1,8 @@
public class Bar {
public String getBar() {
return "";
}
public void setBar(String bar) {
}
}
@@ -0,0 +1,4 @@
public class Bar {
public void setBar(String bar) {
}
}
@@ -0,0 +1,4 @@
public class Bar {
public void setBar(String bar) {
}
}
@@ -0,0 +1,9 @@
class Foo {
fun getBar(): String {
return <caret>Companion.bar
}
companion object {
val bar: String = "bar"
}
}
@@ -0,0 +1,9 @@
class Foo {
fun getBar(): String {
return bar
}
companion object {
val bar: String = "bar"
}
}
@@ -6758,21 +6758,31 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
runTest("idea/testData/inspectionsLocal/redundantCompanionReference/javaFakeGetter.kt");
}
@TestMetadata("javaFakeSetter.kt")
public void testJavaFakeSetter() throws Exception {
runTest("idea/testData/inspectionsLocal/redundantCompanionReference/javaFakeSetter.kt");
}
@TestMetadata("javaGetter.kt")
public void testJavaGetter() throws Exception {
runTest("idea/testData/inspectionsLocal/redundantCompanionReference/javaGetter.kt");
}
@TestMetadata("javaGetter2.kt")
public void testJavaGetter2() throws Exception {
runTest("idea/testData/inspectionsLocal/redundantCompanionReference/javaGetter2.kt");
}
@TestMetadata("javaSetter.kt")
public void testJavaSetter() throws Exception {
runTest("idea/testData/inspectionsLocal/redundantCompanionReference/javaSetter.kt");
}
@TestMetadata("javaSetterWithoutGetter.kt")
public void testJavaSetterWithoutGetter() throws Exception {
runTest("idea/testData/inspectionsLocal/redundantCompanionReference/javaSetterWithoutGetter.kt");
}
@TestMetadata("kotlinGetter.kt")
public void testKotlinGetter() throws Exception {
runTest("idea/testData/inspectionsLocal/redundantCompanionReference/kotlinGetter.kt");
}
@TestMetadata("methodArgument.kt")
public void testMethodArgument() throws Exception {
runTest("idea/testData/inspectionsLocal/redundantCompanionReference/methodArgument.kt");