RemoveRedundantQualifierNameInspection: fix case with non-static java functions

This commit is contained in:
Dmitry Gridin
2019-06-20 21:07:51 +07:00
parent 0b16c51baf
commit f1e2ba728f
12 changed files with 116 additions and 7 deletions
@@ -6,13 +6,16 @@
package org.jetbrains.kotlin.idea.inspections
import com.intellij.codeInspection.*
import com.intellij.lang.jvm.JvmModifier
import com.intellij.openapi.project.Project
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiElementVisitor
import com.intellij.psi.PsiMember
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.idea.analysis.analyzeAsReplacement
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.core.ShortenReferences
import org.jetbrains.kotlin.idea.core.compareDescriptors
import org.jetbrains.kotlin.idea.imports.importableFqName
import org.jetbrains.kotlin.idea.references.mainReference
import org.jetbrains.kotlin.idea.util.getResolutionScope
@@ -65,9 +68,13 @@ class RemoveRedundantQualifierNameInspection : AbstractKotlinInspection(), Clean
}
}
private tailrec fun KtDotQualifiedExpression.firstExpressionWithoutReceiver(): KtDotQualifiedExpression? =
if ((getQualifiedElementSelector()?.mainReference?.resolve() as? KtCallableDeclaration)?.receiverTypeReference == null) this
else (receiverExpression as? KtDotQualifiedExpression)?.firstExpressionWithoutReceiver()
private tailrec fun KtDotQualifiedExpression.firstExpressionWithoutReceiver(): KtDotQualifiedExpression? {
val element = getQualifiedElementSelector()?.mainReference?.resolve() ?: return null
return if (element is KtClassOrObject ||
element is KtCallableDeclaration && element.receiverTypeReference == null ||
element is PsiMember && element.hasModifier(JvmModifier.STATIC)
) this else (receiverExpression as? KtDotQualifiedExpression)?.firstExpressionWithoutReceiver()
}
private tailrec fun <T : KtElement> T.firstApplicableExpression(validator: T.() -> T?, generator: T.() -> T?): T? =
validator() ?: generator()?.firstApplicableExpression(validator, generator)
@@ -90,10 +97,11 @@ private fun KtDotQualifiedExpression.applicableExpression(
?.firstOrNull() ?: return null
return takeIf {
if (newDescriptor is ImportedFromObjectCallableDescriptor<*>)
originalDescriptor == newDescriptor.callableFromObject
else
originalDescriptor == newDescriptor
originalDescriptor.importableFqName == newDescriptor.importableFqName &&
if (newDescriptor is ImportedFromObjectCallableDescriptor<*>)
compareDescriptors(project, newDescriptor.callableFromObject, originalDescriptor)
else
compareDescriptors(project, newDescriptor, originalDescriptor)
}
}
@@ -0,0 +1,11 @@
package my;
public class Reproducer {
public static Reproducer test() {
return new Reproducer();
}
public int number() {
return 42;
}
}
@@ -0,0 +1,9 @@
package my.simple.name
import my.Reproducer
fun test() = Reproducer()
fun main() {
Reproducer.test().number()
}
@@ -0,0 +1,11 @@
package my;
public class Reproducer {
public static Reproducer test() {
return new Reproducer();
}
public int number() {
return 42;
}
}
@@ -0,0 +1,9 @@
package my.simple.name
import my.Reproducer
fun test() = Reproducer()
fun main() {
Reproducer<caret>.test().number()
}
@@ -0,0 +1,5 @@
{
"inspectionClass": "org.jetbrains.kotlin.idea.inspections.RemoveRedundantQualifierNameInspection",
"problem": "none",
"mainFile": "test.kt"
}
@@ -0,0 +1,11 @@
package my;
public class Reproducer {
public static Reproducer test() {
return new Reproducer();
}
public int number() {
return 42;
}
}
@@ -0,0 +1,10 @@
package my.simple.name
import my.Reproducer
import my.Reproducer.test
fun test() = Reproducer()
fun main() {
test().number()
}
@@ -0,0 +1,11 @@
package my;
public class Reproducer {
public static Reproducer test() {
return new Reproducer();
}
public int number() {
return 42;
}
}
@@ -0,0 +1,10 @@
package my.simple.name
import my.Reproducer
import my.Reproducer.test
fun test() = Reproducer()
fun main() {
Reproducer<caret>.test().number()
}
@@ -0,0 +1,4 @@
{
"inspectionClass": "org.jetbrains.kotlin.idea.inspections.RemoveRedundantQualifierNameInspection",
"mainFile": "test.kt"
}
@@ -89,6 +89,16 @@ public class MultiFileLocalInspectionTestGenerated extends AbstractMultiFileLoca
runTest("idea/testData/multiFileLocalInspections/reconcilePackageWithDirectory/packageMatchesDirectory/packageMatchesDirectory.test");
}
@TestMetadata("redundantQualifierName/javaStatic2/fromKotlinTest.test")
public void testRedundantQualifierName_javaStatic2_FromKotlinTest() throws Exception {
runTest("idea/testData/multiFileLocalInspections/redundantQualifierName/javaStatic2/fromKotlinTest.test");
}
@TestMetadata("redundantQualifierName/javaStatic/fromKotlinTest.test")
public void testRedundantQualifierName_javaStatic_FromKotlinTest() throws Exception {
runTest("idea/testData/multiFileLocalInspections/redundantQualifierName/javaStatic/fromKotlinTest.test");
}
@TestMetadata("unusedSymbol/fromKotlinTest/fromKotlinTest.test")
public void testUnusedSymbol_fromKotlinTest_FromKotlinTest() throws Exception {
runTest("idea/testData/multiFileLocalInspections/unusedSymbol/fromKotlinTest/fromKotlinTest.test");