From 0cd1da030710a2565970b5879bc3a4169110ca47 Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Thu, 10 Apr 2025 12:22:19 +0200 Subject: [PATCH] [kotlin] narrow down the highlighting range for foreach parameter unused inspection ^KTIJ-33767 fixed GitOrigin-RevId: 2b5b70fa422dd2b052945744d8e67f0e6f7fc1e4 --- .../shared/ForEachParameterNotUsedInspection.kt | 5 +++++ .../shared/SharedK1LocalInspectionTestGenerated.java | 5 +++++ .../shared/SharedK2LocalInspectionTestGenerated.java | 5 +++++ .../outsideOfApplicableRange.kt | 10 ++++++++++ 4 files changed, 25 insertions(+) create mode 100644 plugins/kotlin/code-insight/inspections-shared/tests/testData/inspectionsLocal/forEachParameterNotUsed/outsideOfApplicableRange.kt diff --git a/plugins/kotlin/code-insight/inspections-shared/src/org/jetbrains/kotlin/idea/codeInsight/inspections/shared/ForEachParameterNotUsedInspection.kt b/plugins/kotlin/code-insight/inspections-shared/src/org/jetbrains/kotlin/idea/codeInsight/inspections/shared/ForEachParameterNotUsedInspection.kt index 0f3aba5a00ec..31a899af5a1d 100644 --- a/plugins/kotlin/code-insight/inspections-shared/src/org/jetbrains/kotlin/idea/codeInsight/inspections/shared/ForEachParameterNotUsedInspection.kt +++ b/plugins/kotlin/code-insight/inspections-shared/src/org/jetbrains/kotlin/idea/codeInsight/inspections/shared/ForEachParameterNotUsedInspection.kt @@ -25,6 +25,7 @@ import org.jetbrains.kotlin.idea.base.resources.KotlinBundle import org.jetbrains.kotlin.idea.codeInsight.inspections.shared.ForEachParameterNotUsedInspection.UnusedForEachParameterInfo import org.jetbrains.kotlin.idea.codeinsight.api.applicable.inspections.KotlinApplicableInspectionBase import org.jetbrains.kotlin.idea.codeinsight.api.applicable.inspections.KotlinModCommandQuickFix +import org.jetbrains.kotlin.idea.codeinsights.impl.base.applicators.ApplicabilityRanges import org.jetbrains.kotlin.idea.refactoring.moveFunctionLiteralOutsideParentheses import org.jetbrains.kotlin.idea.refactoring.util.setParameterListIfAny import org.jetbrains.kotlin.name.CallableId @@ -49,6 +50,10 @@ internal class ForEachParameterNotUsedInspection : /* ...fixes = */ *createQuickFixes(element, context).toTypedArray() ) + override fun getApplicableRanges(element: KtCallExpression): List { + return ApplicabilityRanges.calleeExpression(element) + } + private fun createQuickFixes( element: KtCallExpression, context: UnusedForEachParameterInfo, diff --git a/plugins/kotlin/code-insight/inspections-shared/tests/k1/test/org/jetbrains/kotlin/idea/codeInsight/inspections/shared/SharedK1LocalInspectionTestGenerated.java b/plugins/kotlin/code-insight/inspections-shared/tests/k1/test/org/jetbrains/kotlin/idea/codeInsight/inspections/shared/SharedK1LocalInspectionTestGenerated.java index 79c14db47532..76346f89370a 100644 --- a/plugins/kotlin/code-insight/inspections-shared/tests/k1/test/org/jetbrains/kotlin/idea/codeInsight/inspections/shared/SharedK1LocalInspectionTestGenerated.java +++ b/plugins/kotlin/code-insight/inspections-shared/tests/k1/test/org/jetbrains/kotlin/idea/codeInsight/inspections/shared/SharedK1LocalInspectionTestGenerated.java @@ -1298,6 +1298,11 @@ public abstract class SharedK1LocalInspectionTestGenerated extends AbstractShare runTest("../testData/inspectionsLocal/forEachParameterNotUsed/normal.kt"); } + @TestMetadata("outsideOfApplicableRange.kt") + public void testOutsideOfApplicableRange() throws Exception { + runTest("../testData/inspectionsLocal/forEachParameterNotUsed/outsideOfApplicableRange.kt"); + } + @TestMetadata("repeat.kt") public void testRepeat() throws Exception { runTest("../testData/inspectionsLocal/forEachParameterNotUsed/repeat.kt"); diff --git a/plugins/kotlin/code-insight/inspections-shared/tests/k2/test/org/jetbrains/kotlin/idea/k2/codeInsight/inspections/shared/SharedK2LocalInspectionTestGenerated.java b/plugins/kotlin/code-insight/inspections-shared/tests/k2/test/org/jetbrains/kotlin/idea/k2/codeInsight/inspections/shared/SharedK2LocalInspectionTestGenerated.java index 8a5845cbd135..a633dd31b90d 100644 --- a/plugins/kotlin/code-insight/inspections-shared/tests/k2/test/org/jetbrains/kotlin/idea/k2/codeInsight/inspections/shared/SharedK2LocalInspectionTestGenerated.java +++ b/plugins/kotlin/code-insight/inspections-shared/tests/k2/test/org/jetbrains/kotlin/idea/k2/codeInsight/inspections/shared/SharedK2LocalInspectionTestGenerated.java @@ -1298,6 +1298,11 @@ public abstract class SharedK2LocalInspectionTestGenerated extends AbstractShare runTest("../testData/inspectionsLocal/forEachParameterNotUsed/normal.kt"); } + @TestMetadata("outsideOfApplicableRange.kt") + public void testOutsideOfApplicableRange() throws Exception { + runTest("../testData/inspectionsLocal/forEachParameterNotUsed/outsideOfApplicableRange.kt"); + } + @TestMetadata("repeat.kt") public void testRepeat() throws Exception { runTest("../testData/inspectionsLocal/forEachParameterNotUsed/repeat.kt"); diff --git a/plugins/kotlin/code-insight/inspections-shared/tests/testData/inspectionsLocal/forEachParameterNotUsed/outsideOfApplicableRange.kt b/plugins/kotlin/code-insight/inspections-shared/tests/testData/inspectionsLocal/forEachParameterNotUsed/outsideOfApplicableRange.kt new file mode 100644 index 000000000000..dbe7906223a4 --- /dev/null +++ b/plugins/kotlin/code-insight/inspections-shared/tests/testData/inspectionsLocal/forEachParameterNotUsed/outsideOfApplicableRange.kt @@ -0,0 +1,10 @@ +// WITH_STDLIB +// PROBLEM: none + +fun test(list: List) { + list.forEach { + // + // + // + } +} \ No newline at end of file