Introduced "Unused receiver parameter" inspection.

This commit is contained in:
Evgeny Gerashchenko
2015-03-19 20:57:10 +03:00
parent edd51908ec
commit 1946360a80
16 changed files with 181 additions and 0 deletions
@@ -127,6 +127,9 @@ unused.function=Function ''{0}'' is never used
unused.property=Property ''{0}'' is never used
unused.type.parameter=Type parameter ''{0}'' is never used
unused.receiver.parameter=Receiver parameter is never used
unused.receiver.parameter.remove=Remove redundant receiver parameter
livetemplate.description.main=main() function
livetemplate.description.soutp=Prints function parameter names and values to System.out
livetemplate.description.iter=Iterate over elements of iterable (for-in loop)
@@ -0,0 +1,5 @@
<html>
<body>
This inspection reports unused receiver parameter of extension functions and properties.
</body>
</html>
+7
View File
@@ -864,6 +864,13 @@
level="WARNING"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.inspections.UnusedReceiverParameterInspection"
displayName="Unused receiver parameter"
groupName="Kotlin"
enabledByDefault="true"
level="WARNING"
/>
<localInspection implementationClass="org.jetbrains.kotlin.idea.kdoc.KDocUnresolvedReferenceInspection"
displayName="Unresolved reference in KDoc"
groupName="Kotlin"
@@ -0,0 +1,78 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.idea.inspections
import com.intellij.codeInspection.*
import com.intellij.extapi.psi.ASTDelegatePsiElement
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiElementVisitor
import org.jetbrains.kotlin.descriptors.ReceiverParameterDescriptor
import org.jetbrains.kotlin.idea.JetBundle
import org.jetbrains.kotlin.idea.caches.resolve.analyze
import org.jetbrains.kotlin.idea.search.usagesSearch.descriptor
import org.jetbrains.kotlin.lexer.JetTokens
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.isOverridable
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
import org.jetbrains.kotlin.resolve.scopes.receivers.ExtensionReceiver
public class UnusedReceiverParameterInspection : AbstractKotlinInspection() {
override fun buildVisitor(holder: ProblemsHolder, isOnTheFly: Boolean, session: LocalInspectionToolSession): PsiElementVisitor {
return object : JetVisitorVoid() {
private fun check(callableDeclaration: JetCallableDeclaration) {
val receiverTypeReference = callableDeclaration.getReceiverTypeReference()
if (receiverTypeReference == null) return
if (callableDeclaration.isOverridable() || callableDeclaration.hasModifier(JetTokens.OVERRIDE_KEYWORD)) return
val callable = callableDeclaration.descriptor
var used = false
callableDeclaration.acceptChildren(object : JetVisitorVoid() {
override fun visitJetElement(element: JetElement) {
if (used) return
element.acceptChildren(this)
val bindingContext = element.analyze()
val resolvedCall = element.getResolvedCall(bindingContext) ?: return
if ((resolvedCall.getDispatchReceiver() as? ExtensionReceiver)?.getDeclarationDescriptor() == callable) {
used = true
}
else if (((resolvedCall.getExtensionReceiver() as? ExtensionReceiver)?.getDeclarationDescriptor() == callable)) {
used = true
}
else if ((resolvedCall.getCandidateDescriptor() as? ReceiverParameterDescriptor)?.getContainingDeclaration() == callable) {
used = true
}
}
})
if (!used) {
holder.registerProblem(receiverTypeReference, JetBundle.message("unused.receiver.parameter"), ProblemHighlightType.LIKE_UNUSED_SYMBOL) // TODO add quick fix
}
}
override fun visitNamedFunction(function: JetNamedFunction) {
check(function)
}
override fun visitProperty(property: JetProperty) {
check(property)
}
}
}
}
@@ -0,0 +1,3 @@
fun String.foo() {
println(length())
}
@@ -0,0 +1,5 @@
private fun String.foo() {
otherExt()
}
fun String.otherExt() = length()
@@ -0,0 +1,34 @@
<problems>
<problem>
<file>unusedInProperty.kt</file>
<line>1</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/unusedInProperty.kt" />
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused receiver parameter</problem_class>
<description>Receiver parameter is never used</description>
</problem>
<problem>
<file>unusedInFunction.kt</file>
<line>1</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/unusedInFunction.kt" />
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused receiver parameter</problem_class>
<description>Receiver parameter is never used</description>
</problem>
<problem>
<file>irrelevantThisInFunction.kt</file>
<line>1</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/irrelevantThisInFunction.kt" />
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused receiver parameter</problem_class>
<description>Receiver parameter is never used</description>
</problem>
<problem>
<file>irrelevantCallInFunction.kt</file>
<line>1</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/irrelevantCallInFunction.kt" />
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused receiver parameter</problem_class>
<description>Receiver parameter is never used</description>
</problem>
</problems>
@@ -0,0 +1 @@
// INSPECTION_CLASS: org.jetbrains.kotlin.idea.inspections.UnusedReceiverParameterInspection
@@ -0,0 +1,5 @@
fun String.foo() {
fun String.local() {
println(length())
}
}
@@ -0,0 +1,5 @@
fun Any.foo() {
fun Any.local() {
println(this)
}
}
@@ -0,0 +1,11 @@
open class Foo {
open fun Any.foo() {
}
}
class Bar: Foo() {
override fun Any.foo() {
}
}
@@ -0,0 +1,3 @@
fun String.foo() {
println()
}
@@ -0,0 +1,6 @@
val String.something: Int
get() = 42
fun main(args: Array<String>) {
"".something
}
@@ -0,0 +1,3 @@
fun String.foo() {
println(this)
}
@@ -0,0 +1,6 @@
val String.doubleLength: Int
get() = length() * 2
fun main(args: Array<String>) {
"".doubleLength
}
@@ -87,6 +87,12 @@ public class JetInspectionTestGenerated extends AbstractJetInspectionTest {
doTest(fileName);
}
@TestMetadata("unusedReceiverParameter/inspectionData/inspections.test")
public void testUnusedReceiverParameter_inspectionData_Inspections_test() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/inspections/unusedReceiverParameter/inspectionData/inspections.test");
doTest(fileName);
}
@TestMetadata("unusedSymbol/class/inspectionData/inspections.test")
public void testUnusedSymbol_class_inspectionData_Inspections_test() throws Exception {
String fileName = JetTestUtils.navigationMetadata("idea/testData/inspections/unusedSymbol/class/inspectionData/inspections.test");