From 7c0af78b087e0c68936bded3e25d20a74df7f37c Mon Sep 17 00:00:00 2001 From: Toshiaki Kameyama Date: Sat, 1 Feb 2020 23:00:02 +0900 Subject: [PATCH] UnnecessaryVariableInspection: fix false positive in lambda #KT-32565 Fixed --- .../UnnecessaryVariableInspection.kt | 5 +++++ .../unnecessaryVariable/copyOfValInLambda.kt | 16 ++++++++++++++++ .../unnecessaryVariable/copyOfValInLambda2.kt | 19 +++++++++++++++++++ .../unnecessaryVariable/copyOfValInLambda3.kt | 16 ++++++++++++++++ .../copyOfValInLambda3.kt.after | 15 +++++++++++++++ .../LocalInspectionTestGenerated.java | 15 +++++++++++++++ 6 files changed, 86 insertions(+) create mode 100644 idea/testData/inspectionsLocal/unnecessaryVariable/copyOfValInLambda.kt create mode 100644 idea/testData/inspectionsLocal/unnecessaryVariable/copyOfValInLambda2.kt create mode 100644 idea/testData/inspectionsLocal/unnecessaryVariable/copyOfValInLambda3.kt create mode 100644 idea/testData/inspectionsLocal/unnecessaryVariable/copyOfValInLambda3.kt.after diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/UnnecessaryVariableInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/UnnecessaryVariableInspection.kt index 263719eb797..98ce99eeed4 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/UnnecessaryVariableInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/UnnecessaryVariableInspection.kt @@ -90,6 +90,11 @@ class UnnecessaryVariableInspection : AbstractApplicabilityBasedInspectionfoundCredentials = credentials + } + } + + println(data.foundCredentials) +} diff --git a/idea/testData/inspectionsLocal/unnecessaryVariable/copyOfValInLambda2.kt b/idea/testData/inspectionsLocal/unnecessaryVariable/copyOfValInLambda2.kt new file mode 100644 index 00000000000..05048fdbaac --- /dev/null +++ b/idea/testData/inspectionsLocal/unnecessaryVariable/copyOfValInLambda2.kt @@ -0,0 +1,19 @@ +// PROBLEM: none +// WITH_RUNTIME +fun findCredentials() = 1 + +fun println(i: Int) {} + +fun test() { + val data = run { + val credentials = findCredentials() + + class Foo { + val foundCredentials = credentials + } + + Foo() + } + + val foundCredentials = data.foundCredentials +} \ No newline at end of file diff --git a/idea/testData/inspectionsLocal/unnecessaryVariable/copyOfValInLambda3.kt b/idea/testData/inspectionsLocal/unnecessaryVariable/copyOfValInLambda3.kt new file mode 100644 index 00000000000..f082d13866e --- /dev/null +++ b/idea/testData/inspectionsLocal/unnecessaryVariable/copyOfValInLambda3.kt @@ -0,0 +1,16 @@ +// WITH_RUNTIME +fun findCredentials() = 1 + +fun println(i: Int) {} + +fun test() { + val credentials = findCredentials() + + val data = run { + object { + val foundCredentials = credentials + } + } + + println(data.foundCredentials) +} diff --git a/idea/testData/inspectionsLocal/unnecessaryVariable/copyOfValInLambda3.kt.after b/idea/testData/inspectionsLocal/unnecessaryVariable/copyOfValInLambda3.kt.after new file mode 100644 index 00000000000..05d7566f22f --- /dev/null +++ b/idea/testData/inspectionsLocal/unnecessaryVariable/copyOfValInLambda3.kt.after @@ -0,0 +1,15 @@ +// WITH_RUNTIME +fun findCredentials() = 1 + +fun println(i: Int) {} + +fun test() { + val credentials = findCredentials() + + val data = run { + object { + } + } + + println(credentials) +} diff --git a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java index e5b7227234d..871e5b6ec9a 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java +++ b/idea/tests/org/jetbrains/kotlin/idea/inspections/LocalInspectionTestGenerated.java @@ -13201,6 +13201,21 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest { runTest("idea/testData/inspectionsLocal/unnecessaryVariable/copyOfVal.kt"); } + @TestMetadata("copyOfValInLambda.kt") + public void testCopyOfValInLambda() throws Exception { + runTest("idea/testData/inspectionsLocal/unnecessaryVariable/copyOfValInLambda.kt"); + } + + @TestMetadata("copyOfValInLambda2.kt") + public void testCopyOfValInLambda2() throws Exception { + runTest("idea/testData/inspectionsLocal/unnecessaryVariable/copyOfValInLambda2.kt"); + } + + @TestMetadata("copyOfValInLambda3.kt") + public void testCopyOfValInLambda3() throws Exception { + runTest("idea/testData/inspectionsLocal/unnecessaryVariable/copyOfValInLambda3.kt"); + } + @TestMetadata("copyOfValUnused.kt") public void testCopyOfValUnused() throws Exception { runTest("idea/testData/inspectionsLocal/unnecessaryVariable/copyOfValUnused.kt");