KT-6671 Report unused constructor parameters

#KT-6671 fixed
This commit is contained in:
Evgeny Gerashchenko
2015-01-27 19:24:19 +03:00
parent f96f1b8cb0
commit 897854b3dc
49 changed files with 106 additions and 69 deletions
@@ -641,18 +641,22 @@ public class JetFlowInformationProvider {
report(Errors.UNUSED_VARIABLE.on((JetNamedDeclaration) element, variableDescriptor), ctxt);
}
else if (element instanceof JetParameter) {
PsiElement psiElement = element.getParent().getParent();
if (psiElement instanceof JetFunction) {
PsiElement owner = element.getParent().getParent();
if (owner instanceof JetFunction) {
MainFunctionDetector mainFunctionDetector = new MainFunctionDetector(trace.getBindingContext());
boolean isMain = (psiElement instanceof JetNamedFunction) && mainFunctionDetector.isMain((JetNamedFunction) psiElement);
if (psiElement instanceof JetFunctionLiteral) return;
DeclarationDescriptor descriptor = trace.get(BindingContext.DECLARATION_TO_DESCRIPTOR, psiElement);
assert descriptor instanceof FunctionDescriptor : psiElement.getText();
boolean isMain = (owner instanceof JetNamedFunction) && mainFunctionDetector.isMain((JetNamedFunction) owner);
if (owner instanceof JetFunctionLiteral) return;
DeclarationDescriptor descriptor = trace.get(BindingContext.DECLARATION_TO_DESCRIPTOR, owner);
assert descriptor instanceof FunctionDescriptor : owner.getText();
FunctionDescriptor functionDescriptor = (FunctionDescriptor) descriptor;
if (!isMain && !functionDescriptor.getModality().isOverridable()
&& functionDescriptor.getOverriddenDescriptors().isEmpty()) {
report(Errors.UNUSED_PARAMETER.on((JetParameter) element, variableDescriptor), ctxt);
}
} else if (owner instanceof JetClass) {
if (!((JetParameter) element).hasValOrVarNode() && !((JetClass) owner).isAnnotation()) {
report(Errors.UNUSED_PARAMETER.on((JetParameter) element, variableDescriptor), ctxt);
}
}
}
}
@@ -52,7 +52,7 @@ abstract class B1(
class B2() : B1(1, "r") {}
abstract class B3(i: Int) {
abstract class B3(<!UNUSED_PARAMETER!>i<!>: Int) {
}
fun foo(<!UNUSED_PARAMETER!>c<!>: B3) {
@@ -10,7 +10,7 @@ class WithC2() : <!SUPERTYPE_NOT_INITIALIZED!>WithC1<!>
class WithPC0() {
}
class WithPC1(a : Int) {
class WithPC1(<!UNUSED_PARAMETER!>a<!> : Int) {
}
@@ -18,7 +18,7 @@ class Foo() : <!SUPERTYPE_NOT_INITIALIZED, FINAL_SUPERTYPE!>WithPC0<!>, <!SYNTAX
}
class WithCPI_Dup(x : Int) {
class WithCPI_Dup(<!UNUSED_PARAMETER!>x<!> : Int) {
<!MUST_BE_INITIALIZED_OR_BE_ABSTRACT!>var x : Int<!>
}
@@ -6,7 +6,7 @@ fun bar(x : Int = <!TYPE_MISMATCH!>""<!>, y : Int = x, <!UNUSED_PARAMETER!>z<!>
// KT-371 Resolve default parameters for constructors
class A(x : Int = <!UNINITIALIZED_PARAMETER!>y<!>, y : Int = x) { // None of the references is resolved, no types checked
class A(x : Int = <!UNINITIALIZED_PARAMETER!>y<!>, <!UNUSED_PARAMETER!>y<!> : Int = x) { // None of the references is resolved, no types checked
fun foo(<!UNUSED_PARAMETER!>bool<!>: Boolean, a: Int = <!TYPE_MISMATCH, UNINITIALIZED_PARAMETER!>b<!>, <!UNUSED_PARAMETER!>b<!>: String = <!TYPE_MISMATCH!>a<!>) {}
}
@@ -0,0 +1,15 @@
class C(a: Int, b: Int, c: Int, d: Int, <!UNUSED_PARAMETER!>e<!>: Int = d, val f: String) {
{
a + a
}
val g = b
{
c + c
}
}
fun f(a: Int, b: Int, <!UNUSED_PARAMETER!>c<!>: Int = b) {
a + a
}
@@ -0,0 +1,12 @@
package
internal fun f(/*0*/ a: kotlin.Int, /*1*/ b: kotlin.Int, /*2*/ c: kotlin.Int = ...): kotlin.Unit
internal final class C {
public constructor C(/*0*/ a: kotlin.Int, /*1*/ b: kotlin.Int, /*2*/ c: kotlin.Int, /*3*/ d: kotlin.Int, /*4*/ e: kotlin.Int = ..., /*5*/ f: kotlin.String)
internal final val f: kotlin.String
internal final val g: kotlin.Int
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -5,5 +5,5 @@ var bar : Int = 1
val x : (Int) -> Int = {([<!UNRESOLVED_REFERENCE!>varargs<!>] x : Int) -> x}
class Hello(<!UNRESOLVED_REFERENCE!>varargs<!> args: Any) {
class Hello(<!UNRESOLVED_REFERENCE!>varargs<!> <!UNUSED_PARAMETER!>args<!>: Any) {
}
@@ -7,5 +7,5 @@ var bar : Int = 1
val x : (Int) -> Int = {([test] x : Int) -> x}
class Hello(test args: Any) {
class Hello(test <!UNUSED_PARAMETER!>args<!>: Any) {
}
@@ -1,6 +1,6 @@
package test
open class ToResolve<SomeClass>(f : (Int) -> Int)
open class ToResolve<SomeClass>(<!UNUSED_PARAMETER!>f<!> : (Int) -> Int)
fun testFun(<!UNUSED_PARAMETER!>a<!> : Int) = 12
class TestSome<P> {
@@ -192,7 +192,7 @@ fun reassignFunParams(a: Int) {
<!VAL_REASSIGNMENT!>a<!> = <!UNUSED_VALUE!>1<!>
}
open class Open(a: Int, w: Int) {}
open class Open(<!UNUSED_PARAMETER!>a<!>: Int, <!UNUSED_PARAMETER!>w<!>: Int) {}
class LocalValsVsProperties(val a: Int, w: Int) : Open(a, w) {
val x : Int
@@ -1,7 +1,7 @@
//KT-657 Semantic checks for when without condition
package kt657
class Pair<A, B>(a: A, b: B)
class Pair<A, B>(<!UNUSED_PARAMETER!>a<!>: A, <!UNUSED_PARAMETER!>b<!>: B)
fun foo() =
when {
@@ -3,7 +3,7 @@
package kt234_kt973
class Pair<A, B>(a: A, b: B)
class Pair<A, B>(<!UNUSED_PARAMETER!>a<!>: A, <!UNUSED_PARAMETER!>b<!>: B)
fun test(t : Pair<Int, Int>) : Int {
when (t) {
@@ -1,4 +1,4 @@
data class A(x: Int, y: String)
data class A(<!UNUSED_PARAMETER!>x<!>: Int, <!UNUSED_PARAMETER!>y<!>: String)
fun foo(a: A) {
a.<!UNRESOLVED_REFERENCE!>component1<!>()
@@ -1,4 +1,4 @@
data class A(val x: Int, y: String)
data class A(val x: Int, <!UNUSED_PARAMETER!>y<!>: String)
fun foo(a: A) {
a.component1() : Int
@@ -1,4 +1,4 @@
data class A(var x: Int, y: String)
data class A(var x: Int, <!UNUSED_PARAMETER!>y<!>: String)
fun foo(a: A) {
a.component1() : Int
@@ -1,4 +1,4 @@
data class A(x: Int, val y: String)
data class A(<!UNUSED_PARAMETER!>x<!>: Int, val y: String)
fun foo(a: A) {
a.component1() : String
@@ -1,4 +1,4 @@
data class A(x: Int, var y: String)
data class A(<!UNUSED_PARAMETER!>x<!>: Int, var y: String)
fun foo(a: A) {
a.component1() : String
@@ -1,4 +1,4 @@
open class Base(x: String, y: Int)
open class Base(<!UNUSED_PARAMETER!>x<!>: String, <!UNUSED_PARAMETER!>y<!>: Int)
fun test(x: Any, y: Int?) {
if (x !is String) return
@@ -1,7 +1,7 @@
fun test(x: Any) {
if (x !is String) return
class Local(s: String = <!DEBUG_INFO_SMARTCAST!>x<!>) {
class Local(<!UNUSED_PARAMETER!>s<!>: String = <!DEBUG_INFO_SMARTCAST!>x<!>) {
fun foo(s: String = <!DEBUG_INFO_SMARTCAST!>x<!>): String = s
}
}
@@ -13,4 +13,4 @@ fun f(a: Any?) {
trait B {
fun foo() {}
}
open class X(b: B)
open class X(<!UNUSED_PARAMETER!>b<!>: B)
@@ -6,4 +6,4 @@ fun foo(x: Any?) {
}
}
open class Base(s: String)
open class Base(<!UNUSED_PARAMETER!>s<!>: String)
@@ -1,6 +1,6 @@
val a: Int by A(1)
class A<T: Any>(i: T) {
class A<T: Any>(<!UNUSED_PARAMETER!>i<!>: T) {
fun get(t: Any?, p: PropertyMetadata): T {
t.equals(p) // to avoid UNUSED_PARAMETER warning
throw Exception()
@@ -5,7 +5,7 @@ val b by Delegate(<!TYPECHECKER_HAS_RUN_INTO_RECURSIVE_PROBLEM!>b<!>)
val c by <!DEBUG_INFO_ELEMENT_WITH_ERROR_TYPE!>d<!>
val d by <!TYPECHECKER_HAS_RUN_INTO_RECURSIVE_PROBLEM!>c<!>
class Delegate(i: Int) {
class Delegate(<!UNUSED_PARAMETER!>i<!>: Int) {
fun get(t: Any?, p: PropertyMetadata): Int {
t.equals(p) // to avoid UNUSED_PARAMETER warning
return 1
@@ -4,7 +4,7 @@ class A {
val Int.a by Delegate(<!TYPE_MISMATCH!>this<!>)
}
class Delegate(i: Int) {
class Delegate(<!UNUSED_PARAMETER!>i<!>: Int) {
fun get(t: Any?, p: PropertyMetadata): Int {
t.equals(p) // to avoid UNUSED_PARAMETER warning
return 1
@@ -5,7 +5,7 @@ trait T {
val v : Int
}
open <!ABSTRACT_MEMBER_NOT_IMPLEMENTED!>class Br<!>(t : T) : T {
open <!ABSTRACT_MEMBER_NOT_IMPLEMENTED!>class Br<!>(<!UNUSED_PARAMETER!>t<!> : T) : T {
}
@@ -4,7 +4,7 @@ package bar
import java.io.File
class Customer(name: String)
class Customer(<!UNUSED_PARAMETER!>name<!>: String)
fun foo(f: File, c: Customer) {
f.name
@@ -3,7 +3,7 @@
class A<X, Y : X>
class B<X, Y : X>(foo: A<X, Y>) {
class B<X, Y : X>(<!UNUSED_PARAMETER!>foo<!>: A<X, Y>) {
fun test1(a: A<X, Y>) {
B(a)
val b: B<X, Y> = B(a)
@@ -13,7 +13,7 @@ class B<X, Y : X>(foo: A<X, Y>) {
class C<X, Z, Y : X>
class D<X, Z, Y : X>(foo: C<X, Z, Y>) {
class D<X, Z, Y : X>(<!UNUSED_PARAMETER!>foo<!>: C<X, Z, Y>) {
fun test(a: C<Y, Y, Y>) {
val d: D<X, Y, Y> = <!TYPE_MISMATCH!>D(a)<!>
}
@@ -1,6 +1,6 @@
package a
class MyList<T>(t: T) {}
class MyList<T>(<!UNUSED_PARAMETER!>t<!>: T) {}
fun <T> getMyList(t: T) : MyList< T> = MyList(t)
fun <T> getMyListToWriteTo(t: T) : MyList< in T> = MyList(t)
@@ -3,7 +3,7 @@ package Hello
open class Base<T>
class StringBase : Base<String>()
class Client<T, X: Base<T>>(x: X)
class Client<T, X: Base<T>>(<!UNUSED_PARAMETER!>x<!>: X)
fun test() {
val c = Client(StringBase()) // Type inference fails here for T.
@@ -44,9 +44,9 @@ abstract class Test() {
}
open class Super(i : Int)
open class Super(<!UNUSED_PARAMETER!>i<!> : Int)
class TestPCParameters(w : Int, x : Int, val y : Int, var z : Int) : Super(w) {
class TestPCParameters(w : Int, <!UNUSED_PARAMETER!>x<!> : Int, val y : Int, var z : Int) : Super(w) {
val xx = w
@@ -1,4 +1,4 @@
class Z(s: (Int) -> Int) {
class Z(<!UNUSED_PARAMETER!>s<!>: (Int) -> Int) {
}
@@ -27,20 +27,20 @@ class FinalClass() {
annotation class annotated(val text: String = "not given")
//Check legal modifiers in constructor
class LegalModifier(val a: Int, annotated private var b: String, annotated vararg v: Int)
class LegalModifier(val a: Int, annotated private var b: String, annotated vararg <!UNUSED_PARAMETER!>v<!>: Int)
//Check illegal modifier in constructor parameters
class IllegalModifiers1(<!ILLEGAL_MODIFIER!>private<!> a: Int)
class IllegalModifiers1(<!ILLEGAL_MODIFIER!>private<!> <!UNUSED_PARAMETER!>a<!>: Int)
//Check multiple illegal modifiers in constructor
class IllegalModifiers2(<!ILLEGAL_MODIFIER!>private<!> <!ILLEGAL_MODIFIER!>abstract<!> a: Int)
class IllegalModifiers2(<!ILLEGAL_MODIFIER!>private<!> <!ILLEGAL_MODIFIER!>abstract<!> <!UNUSED_PARAMETER!>a<!>: Int)
//Check annotations with illegal modifiers in constructor
class IllegalModifiers3(annotated <!ILLEGAL_MODIFIER!>public<!> <!ILLEGAL_MODIFIER!>abstract<!> b: String)
class IllegalModifiers3(annotated <!ILLEGAL_MODIFIER!>public<!> <!ILLEGAL_MODIFIER!>abstract<!> <!UNUSED_PARAMETER!>b<!>: String)
//Check annotations and vararg with illegal modifiers in constructor
class IllegalModifiers4(val a: Int, annotated("a text") <!ILLEGAL_MODIFIER!>protected<!> vararg v: Int)
class IllegalModifiers4(val a: Int, annotated("a text") <!ILLEGAL_MODIFIER!>protected<!> vararg <!UNUSED_PARAMETER!>v<!>: Int)
//Check illegal modifiers for functions and catch block
abstract class IllegalModifiers5() {
@@ -1,6 +1,6 @@
package toplevelObjectDeclarations
open class Foo(y : Int) {
open class Foo(<!UNUSED_PARAMETER!>y<!> : Int) {
open fun foo() : Int = 1
}
@@ -6,8 +6,8 @@ trait ResolverForModule
trait ResolverForProject<M1, R1>
class ResolverForProjectImpl<M : ModuleInfo, R : ResolverForModule>(
descriptorByModule: Map<M, ModuleDescriptorImpl>,
delegateResolver: ResolverForProject<M, R>
<!UNUSED_PARAMETER!>descriptorByModule<!>: Map<M, ModuleDescriptorImpl>,
<!UNUSED_PARAMETER!>delegateResolver<!>: ResolverForProject<M, R>
) : ResolverForProject<M, R>
fun <M2: ModuleInfo, A: ResolverForModule> foo(delegateResolver: ResolverForProject<M2, A>): ResolverForProject<M2, A> {
@@ -5,8 +5,8 @@ trait ResolverForProject<M1> {
}
class ResolverForProjectImpl<M>(
descriptorByModule: Map<M, String>,
delegateResolver: ResolverForProject<M>
<!UNUSED_PARAMETER!>descriptorByModule<!>: Map<M, String>,
<!UNUSED_PARAMETER!>delegateResolver<!>: ResolverForProject<M>
) : ResolverForProject<M>
trait WithFoo {
@@ -1,6 +1,6 @@
package p
class X<V>(provider: () -> V, trackValue: Boolean) {
class X<V>(<!UNUSED_PARAMETER!>provider<!>: () -> V, <!UNUSED_PARAMETER!>trackValue<!>: Boolean) {
}
class B {
@@ -5,15 +5,15 @@ import java.util.HashMap
//KT-250 Incorrect variable resolve in constructor arguments of superclass
open class A(val x: Int)
class B(y: Int) : A(<!UNRESOLVED_REFERENCE!>x<!>) //x is resolved as a property in a, so no error is generated
class B(<!UNUSED_PARAMETER!>y<!>: Int) : A(<!UNRESOLVED_REFERENCE!>x<!>) //x is resolved as a property in a, so no error is generated
//KT-617 Prohibit dollars in call to superclass constructors
open class M(p: Int)
open class M(<!UNUSED_PARAMETER!>p<!>: Int)
class N(val p: Int) : A(<!UNRESOLVED_REFERENCE!>$p<!>)
//KT-10 Don't allow to use properties in supertype initializers
open class Element()
class TextElement(name: String) : Element()
class TextElement(<!UNUSED_PARAMETER!>name<!>: String) : Element()
abstract class Tag(val name : String) {
val children = ArrayList<Element>()
@@ -36,7 +36,7 @@ class Body1() : BodyTag(<!NO_THIS!>this<!>.<!DEBUG_INFO_ELEMENT_WITH_ERROR_TYPE!
//more tests
open class X(p: Int, r: Int) {
open class X(<!UNUSED_PARAMETER!>p<!>: Int, <!UNUSED_PARAMETER!>r<!>: Int) {
val s = "s"
}
@@ -1,6 +1,6 @@
//KT-2746 Do.smartcasts in inference
class C<T>(t :T)
class C<T>(<!UNUSED_PARAMETER!>t<!> :T)
fun test1(a: Any) {
if (a is String) {
@@ -1,6 +1,6 @@
package h
public class MyClass<S, T>(param: MyClass<S, T>) {
public class MyClass<S, T>(<!UNUSED_PARAMETER!>param<!>: MyClass<S, T>) {
fun test() {
val result: MyClass<Any, Any>? = null
MyClass<S, Any>(result <!CAST_NEVER_SUCCEEDS!>as<!> MyClass<S, Any>)
@@ -1,6 +1,6 @@
fun foo<T: Any>(vararg <!UNUSED_PARAMETER!>ts<!>: T): T? = null
class Pair<A>(a: A)
class Pair<A>(<!UNUSED_PARAMETER!>a<!>: A)
fun test() {
val v = foo(Pair(1))
@@ -17,9 +17,9 @@ class Test<in I, out O, P>(
var type9: In<<!TYPE_VARIANCE_CONFLICT(I; in; invariant; In<I>)!>I<!>>,
var type0: In<<!TYPE_VARIANCE_CONFLICT(O; out; invariant; In<O>)!>O<!>>,
type11: I,
type12: O,
type13: P,
type14: In<I>,
type15: In<O>
<!UNUSED_PARAMETER!>type11<!>: I,
<!UNUSED_PARAMETER!>type12<!>: O,
<!UNUSED_PARAMETER!>type13<!>: P,
<!UNUSED_PARAMETER!>type14<!>: In<I>,
<!UNUSED_PARAMETER!>type15<!>: In<O>
)
@@ -526,6 +526,12 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("UnusedParameters.kt")
public void testUnusedParameters() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/UnusedParameters.kt");
doTest(fileName);
}
@TestMetadata("UnusedVariables.kt")
public void testUnusedVariables() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/UnusedVariables.kt");
+1 -1
View File
@@ -164,7 +164,7 @@ abstract class B1(
class B2() : B1(1, "r") {}
abstract class B3(i: Int) {
abstract class B3(<warning>i</warning>: Int) {
}
fun foo(<warning>a</warning>: B3) {
+2 -2
View File
@@ -14,7 +14,7 @@ class NoPC {
class WithPC0() {
}
class WithPC1(a : Int) {
class WithPC1(<warning>a</warning> : Int) {
}
@@ -22,7 +22,7 @@ class Foo() : <error>WithPC0</error>(), <error>this</error>() {
}
class WithCPI_Dup(x : Int) {
class WithCPI_Dup(<warning>x</warning> : Int) {
<error>var x : Int</error>
}
+1 -1
View File
@@ -1,5 +1,5 @@
package toplevelObjectDeclarations
open class Foo(y : Int) {
open class Foo(<warning>y</warning> : Int) {
open fun foo() : Int = 1
}
+1 -1
View File
@@ -1,6 +1,6 @@
package unresolved
class Pair<A, B>(a: A, b: B)
class Pair<A, B>(<warning>a</warning>: A, <warning>b</warning>: B)
fun testGenericArgumentsCount() {
val <warning>p1</warning>: Pair<error><Int></error> = Pair(2, 2)
@@ -40,9 +40,9 @@
}
<info>open</info> class Super(i : Int)
<info>open</info> class Super(<warning>i</warning> : Int)
class TestPCParameters(w : Int, x : Int, val <info>y</info> : Int, var <info>z</info> : Int) : Super(w) {
class TestPCParameters(w : Int, <warning>x</warning> : Int, val <info>y</info> : Int, var <info>z</info> : Int) : Super(w) {
val <info>xx</info> = w
@@ -14,7 +14,7 @@ fun test() {
class Test(): <warning descr="'test.MyClass' is deprecated. Use A instead">MyClass</warning>() {}
class Test2(param: <warning descr="'test.MyClass' is deprecated. Use A instead">MyClass</warning>) {}
class Test2(<warning>param</warning>: <warning descr="'test.MyClass' is deprecated. Use A instead">MyClass</warning>) {}
// NO_CHECK_INFOS
// NO_CHECK_WEAK_WARNINGS
@@ -8,7 +8,7 @@ fun test() {
class Test(): <warning descr="'MyTrait' is deprecated. Use A instead">MyTrait</warning> { }
class Test2(param: <warning descr="'MyTrait' is deprecated. Use A instead">MyTrait</warning>) {}
class Test2(<warning>param</warning>: <warning descr="'MyTrait' is deprecated. Use A instead">MyTrait</warning>) {}
// NO_CHECK_INFOS
// NO_CHECK_WEAK_WARNINGS