From 681e85eaed8e047ea955cbc8b3ea5037b1581cf9 Mon Sep 17 00:00:00 2001 From: Mikhail Zarechenskiy Date: Mon, 27 Mar 2023 11:20:33 +0200 Subject: [PATCH] Don't report unused variable warning for delegated variables with provide delegate ^KT-38871 Fixed --- ...CompilerTestFE10TestdataTestGenerated.java | 12 +++++++++ ...sticCompilerFE10TestDataTestGenerated.java | 12 +++++++++ ...eeOldFrontendDiagnosticsTestGenerated.java | 12 +++++++++ ...siOldFrontendDiagnosticsTestGenerated.java | 12 +++++++++ .../cfg/ControlFlowInformationProviderImpl.kt | 15 +++++++++-- .../tests/noUnusedOnDelegationWithProvider.kt | 27 +++++++++++++++++++ ...dVariableOnRegularDelegatedProperty.fir.kt | 21 +++++++++++++++ ...nusedVariableOnRegularDelegatedProperty.kt | 21 +++++++++++++++ .../test/runners/DiagnosticTestGenerated.java | 12 +++++++++ 9 files changed, 142 insertions(+), 2 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/noUnusedOnDelegationWithProvider.kt create mode 100644 compiler/testData/diagnostics/tests/unusedVariableOnRegularDelegatedProperty.fir.kt create mode 100644 compiler/testData/diagnostics/tests/unusedVariableOnRegularDelegatedProperty.kt diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java index 5bcafd145ca..56fa007b4fd 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/DiagnosticCompilerTestFE10TestdataTestGenerated.java @@ -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 { diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java index 08f407e4484..d56c6d201ce 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/diagnostic/compiler/based/LLFirPreresolvedReversedDiagnosticCompilerFE10TestDataTestGenerated.java @@ -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 { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java index bcc3ba28f8f..2c78378eb9f 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirLightTreeOldFrontendDiagnosticsTestGenerated.java @@ -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 { diff --git a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java index 7657c5f5425..7cf9bc81146 100644 --- a/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java +++ b/compiler/fir/analysis-tests/tests-gen/org/jetbrains/kotlin/test/runners/FirPsiOldFrontendDiagnosticsTestGenerated.java @@ -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 { diff --git a/compiler/frontend/cfg/src/org/jetbrains/kotlin/cfg/ControlFlowInformationProviderImpl.kt b/compiler/frontend/cfg/src/org/jetbrains/kotlin/cfg/ControlFlowInformationProviderImpl.kt index feb578523e6..89b5e3f0670 100644 --- a/compiler/frontend/cfg/src/org/jetbrains/kotlin/cfg/ControlFlowInformationProviderImpl.kt +++ b/compiler/frontend/cfg/src/org/jetbrains/kotlin/cfg/ControlFlowInformationProviderImpl.kt @@ -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) diff --git a/compiler/testData/diagnostics/tests/noUnusedOnDelegationWithProvider.kt b/compiler/testData/diagnostics/tests/noUnusedOnDelegationWithProvider.kt new file mode 100644 index 00000000000..16eff119194 --- /dev/null +++ b/compiler/testData/diagnostics/tests/noUnusedOnDelegationWithProvider.kt @@ -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() + } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/unusedVariableOnRegularDelegatedProperty.fir.kt b/compiler/testData/diagnostics/tests/unusedVariableOnRegularDelegatedProperty.fir.kt new file mode 100644 index 00000000000..9fe9843a784 --- /dev/null +++ b/compiler/testData/diagnostics/tests/unusedVariableOnRegularDelegatedProperty.fir.kt @@ -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 + } +} diff --git a/compiler/testData/diagnostics/tests/unusedVariableOnRegularDelegatedProperty.kt b/compiler/testData/diagnostics/tests/unusedVariableOnRegularDelegatedProperty.kt new file mode 100644 index 00000000000..885f61efee7 --- /dev/null +++ b/compiler/testData/diagnostics/tests/unusedVariableOnRegularDelegatedProperty.kt @@ -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 + } +} diff --git a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java index 1e22d333724..3ddf58dba1d 100644 --- a/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java +++ b/compiler/tests-common-new/tests-gen/org/jetbrains/kotlin/test/runners/DiagnosticTestGenerated.java @@ -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 {