From 056daeaadb30edc24516c71f59b4a72a3809aa55 Mon Sep 17 00:00:00 2001 From: Evgeny Gerashchenko Date: Wed, 18 Feb 2015 15:31:55 +0300 Subject: [PATCH] Don't mark parameters of get/set/propertyDelegated functions as unused. #KT-5974 fixed --- .../kotlin/cfg/JetFlowInformationProvider.java | 11 ++++++++--- .../diagnostics/tests/UnusedParameters.kt | 15 ++++++++++++++- .../diagnostics/tests/UnusedParameters.txt | 5 +++++ compiler/testData/diagnostics/tests/Variance.kt | 4 ++-- .../local/LocalClassDelegatedProperties.kt | 2 +- .../inference/noErrorsForImplicitConstraints.kt | 8 ++++---- .../thisOfNothingNullableType.kt | 4 ++-- .../tests/delegatedProperty/thisOfNothingType.kt | 4 ++-- .../diagnostics/tests/regressions/Jet169.kt | 2 +- .../diagnostics/tests/regressions/kt235.kt | 8 ++++---- .../diagnostics/tests/regressions/kt258.kt | 2 +- .../diagnostics/tests/regressions/kt459.kt | 2 +- .../diagnostics/tests/variance/ValProperty.kt | 4 ++-- .../diagnostics/tests/variance/VarProperty.kt | 4 ++-- idea/testData/checker/Variance.kt | 4 ++-- idea/testData/checker/regression/Jet169.kt | 2 +- 16 files changed, 52 insertions(+), 29 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/cfg/JetFlowInformationProvider.java b/compiler/frontend/src/org/jetbrains/kotlin/cfg/JetFlowInformationProvider.java index 3dd1f069583..53543cda1b1 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/cfg/JetFlowInformationProvider.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/cfg/JetFlowInformationProvider.java @@ -649,10 +649,15 @@ public class JetFlowInformationProvider { 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); + String functionName = functionDescriptor.getName().asString(); + if (isMain + || functionDescriptor.getModality().isOverridable() + || !functionDescriptor.getOverriddenDescriptors().isEmpty() + || "get".equals(functionName) || "set".equals(functionName) || "propertyDelegated".equals(functionName) + ) { + return; } + 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); diff --git a/compiler/testData/diagnostics/tests/UnusedParameters.kt b/compiler/testData/diagnostics/tests/UnusedParameters.kt index 8bf64c0516f..1aaf76ae0c7 100644 --- a/compiler/testData/diagnostics/tests/UnusedParameters.kt +++ b/compiler/testData/diagnostics/tests/UnusedParameters.kt @@ -12,4 +12,17 @@ class C(a: Int, b: Int, c: Int, d: Int, e: Int = d, val f fun f(a: Int, b: Int, c: Int = b) { a + a -} \ No newline at end of file +} + +fun Any.get(thisRef: Any?, prop: PropertyMetadata): String = ":)" +fun Any.set(thisRef: Any?, prop: PropertyMetadata, value: String) { +} + +fun Any.propertyDelegated(prop: PropertyMetadata) { +} + +fun get(p: Any) { +} + +fun set(p: Any) { +} diff --git a/compiler/testData/diagnostics/tests/UnusedParameters.txt b/compiler/testData/diagnostics/tests/UnusedParameters.txt index 95d079117ea..d52fc07037f 100644 --- a/compiler/testData/diagnostics/tests/UnusedParameters.txt +++ b/compiler/testData/diagnostics/tests/UnusedParameters.txt @@ -1,6 +1,11 @@ package internal fun f(/*0*/ a: kotlin.Int, /*1*/ b: kotlin.Int, /*2*/ c: kotlin.Int = ...): kotlin.Unit +internal fun get(/*0*/ p: kotlin.Any): kotlin.Unit +internal fun set(/*0*/ p: kotlin.Any): kotlin.Unit +internal fun kotlin.Any.get(/*0*/ thisRef: kotlin.Any?, /*1*/ prop: kotlin.PropertyMetadata): kotlin.String +internal fun kotlin.Any.propertyDelegated(/*0*/ prop: kotlin.PropertyMetadata): kotlin.Unit +internal fun kotlin.Any.set(/*0*/ thisRef: kotlin.Any?, /*1*/ prop: kotlin.PropertyMetadata, /*2*/ value: kotlin.String): 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) diff --git a/compiler/testData/diagnostics/tests/Variance.kt b/compiler/testData/diagnostics/tests/Variance.kt index 65aa1dd137b..69737a756f1 100644 --- a/compiler/testData/diagnostics/tests/Variance.kt +++ b/compiler/testData/diagnostics/tests/Variance.kt @@ -19,8 +19,8 @@ fun foo(c: Consumer, p: Producer, u: Usual) { //Arrays copy example class Array(val length : Int, val t : T) { - fun get(index : Int) : T { return t } - fun set(index : Int, value : T) { /* ... */ } + fun get(index : Int) : T { return t } + fun set(index : Int, value : T) { /* ... */ } } fun copy1(from : Array, to : Array) {} diff --git a/compiler/testData/diagnostics/tests/dataFlow/local/LocalClassDelegatedProperties.kt b/compiler/testData/diagnostics/tests/dataFlow/local/LocalClassDelegatedProperties.kt index d8944f9ee76..beb9fa4cd98 100644 --- a/compiler/testData/diagnostics/tests/dataFlow/local/LocalClassDelegatedProperties.kt +++ b/compiler/testData/diagnostics/tests/dataFlow/local/LocalClassDelegatedProperties.kt @@ -1,5 +1,5 @@ class Del { - fun get(_this: Any?, p: PropertyMetadata): Int = 0 + fun get(_this: Any?, p: PropertyMetadata): Int = 0 } fun df(del: Del): Del = del diff --git a/compiler/testData/diagnostics/tests/delegatedProperty/inference/noErrorsForImplicitConstraints.kt b/compiler/testData/diagnostics/tests/delegatedProperty/inference/noErrorsForImplicitConstraints.kt index 8a4bdbe9836..cf475f7f555 100644 --- a/compiler/testData/diagnostics/tests/delegatedProperty/inference/noErrorsForImplicitConstraints.kt +++ b/compiler/testData/diagnostics/tests/delegatedProperty/inference/noErrorsForImplicitConstraints.kt @@ -9,11 +9,11 @@ fun getMyProperty1() = MyProperty1() class MyProperty1 { - public fun get(thisRef: R, desc: PropertyMetadata): T { + public fun get(thisRef: R, desc: PropertyMetadata): T { throw Exception() } - public fun set(i: Int, j: Int, k: Int) { + public fun set(i: Int, j: Int, k: Int) { println("set") } } @@ -29,11 +29,11 @@ fun getMyProperty2() = MyProperty2() class MyProperty2 { - public fun get(thisRef: R, desc: PropertyMetadata): T { + public fun get(thisRef: R, desc: PropertyMetadata): T { throw Exception() } - public fun set(i: Int) { + public fun set(i: Int) { println("set") } } diff --git a/compiler/testData/diagnostics/tests/delegatedProperty/thisOfNothingNullableType.kt b/compiler/testData/diagnostics/tests/delegatedProperty/thisOfNothingNullableType.kt index d23ddca1a74..b97e30db73e 100644 --- a/compiler/testData/diagnostics/tests/delegatedProperty/thisOfNothingNullableType.kt +++ b/compiler/testData/diagnostics/tests/delegatedProperty/thisOfNothingNullableType.kt @@ -5,11 +5,11 @@ class A { var aTopLevel: Int by Delegate() class Delegate { - fun get(t: Nothing?, p: PropertyMetadata): Int { + fun get(t: Nothing?, p: PropertyMetadata): Int { p.equals(null) // to avoid UNUSED_PARAMETER warning return 1 } - fun set(t: Nothing?, p: PropertyMetadata, a: Int) { + fun set(t: Nothing?, p: PropertyMetadata, a: Int) { p.equals(a) // to avoid UNUSED_PARAMETER warning } } \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/delegatedProperty/thisOfNothingType.kt b/compiler/testData/diagnostics/tests/delegatedProperty/thisOfNothingType.kt index 6ef3066c6fd..09d63bedc09 100644 --- a/compiler/testData/diagnostics/tests/delegatedProperty/thisOfNothingType.kt +++ b/compiler/testData/diagnostics/tests/delegatedProperty/thisOfNothingType.kt @@ -5,11 +5,11 @@ class A { var aTopLevel: Int by Delegate() class Delegate { - fun get(t: Nothing, p: PropertyMetadata): Int { + fun get(t: Nothing, p: PropertyMetadata): Int { p.equals(null) // to avoid UNUSED_PARAMETER warning return 1 } - fun set(t: Nothing, p: PropertyMetadata, a: Int) { + fun set(t: Nothing, p: PropertyMetadata, a: Int) { p.equals(a) // to avoid UNUSED_PARAMETER warning } } \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/regressions/Jet169.kt b/compiler/testData/diagnostics/tests/regressions/Jet169.kt index 6a3ecf0c7a2..eaf1494d564 100644 --- a/compiler/testData/diagnostics/tests/regressions/Jet169.kt +++ b/compiler/testData/diagnostics/tests/regressions/Jet169.kt @@ -1,4 +1,4 @@ -fun set(key : String, value : String) { +fun set(key : String, value : String) { val a : String? = "" when (a) { "" -> a.get(0) diff --git a/compiler/testData/diagnostics/tests/regressions/kt235.kt b/compiler/testData/diagnostics/tests/regressions/kt235.kt index 5adfcf6adbc..05915d5c84b 100644 --- a/compiler/testData/diagnostics/tests/regressions/kt235.kt +++ b/compiler/testData/diagnostics/tests/regressions/kt235.kt @@ -35,13 +35,13 @@ fun main(args: Array) { } class MyArray() { - fun get(i: Int): Int = 1 - fun set(i: Int, value: Int): Int = 1 + fun get(i: Int): Int = 1 + fun set(i: Int, value: Int): Int = 1 } class MyArray1() { - fun get(i: Int): Int = 1 - fun set(i: Int, value: Int) {} + fun get(i: Int): Int = 1 + fun set(i: Int, value: Int) {} } class MyNumber() { diff --git a/compiler/testData/diagnostics/tests/regressions/kt258.kt b/compiler/testData/diagnostics/tests/regressions/kt258.kt index 3611bcfbec6..7a5252fd74e 100644 --- a/compiler/testData/diagnostics/tests/regressions/kt258.kt +++ b/compiler/testData/diagnostics/tests/regressions/kt258.kt @@ -7,4 +7,4 @@ fun test() { attributes["href"] = "1" // inference fails, but it shouldn't } -fun MutableMap.set(key : K, value : V) {}//= this.put(key, value) +fun MutableMap.set(key : K, value : V) {}//= this.put(key, value) \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/regressions/kt459.kt b/compiler/testData/diagnostics/tests/regressions/kt459.kt index 0cfd2467988..b300e3b232b 100644 --- a/compiler/testData/diagnostics/tests/regressions/kt459.kt +++ b/compiler/testData/diagnostics/tests/regressions/kt459.kt @@ -5,4 +5,4 @@ fun test() { attributes["href"] = "1" // inference fails, but it shouldn't } -fun Map.set(key : K, value : V) {}//= this.put(key, value) +fun Map.set(key : K, value : V) {}//= this.put(key, value) \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/variance/ValProperty.kt b/compiler/testData/diagnostics/tests/variance/ValProperty.kt index 3fd60976a2b..2699705866a 100644 --- a/compiler/testData/diagnostics/tests/variance/ValProperty.kt +++ b/compiler/testData/diagnostics/tests/variance/ValProperty.kt @@ -3,8 +3,8 @@ trait Out trait Inv class Delegate { - fun get(t: Any, p: PropertyMetadata): T = null!! - fun set(t: Any, p: PropertyMetadata, value: T) {} + fun get(t: Any, p: PropertyMetadata): T = null!! + fun set(t: Any, p: PropertyMetadata, value: T) {} } fun getT(): T = null!! diff --git a/compiler/testData/diagnostics/tests/variance/VarProperty.kt b/compiler/testData/diagnostics/tests/variance/VarProperty.kt index b2cd1b7f5bc..5e8812c2c0b 100644 --- a/compiler/testData/diagnostics/tests/variance/VarProperty.kt +++ b/compiler/testData/diagnostics/tests/variance/VarProperty.kt @@ -3,8 +3,8 @@ trait Out trait Inv class Delegate { - fun get(t: Any, p: PropertyMetadata): T = null!! - fun set(t: Any, p: PropertyMetadata, varue: T) {} + fun get(t: Any, p: PropertyMetadata): T = null!! + fun set(t: Any, p: PropertyMetadata, varue: T) {} } fun getT(): T = null!! diff --git a/idea/testData/checker/Variance.kt b/idea/testData/checker/Variance.kt index 42f2903a26c..dab5c218c43 100644 --- a/idea/testData/checker/Variance.kt +++ b/idea/testData/checker/Variance.kt @@ -19,8 +19,8 @@ fun foo(c: Consumer, p: Producer, u: Usual) { //Arrays copy example class Array(val length : Int, val t : T) { - fun get(index : Int) : T { return t } - fun set(index : Int, value : T) { /* ... */ } + fun get(index : Int) : T { return t } + fun set(index : Int, value : T) { /* ... */ } } fun copy1(from : Array, to : Array) {} diff --git a/idea/testData/checker/regression/Jet169.kt b/idea/testData/checker/regression/Jet169.kt index e38e1eb9a05..7787764d323 100644 --- a/idea/testData/checker/regression/Jet169.kt +++ b/idea/testData/checker/regression/Jet169.kt @@ -1,4 +1,4 @@ -fun set(key : String, value : String) { +fun set(key : String, value : String) { val a : String? = "" when (a) { "" -> a.get(0)