KT-7693 Unused constructor parameter is not detected if class is constructed with named argument

#KT-7693 fixed
This commit is contained in:
Evgeny Gerashchenko
2015-05-25 12:27:13 +03:00
parent 3c7225e83a
commit 0488229a61
10 changed files with 59 additions and 2 deletions
@@ -20,6 +20,7 @@ import com.intellij.lang.ASTNode;
import com.intellij.navigation.ItemPresentation;
import com.intellij.navigation.ItemPresentationProviders;
import com.intellij.psi.PsiElement;
import com.intellij.psi.search.SearchScope;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.lexer.JetTokens;
@@ -174,4 +175,17 @@ public class JetParameter extends JetNamedDeclarationStub<KotlinParameterStub> i
public List<JetTypeParameter> getTypeParameters() {
return Collections.emptyList();
}
@NotNull
@Override
public SearchScope getUseScope() {
PsiElement parent = getParent();
if (parent != null) {
PsiElement grandparent = parent.getParent();
if (grandparent instanceof JetNamedFunction) {
return grandparent.getUseScope();
}
}
return super.getUseScope();
}
}
@@ -133,6 +133,8 @@ public object UsageTypeUtils {
return DELEGATE
}
if (refExpr.getParent() is JetValueArgumentName) return NAMED_ARGUMENT
val dotQualifiedExpression = refExpr.getNonStrictParentOfType<JetDotQualifiedExpression>()
if (dotQualifiedExpression != null) {
@@ -258,5 +260,6 @@ enum class UsageTypeEnum {
TYPE_PARAMETER,
CLASS_CAST_TO,
ANNOTATION,
CLASS_NEW_OPERATOR
CLASS_NEW_OPERATOR,
NAMED_ARGUMENT
}
@@ -71,6 +71,7 @@ public object JetUsageTypeProvider : UsageTypeProviderEx {
CLASS_CAST_TO -> UsageType.CLASS_CAST_TO
ANNOTATION -> UsageType.ANNOTATION
CLASS_NEW_OPERATOR -> UsageType.CLASS_NEW_OPERATOR
NAMED_ARGUMENT -> JetUsageTypes.NAMED_ARGUMENT
}
}
}
@@ -107,4 +108,5 @@ object JetUsageTypes {
// common usage types
val CALLABLE_REFERENCE = UsageType(JetBundle.message("usageType.callable.reference"))
val NAMED_ARGUMENT = UsageType("Named argument")
}
@@ -199,7 +199,7 @@ public class UnusedSymbolInspection : AbstractKotlinInspection() {
return !query.forEach(Processor {
assert(it != null, { "Found reference is null, was looking for: " + declaration.getElementTextWithContext() +
" findAll(): " + query.findAll().map { it?.getElement()?.let{ it.getElementTextWithContext() } } })
declaration.isAncestor(it.getElement())
declaration.isAncestor(it.getElement()) || it.getElement().getParent() is JetValueArgumentName
})
}
@@ -6,4 +6,12 @@ open class A<T>(<caret>foo: T) {
}
val t: T = foo
fun usage() {
return A(foo = ":)")
}
}
fun usage() {
A(foo = ":)")
}
@@ -1,2 +1,4 @@
Named argument (11: 18) return A(foo = ":)")
Named argument (16: 7) A(foo = ":)")
Value read (5: 17) println(foo)
Value read (8: 16) val t: T = foo
@@ -4,3 +4,7 @@ fun foo<T>(<caret>t: T): T {
println(t)
return t
}
fun usage() {
foo(t = ":)")
}
@@ -1,2 +1,3 @@
Named argument (9: 9) foo(t = ":)")
Value read (4: 13) println(t)
Value read (5: 12) return t
@@ -7,4 +7,22 @@
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused Symbol</problem_class>
<description>Property 'param' is never used</description>
</problem>
<problem>
<file>paramPropertyUsedOnlyAsNamedArgument.kt</file>
<line>1</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/paramPropertyUsedOnlyAsNamedArgument.kt" />
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused Symbol</problem_class>
<description>Property 'param1' is never used</description>
</problem>
<problem>
<file>paramPropertyUsedOnlyAsNamedArgument.kt</file>
<line>1</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/paramPropertyUsedOnlyAsNamedArgument.kt" />
<problem_class severity="WARNING" attribute_key="NOT_USED_ELEMENT_ATTRIBUTES">Unused Symbol</problem_class>
<description>Property 'param2' is never used</description>
</problem>
</problems>
@@ -0,0 +1,5 @@
class C(val param1: Int, val param2: Int)
fun main(args: Array<String>) {
C(1, param2 = 1)
}