RemoveRedundantQualifierNameInspection: fix case with non-static java functions
This commit is contained in:
+15
-7
@@ -6,13 +6,16 @@
|
|||||||
package org.jetbrains.kotlin.idea.inspections
|
package org.jetbrains.kotlin.idea.inspections
|
||||||
|
|
||||||
import com.intellij.codeInspection.*
|
import com.intellij.codeInspection.*
|
||||||
|
import com.intellij.lang.jvm.JvmModifier
|
||||||
import com.intellij.openapi.project.Project
|
import com.intellij.openapi.project.Project
|
||||||
import com.intellij.openapi.util.TextRange
|
import com.intellij.openapi.util.TextRange
|
||||||
import com.intellij.psi.PsiElementVisitor
|
import com.intellij.psi.PsiElementVisitor
|
||||||
|
import com.intellij.psi.PsiMember
|
||||||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
|
||||||
import org.jetbrains.kotlin.idea.analysis.analyzeAsReplacement
|
import org.jetbrains.kotlin.idea.analysis.analyzeAsReplacement
|
||||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||||
import org.jetbrains.kotlin.idea.core.ShortenReferences
|
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.imports.importableFqName
|
||||||
import org.jetbrains.kotlin.idea.references.mainReference
|
import org.jetbrains.kotlin.idea.references.mainReference
|
||||||
import org.jetbrains.kotlin.idea.util.getResolutionScope
|
import org.jetbrains.kotlin.idea.util.getResolutionScope
|
||||||
@@ -65,9 +68,13 @@ class RemoveRedundantQualifierNameInspection : AbstractKotlinInspection(), Clean
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private tailrec fun KtDotQualifiedExpression.firstExpressionWithoutReceiver(): KtDotQualifiedExpression? =
|
private tailrec fun KtDotQualifiedExpression.firstExpressionWithoutReceiver(): KtDotQualifiedExpression? {
|
||||||
if ((getQualifiedElementSelector()?.mainReference?.resolve() as? KtCallableDeclaration)?.receiverTypeReference == null) this
|
val element = getQualifiedElementSelector()?.mainReference?.resolve() ?: return null
|
||||||
else (receiverExpression as? KtDotQualifiedExpression)?.firstExpressionWithoutReceiver()
|
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? =
|
private tailrec fun <T : KtElement> T.firstApplicableExpression(validator: T.() -> T?, generator: T.() -> T?): T? =
|
||||||
validator() ?: generator()?.firstApplicableExpression(validator, generator)
|
validator() ?: generator()?.firstApplicableExpression(validator, generator)
|
||||||
@@ -90,10 +97,11 @@ private fun KtDotQualifiedExpression.applicableExpression(
|
|||||||
?.firstOrNull() ?: return null
|
?.firstOrNull() ?: return null
|
||||||
|
|
||||||
return takeIf {
|
return takeIf {
|
||||||
if (newDescriptor is ImportedFromObjectCallableDescriptor<*>)
|
originalDescriptor.importableFqName == newDescriptor.importableFqName &&
|
||||||
originalDescriptor == newDescriptor.callableFromObject
|
if (newDescriptor is ImportedFromObjectCallableDescriptor<*>)
|
||||||
else
|
compareDescriptors(project, newDescriptor.callableFromObject, originalDescriptor)
|
||||||
originalDescriptor == newDescriptor
|
else
|
||||||
|
compareDescriptors(project, newDescriptor, originalDescriptor)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Vendored
+11
@@ -0,0 +1,11 @@
|
|||||||
|
package my;
|
||||||
|
|
||||||
|
public class Reproducer {
|
||||||
|
public static Reproducer test() {
|
||||||
|
return new Reproducer();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int number() {
|
||||||
|
return 42;
|
||||||
|
}
|
||||||
|
}
|
||||||
+9
@@ -0,0 +1,9 @@
|
|||||||
|
package my.simple.name
|
||||||
|
|
||||||
|
import my.Reproducer
|
||||||
|
|
||||||
|
fun test() = Reproducer()
|
||||||
|
|
||||||
|
fun main() {
|
||||||
|
Reproducer.test().number()
|
||||||
|
}
|
||||||
Vendored
+11
@@ -0,0 +1,11 @@
|
|||||||
|
package my;
|
||||||
|
|
||||||
|
public class Reproducer {
|
||||||
|
public static Reproducer test() {
|
||||||
|
return new Reproducer();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int number() {
|
||||||
|
return 42;
|
||||||
|
}
|
||||||
|
}
|
||||||
+9
@@ -0,0 +1,9 @@
|
|||||||
|
package my.simple.name
|
||||||
|
|
||||||
|
import my.Reproducer
|
||||||
|
|
||||||
|
fun test() = Reproducer()
|
||||||
|
|
||||||
|
fun main() {
|
||||||
|
Reproducer<caret>.test().number()
|
||||||
|
}
|
||||||
Vendored
+5
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"inspectionClass": "org.jetbrains.kotlin.idea.inspections.RemoveRedundantQualifierNameInspection",
|
||||||
|
"problem": "none",
|
||||||
|
"mainFile": "test.kt"
|
||||||
|
}
|
||||||
Vendored
+11
@@ -0,0 +1,11 @@
|
|||||||
|
package my;
|
||||||
|
|
||||||
|
public class Reproducer {
|
||||||
|
public static Reproducer test() {
|
||||||
|
return new Reproducer();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int number() {
|
||||||
|
return 42;
|
||||||
|
}
|
||||||
|
}
|
||||||
+10
@@ -0,0 +1,10 @@
|
|||||||
|
package my.simple.name
|
||||||
|
|
||||||
|
import my.Reproducer
|
||||||
|
import my.Reproducer.test
|
||||||
|
|
||||||
|
fun test() = Reproducer()
|
||||||
|
|
||||||
|
fun main() {
|
||||||
|
test().number()
|
||||||
|
}
|
||||||
idea/testData/multiFileLocalInspections/redundantQualifierName/javaStatic2/before/my/Reproducer.java
Vendored
+11
@@ -0,0 +1,11 @@
|
|||||||
|
package my;
|
||||||
|
|
||||||
|
public class Reproducer {
|
||||||
|
public static Reproducer test() {
|
||||||
|
return new Reproducer();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int number() {
|
||||||
|
return 42;
|
||||||
|
}
|
||||||
|
}
|
||||||
+10
@@ -0,0 +1,10 @@
|
|||||||
|
package my.simple.name
|
||||||
|
|
||||||
|
import my.Reproducer
|
||||||
|
import my.Reproducer.test
|
||||||
|
|
||||||
|
fun test() = Reproducer()
|
||||||
|
|
||||||
|
fun main() {
|
||||||
|
Reproducer<caret>.test().number()
|
||||||
|
}
|
||||||
Vendored
+4
@@ -0,0 +1,4 @@
|
|||||||
|
{
|
||||||
|
"inspectionClass": "org.jetbrains.kotlin.idea.inspections.RemoveRedundantQualifierNameInspection",
|
||||||
|
"mainFile": "test.kt"
|
||||||
|
}
|
||||||
Generated
+10
@@ -89,6 +89,16 @@ public class MultiFileLocalInspectionTestGenerated extends AbstractMultiFileLoca
|
|||||||
runTest("idea/testData/multiFileLocalInspections/reconcilePackageWithDirectory/packageMatchesDirectory/packageMatchesDirectory.test");
|
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")
|
@TestMetadata("unusedSymbol/fromKotlinTest/fromKotlinTest.test")
|
||||||
public void testUnusedSymbol_fromKotlinTest_FromKotlinTest() throws Exception {
|
public void testUnusedSymbol_fromKotlinTest_FromKotlinTest() throws Exception {
|
||||||
runTest("idea/testData/multiFileLocalInspections/unusedSymbol/fromKotlinTest/fromKotlinTest.test");
|
runTest("idea/testData/multiFileLocalInspections/unusedSymbol/fromKotlinTest/fromKotlinTest.test");
|
||||||
|
|||||||
Reference in New Issue
Block a user