Redundant overriding method: do not report when super method is not called
#KT-34959 Fixed
This commit is contained in:
committed by
klunnii
parent
76ed09482f
commit
a2bde2ffb2
@@ -14,6 +14,7 @@ import org.jetbrains.kotlin.descriptors.FunctionDescriptor
|
|||||||
import org.jetbrains.kotlin.descriptors.Modality
|
import org.jetbrains.kotlin.descriptors.Modality
|
||||||
import org.jetbrains.kotlin.idea.KotlinBundle
|
import org.jetbrains.kotlin.idea.KotlinBundle
|
||||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||||
|
import org.jetbrains.kotlin.idea.caches.resolve.resolveToCall
|
||||||
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
|
import org.jetbrains.kotlin.idea.caches.resolve.resolveToDescriptorIfAny
|
||||||
import org.jetbrains.kotlin.lexer.KtTokens
|
import org.jetbrains.kotlin.lexer.KtTokens
|
||||||
import org.jetbrains.kotlin.load.java.JavaDescriptorVisibilities
|
import org.jetbrains.kotlin.load.java.JavaDescriptorVisibilities
|
||||||
@@ -58,8 +59,15 @@ class KotlinRedundantOverrideInspection : AbstractKotlinInspection(), CleanupLoc
|
|||||||
val superCallElement = qualifiedExpression.selectorExpression as? KtCallElement ?: return
|
val superCallElement = qualifiedExpression.selectorExpression as? KtCallElement ?: return
|
||||||
if (!isSameFunctionName(superCallElement, function)) return
|
if (!isSameFunctionName(superCallElement, function)) return
|
||||||
if (!isSameArguments(superCallElement, function)) return
|
if (!isSameArguments(superCallElement, function)) return
|
||||||
|
|
||||||
val context = function.analyze()
|
val context = function.analyze()
|
||||||
val functionDescriptor = context[BindingContext.FUNCTION, function]
|
val functionDescriptor = context[BindingContext.FUNCTION, function]
|
||||||
|
val functionParams = functionDescriptor?.valueParameters.orEmpty()
|
||||||
|
val superCallFunctionParams = superCallElement.resolveToCall()?.resultingDescriptor?.valueParameters.orEmpty()
|
||||||
|
if (functionParams.size == superCallFunctionParams.size
|
||||||
|
&& functionParams.zip(superCallFunctionParams).any { it.first.type != it.second.type }
|
||||||
|
) return
|
||||||
|
|
||||||
if (function.hasDerivedProperty(functionDescriptor, context)) return
|
if (function.hasDerivedProperty(functionDescriptor, context)) return
|
||||||
val overriddenDescriptors = functionDescriptor?.original?.overriddenDescriptors
|
val overriddenDescriptors = functionDescriptor?.original?.overriddenDescriptors
|
||||||
if (overriddenDescriptors?.any { it is JavaMethodDescriptor && it.visibility == JavaDescriptorVisibilities.PACKAGE_VISIBILITY } == true) return
|
if (overriddenDescriptors?.any { it is JavaMethodDescriptor && it.visibility == JavaDescriptorVisibilities.PACKAGE_VISIBILITY } == true) return
|
||||||
|
|||||||
+16
@@ -0,0 +1,16 @@
|
|||||||
|
// PROBLEM: none
|
||||||
|
|
||||||
|
interface I {
|
||||||
|
fun foo(parent: String)
|
||||||
|
}
|
||||||
|
|
||||||
|
open class A {
|
||||||
|
open fun foo(parent: Any) {}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class B : A(), I {
|
||||||
|
override <caret>fun foo(parent: String) {
|
||||||
|
super.foo(parent)
|
||||||
|
}
|
||||||
|
}
|
||||||
+5
@@ -8419,6 +8419,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
|
|||||||
runTest("idea/testData/inspectionsLocal/redundantOverride/callDifferentSuperMethod.kt");
|
runTest("idea/testData/inspectionsLocal/redundantOverride/callDifferentSuperMethod.kt");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@TestMetadata("callDifferentSuperMethod2.kt")
|
||||||
|
public void testCallDifferentSuperMethod2() throws Exception {
|
||||||
|
runTest("idea/testData/inspectionsLocal/redundantOverride/callDifferentSuperMethod2.kt");
|
||||||
|
}
|
||||||
|
|
||||||
@TestMetadata("classAndInterface.kt")
|
@TestMetadata("classAndInterface.kt")
|
||||||
public void testClassAndInterface() throws Exception {
|
public void testClassAndInterface() throws Exception {
|
||||||
runTest("idea/testData/inspectionsLocal/redundantOverride/classAndInterface.kt");
|
runTest("idea/testData/inspectionsLocal/redundantOverride/classAndInterface.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user