Report UNUSED_DESTRUCTURED_PARAMETER_ENTRY

on destructured lambda parameters

 #KT-14347 Fixed
This commit is contained in:
Denis Zharkov
2016-10-18 18:25:12 +03:00
parent 9716d2ad54
commit 51f4244980
11 changed files with 109 additions and 16 deletions
@@ -602,11 +602,17 @@ class ControlFlowInformationProvider private constructor(
) {
element.nameIdentifier ?: return
if (!VariableUseState.isUsed(variableUseState)) {
if (!element.isSingleUnderscore && KtPsiUtil.isRemovableVariableDeclaration(element)) {
report(Errors.UNUSED_VARIABLE.on(element, variableDescriptor), ctxt)
}
else if (element is KtParameter) {
processUnusedParameter(ctxt, element, variableDescriptor)
if (element.isSingleUnderscore) return
when {
// KtDestructuringDeclarationEntry -> KtDestructuringDeclaration -> KtParameter -> KtParameterList
element is KtDestructuringDeclarationEntry && element.parent?.parent?.parent is KtParameterList ->
report(Errors.UNUSED_DESTRUCTURED_PARAMETER_ENTRY.on(element, variableDescriptor), ctxt)
KtPsiUtil.isRemovableVariableDeclaration(element) ->
report(Errors.UNUSED_VARIABLE.on(element, variableDescriptor), ctxt)
element is KtParameter ->
processUnusedParameter(ctxt, element, variableDescriptor)
}
}
else if (variableUseState === ONLY_WRITTEN_NEVER_READ && KtPsiUtil.isRemovableVariableDeclaration(element)) {
@@ -626,7 +632,6 @@ class ControlFlowInformationProvider private constructor(
private fun processUnusedParameter(ctxt: VariableUseContext, element: KtParameter, variableDescriptor: VariableDescriptor) {
val owner = element.parent?.parent
if (element.isSingleUnderscore) return
when (owner) {
is KtPrimaryConstructor -> if (!element.hasValOrVar()) {
val containingClass = owner.getContainingClassOrObject()
@@ -40,7 +40,10 @@ import org.jetbrains.kotlin.lexer.KtTokens.*
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getQualifiedElementSelector
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
import org.jetbrains.kotlin.resolve.*
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.BindingContextUtils
import org.jetbrains.kotlin.resolve.BindingTrace
import org.jetbrains.kotlin.resolve.CompileTimeConstantUtils
import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall
import org.jetbrains.kotlin.resolve.calls.model.ArgumentMatch
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
@@ -49,8 +52,8 @@ import org.jetbrains.kotlin.resolve.calls.tasks.ExplicitReceiverKind
import org.jetbrains.kotlin.resolve.calls.tower.getFakeDescriptorForObject
import org.jetbrains.kotlin.resolve.calls.util.FakeCallableDescriptorForObject
import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator
import org.jetbrains.kotlin.types.expressions.DoubleColonLHS
import org.jetbrains.kotlin.resolve.scopes.receivers.*
import org.jetbrains.kotlin.types.expressions.DoubleColonLHS
import org.jetbrains.kotlin.types.expressions.OperatorConventions
import java.util.*
@@ -970,6 +973,10 @@ class ControlFlowProcessor(private val trace: BindingTrace) {
builder.bindLabel(skipDefaultValue)
}
generateInitializer(parameter, computePseudoValueForParameter(parameter))
parameter.destructuringDeclaration?.let {
visitDestructuringDeclaration(it, generateWriteForEntries = true)
}
}
private fun computePseudoValueForParameter(parameter: KtParameter): PseudoValue {
@@ -667,6 +667,8 @@ public interface Errors {
DiagnosticFactory1<KtNamedDeclaration, VariableDescriptor> UNUSED_VARIABLE = DiagnosticFactory1.create(WARNING, DECLARATION_NAME);
DiagnosticFactory1<KtParameter, VariableDescriptor> UNUSED_PARAMETER = DiagnosticFactory1.create(WARNING, DECLARATION_NAME);
DiagnosticFactory1<KtDestructuringDeclarationEntry, VariableDescriptor> UNUSED_DESTRUCTURED_PARAMETER_ENTRY =
DiagnosticFactory1.create(WARNING, DECLARATION_NAME);
DiagnosticFactory2<KtTypeParameter, TypeParameterDescriptor, KotlinType> UNUSED_TYPEALIAS_PARAMETER =
DiagnosticFactory2.create(WARNING, DECLARATION_NAME);
@@ -275,6 +275,7 @@ public class DefaultErrorMessages {
MAP.put(UNINITIALIZED_ENUM_COMPANION, "Companion object of enum class ''{0}'' is uninitialized here", NAME);
MAP.put(UNUSED_VARIABLE, "Variable ''{0}'' is never used", NAME);
MAP.put(UNUSED_PARAMETER, "Parameter ''{0}'' is never used", NAME);
MAP.put(UNUSED_DESTRUCTURED_PARAMETER_ENTRY, "Destructured parameter ''{0}'' is never used", NAME);
MAP.put(ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE, "Variable ''{0}'' is assigned but never accessed", NAME);
MAP.put(VARIABLE_WITH_REDUNDANT_INITIALIZER, "Variable ''{0}'' initializer is redundant", NAME);
MAP.put(UNUSED_VALUE, "The value ''{0}'' assigned to ''{1}'' is never used", ELEMENT_TEXT, FQ_NAMES_IN_TYPES);