Don't report unused variable warning for delegated variables with provide delegate

^KT-38871 Fixed
This commit is contained in:
Mikhail Zarechenskiy
2023-03-27 11:20:33 +02:00
committed by Space Team
parent 18cfc9fcf1
commit 681e85eaed
9 changed files with 142 additions and 2 deletions
@@ -621,6 +621,12 @@ public class DiagnosticCompilerTestFE10TestdataTestGenerated extends AbstractDia
runTest("compiler/testData/diagnostics/tests/noSymbolProvidersDuplicationInDiamond.kt");
}
@Test
@TestMetadata("noUnusedOnDelegationWithProvider.kt")
public void testNoUnusedOnDelegationWithProvider() throws Exception {
runTest("compiler/testData/diagnostics/tests/noUnusedOnDelegationWithProvider.kt");
}
@Test
@TestMetadata("Nullability.kt")
public void testNullability() throws Exception {
@@ -1143,6 +1149,12 @@ public class DiagnosticCompilerTestFE10TestdataTestGenerated extends AbstractDia
runTest("compiler/testData/diagnostics/tests/UnusedParameters.kt");
}
@Test
@TestMetadata("unusedVariableOnRegularDelegatedProperty.kt")
public void testUnusedVariableOnRegularDelegatedProperty() throws Exception {
runTest("compiler/testData/diagnostics/tests/unusedVariableOnRegularDelegatedProperty.kt");
}
@Test
@TestMetadata("UnusedVariables.kt")
public void testUnusedVariables() throws Exception {
@@ -621,6 +621,12 @@ public class LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated
runTest("compiler/testData/diagnostics/tests/noSymbolProvidersDuplicationInDiamond.kt");
}
@Test
@TestMetadata("noUnusedOnDelegationWithProvider.kt")
public void testNoUnusedOnDelegationWithProvider() throws Exception {
runTest("compiler/testData/diagnostics/tests/noUnusedOnDelegationWithProvider.kt");
}
@Test
@TestMetadata("Nullability.kt")
public void testNullability() throws Exception {
@@ -1143,6 +1149,12 @@ public class LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated
runTest("compiler/testData/diagnostics/tests/UnusedParameters.kt");
}
@Test
@TestMetadata("unusedVariableOnRegularDelegatedProperty.kt")
public void testUnusedVariableOnRegularDelegatedProperty() throws Exception {
runTest("compiler/testData/diagnostics/tests/unusedVariableOnRegularDelegatedProperty.kt");
}
@Test
@TestMetadata("UnusedVariables.kt")
public void testUnusedVariables() throws Exception {
@@ -621,6 +621,12 @@ public class FirLightTreeOldFrontendDiagnosticsTestGenerated extends AbstractFir
runTest("compiler/testData/diagnostics/tests/noSymbolProvidersDuplicationInDiamond.kt");
}
@Test
@TestMetadata("noUnusedOnDelegationWithProvider.kt")
public void testNoUnusedOnDelegationWithProvider() throws Exception {
runTest("compiler/testData/diagnostics/tests/noUnusedOnDelegationWithProvider.kt");
}
@Test
@TestMetadata("Nullability.kt")
public void testNullability() throws Exception {
@@ -1143,6 +1149,12 @@ public class FirLightTreeOldFrontendDiagnosticsTestGenerated extends AbstractFir
runTest("compiler/testData/diagnostics/tests/UnusedParameters.kt");
}
@Test
@TestMetadata("unusedVariableOnRegularDelegatedProperty.kt")
public void testUnusedVariableOnRegularDelegatedProperty() throws Exception {
runTest("compiler/testData/diagnostics/tests/unusedVariableOnRegularDelegatedProperty.kt");
}
@Test
@TestMetadata("UnusedVariables.kt")
public void testUnusedVariables() throws Exception {
@@ -621,6 +621,12 @@ public class FirPsiOldFrontendDiagnosticsTestGenerated extends AbstractFirPsiDia
runTest("compiler/testData/diagnostics/tests/noSymbolProvidersDuplicationInDiamond.kt");
}
@Test
@TestMetadata("noUnusedOnDelegationWithProvider.kt")
public void testNoUnusedOnDelegationWithProvider() throws Exception {
runTest("compiler/testData/diagnostics/tests/noUnusedOnDelegationWithProvider.kt");
}
@Test
@TestMetadata("Nullability.kt")
public void testNullability() throws Exception {
@@ -1143,6 +1149,12 @@ public class FirPsiOldFrontendDiagnosticsTestGenerated extends AbstractFirPsiDia
runTest("compiler/testData/diagnostics/tests/UnusedParameters.kt");
}
@Test
@TestMetadata("unusedVariableOnRegularDelegatedProperty.kt")
public void testUnusedVariableOnRegularDelegatedProperty() throws Exception {
runTest("compiler/testData/diagnostics/tests/unusedVariableOnRegularDelegatedProperty.kt");
}
@Test
@TestMetadata("UnusedVariables.kt")
public void testUnusedVariables() throws Exception {
@@ -690,6 +690,14 @@ class ControlFlowInformationProviderImpl private constructor(
private val VariableDescriptor.isLocalVariableWithDelegate: Boolean
get() = this is LocalVariableDescriptor && this.isDelegated
private val VariableDescriptor.isLocalVariableWithProvideDelegate: Boolean
get() {
if (!isLocalVariableWithDelegate) return false
if (this !is VariableDescriptorWithAccessors) return false
return trace.bindingContext[PROVIDE_DELEGATE_RESOLVED_CALL, this] != null
}
private fun processUnusedDeclaration(
element: KtNamedDeclaration,
variableDescriptor: VariableDescriptor,
@@ -704,8 +712,11 @@ class ControlFlowInformationProviderImpl private constructor(
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)
KtPsiUtil.isRemovableVariableDeclaration(element) -> {
if (!variableDescriptor.isLocalVariableWithProvideDelegate) {
report(Errors.UNUSED_VARIABLE.on(element, variableDescriptor), ctxt)
}
}
element is KtParameter ->
processUnusedParameter(ctxt, element, variableDescriptor)
@@ -0,0 +1,27 @@
// FIR_IDENTICAL
// !DIAGNOSTICS: +UNUSED_VARIABLE
import kotlin.reflect.KProperty
class Example {
val valProp: String by Delegate()
val varProp: String by Delegate()
fun foo() {
val valVariable by Delegate()
val varVariable by Delegate()
}
}
class Delegate {
operator fun getValue(thisRef: Any?, property: KProperty<*>): String = "delegation"
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: String) {
// setValue
}
operator fun provideDelegate(thisRef: Any?, property: KProperty<*>): Delegate {
// side effect
return Delegate()
}
}
@@ -0,0 +1,21 @@
// !DIAGNOSTICS: +UNUSED_VARIABLE
import kotlin.reflect.KProperty
class Example {
val valProp: String by Delegate()
val varProp: String by Delegate()
fun foo() {
val valVariable by Delegate()
val varVariable by Delegate()
}
}
class Delegate {
operator fun getValue(thisRef: Any?, property: KProperty<*>): String = "delegation"
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: String) {
// setValue
}
}
@@ -0,0 +1,21 @@
// !DIAGNOSTICS: +UNUSED_VARIABLE
import kotlin.reflect.KProperty
class Example {
val valProp: String by Delegate()
val varProp: String by Delegate()
fun foo() {
val <!UNUSED_VARIABLE!>valVariable<!> by Delegate()
val <!UNUSED_VARIABLE!>varVariable<!> by Delegate()
}
}
class Delegate {
operator fun getValue(thisRef: Any?, property: KProperty<*>): String = "delegation"
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: String) {
// setValue
}
}
@@ -621,6 +621,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest {
runTest("compiler/testData/diagnostics/tests/noSymbolProvidersDuplicationInDiamond.kt");
}
@Test
@TestMetadata("noUnusedOnDelegationWithProvider.kt")
public void testNoUnusedOnDelegationWithProvider() throws Exception {
runTest("compiler/testData/diagnostics/tests/noUnusedOnDelegationWithProvider.kt");
}
@Test
@TestMetadata("Nullability.kt")
public void testNullability() throws Exception {
@@ -1143,6 +1149,12 @@ public class DiagnosticTestGenerated extends AbstractDiagnosticTest {
runTest("compiler/testData/diagnostics/tests/UnusedParameters.kt");
}
@Test
@TestMetadata("unusedVariableOnRegularDelegatedProperty.kt")
public void testUnusedVariableOnRegularDelegatedProperty() throws Exception {
runTest("compiler/testData/diagnostics/tests/unusedVariableOnRegularDelegatedProperty.kt");
}
@Test
@TestMetadata("UnusedVariables.kt")
public void testUnusedVariables() throws Exception {