Suppressing unused parameter annotation when function is entry point.

This commit is contained in:
Evgeny Gerashchenko
2015-02-17 21:23:01 +03:00
parent 943131b7b0
commit b1840ebd99
7 changed files with 91 additions and 27 deletions
@@ -481,7 +481,7 @@ public interface Errors {
DiagnosticFactory1<JetSimpleNameExpression, ValueParameterDescriptor> UNINITIALIZED_PARAMETER = DiagnosticFactory1.create(ERROR);
DiagnosticFactory1<JetNamedDeclaration, VariableDescriptor> UNUSED_VARIABLE = DiagnosticFactory1.create(WARNING, DECLARATION_NAME);
DiagnosticFactory1<JetNamedDeclaration, VariableDescriptor> UNUSED_PARAMETER = DiagnosticFactory1.create(WARNING, DECLARATION_NAME);
DiagnosticFactory1<JetParameter, VariableDescriptor> UNUSED_PARAMETER = DiagnosticFactory1.create(WARNING, DECLARATION_NAME);
DiagnosticFactory1<JetNamedDeclaration, VariableDescriptor> ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE =
DiagnosticFactory1.create(WARNING, DECLARATION_NAME);
@@ -48,6 +48,6 @@ public class DuplicateJvmSignatureAnnotator implements Annotator {
Diagnostics diagnostics = AsJavaPackage.getJvmSignatureDiagnostics(element, otherDiagnostics, moduleScope);
if (diagnostics == null) return;
JetPsiChecker.annotateElement(element, holder, diagnostics);
new JetPsiChecker().annotateElement(element, holder, diagnostics);
}
}
@@ -45,6 +45,7 @@ import org.jetbrains.kotlin.idea.caches.resolve.*
import org.jetbrains.kotlin.idea.quickfix.QuickFixes
import kotlin.platform.platformStatic
import org.jetbrains.kotlin.idea.kdoc.KDocHighlightingVisitor
import org.jetbrains.kotlin.psi.JetParameter
public open class JetPsiChecker : Annotator, HighlightRangeExtension {
@@ -71,7 +72,18 @@ public open class JetPsiChecker : Annotator, HighlightRangeExtension {
return file is JetFile
}
private class ElementAnnotator(private val element: PsiElement, private val holder: AnnotationHolder) {
open protected fun shouldSuppressUnusedParameter(parameter: JetParameter): Boolean = false
fun annotateElement(element: PsiElement, holder: AnnotationHolder, diagnostics: Diagnostics) {
if (ProjectRootsUtil.isInProjectSource(element) || element.getContainingFile() is JetCodeFragment) {
val elementAnnotator = ElementAnnotator(element, holder)
for (diagnostic in diagnostics.forElement(element)) {
elementAnnotator.registerDiagnosticAnnotations(diagnostic)
}
}
}
private inner class ElementAnnotator(private val element: PsiElement, private val holder: AnnotationHolder) {
private var isMarkedWithRedeclaration = false
fun registerDiagnosticAnnotations(diagnostic: Diagnostic) {
@@ -130,6 +142,9 @@ public open class JetPsiChecker : Annotator, HighlightRangeExtension {
}
}
Severity.WARNING -> {
if (factory == Errors.UNUSED_PARAMETER && shouldSuppressUnusedParameter(diagnostic.getPsiElement() as JetParameter)) {
return
}
for (textRange in textRanges) {
val annotation = holder.createWarningAnnotation(textRange, getDefaultMessage(diagnostic))
setUpAnnotation(diagnostic, annotation, if (factory in Errors.UNUSED_ELEMENT_DIAGNOSTICS)
@@ -227,14 +242,5 @@ public open class JetPsiChecker : Annotator, HighlightRangeExtension {
DeprecatedAnnotationVisitor(holder, bindingContext)
)
platformStatic fun annotateElement(element: PsiElement, holder: AnnotationHolder, diagnostics: Diagnostics) {
if (ProjectRootsUtil.isInProjectSource(element) || element.getContainingFile() is JetCodeFragment) {
val elementAnnotator = ElementAnnotator(element, holder)
for (diagnostic in diagnostics.forElement(element)) {
elementAnnotator.registerDiagnosticAnnotations(diagnostic)
}
}
}
}
}
@@ -20,7 +20,10 @@ import com.intellij.lang.annotation.AnnotationHolder;
import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.psi.PsiElement;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.idea.inspections.UnusedSymbolInspection;
import org.jetbrains.kotlin.psi.JetFile;
import org.jetbrains.kotlin.psi.JetNamedFunction;
import org.jetbrains.kotlin.psi.JetParameter;
public class JetPsiCheckerAndHighlightingUpdater extends JetPsiChecker {
@Override
@@ -37,4 +40,14 @@ public class JetPsiCheckerAndHighlightingUpdater extends JetPsiChecker {
throw e;
}
}
@Override
protected boolean shouldSuppressUnusedParameter(@NotNull JetParameter parameter) {
PsiElement grandParent = parameter.getParent().getParent();
if (grandParent instanceof JetNamedFunction) {
JetNamedFunction function = (JetNamedFunction) grandParent;
return UnusedSymbolInspection.Default.isEntryPoint(function);
}
return false;
}
}
@@ -72,7 +72,23 @@ import java.awt.GridBagLayout
import javax.swing.JPanel
public class UnusedSymbolInspection : AbstractKotlinInspection() {
private val javaInspection = UnusedDeclarationInspection()
class object {
private val javaInspection = UnusedDeclarationInspection()
public fun isEntryPoint(declaration: JetNamedDeclaration): Boolean {
// TODO Workaround for EA-64030 - IOE: PsiJavaParserFacadeImpl.createAnnotationFromText
// This should be fixed on IDEA side: ClsAnnotation should not throw exceptions when annotation class has Java keyword
if (declaration.getAnnotationEntries().any { it.getTypeReference().getText().endsWith("native") }) return false
if (ProjectStructureUtil.isJsKotlinModule(declaration.getContainingJetFile())) return false
val lightElement: PsiElement? = when (declaration) {
is JetClassOrObject -> declaration.toLightClass()
is JetNamedFunction -> LightClassUtil.getLightClassMethod(declaration)
else -> return false
}
return lightElement != null && javaInspection.isEntryPoint(lightElement)
}
}
override fun runForWholeFile() = true
@@ -130,20 +146,6 @@ public class UnusedSymbolInspection : AbstractKotlinInspection() {
}
}
private fun isEntryPoint(declaration: JetNamedDeclaration): Boolean {
// TODO Workaround for EA-64030 - IOE: PsiJavaParserFacadeImpl.createAnnotationFromText
// This should be fixed on IDEA side: ClsAnnotation should not throw exceptions when annotation class has Java keyword
if (declaration.getAnnotationEntries().any { it.getTypeReference().getText().endsWith("native") }) return false
if (ProjectStructureUtil.isJsKotlinModule(declaration.getContainingJetFile())) return false
val lightElement: PsiElement? = when (declaration) {
is JetClassOrObject -> declaration.toLightClass()
is JetNamedFunction -> LightClassUtil.getLightClassMethod(declaration)
else -> return false
}
return lightElement != null && javaInspection.isEntryPoint(lightElement)
}
private fun classOrObjectHasTextUsages(classOrObject: JetClassOrObject): Boolean {
var hasTextUsages = false
@@ -0,0 +1,11 @@
annotation class MyTestAnnotation
fun unused(<warning descr="[UNUSED_PARAMETER] Parameter 'p' is never used">p</warning>: Int) {
}
[MyTestAnnotation]
fun unusedButAnnotated(p: Int) {
}
@@ -0,0 +1,32 @@
/*
* 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.checkers
import com.intellij.codeInspection.ex.EntryPointsManagerBase
public class JetPsiCheckerCustomTest : AbstractJetPsiCheckerTest() {
public fun testNoUnusedParameterWhenCustom() {
val testAnnotation = "MyTestAnnotation"
EntryPointsManagerBase.getInstance(getProject()).ADDITIONAL_ANNOTATIONS.add(testAnnotation)
try {
doTest("idea/testData/checker/custom/NoUnusedParameterWhenCustom.kt")
}
finally {
EntryPointsManagerBase.getInstance(getProject()).ADDITIONAL_ANNOTATIONS.remove(testAnnotation)
}
}
}