UnnecessaryVariableInspection: fix false positive in lambda
#KT-32565 Fixed
This commit is contained in:
committed by
Yan Zhulanow
parent
64f6ed586b
commit
7c0af78b08
@@ -90,6 +90,11 @@ class UnnecessaryVariableInspection : AbstractApplicabilityBasedInspection<KtPro
|
||||
)
|
||||
)
|
||||
if (!validator(copyName)) return false
|
||||
if (containingDeclaration is KtClassOrObject) {
|
||||
val enclosingBlock = enclosingElement as? KtBlockExpression
|
||||
val initializerDeclaration = DescriptorToSourceUtils.descriptorToDeclaration(initializerDescriptor)
|
||||
if (enclosingBlock?.statements?.none { it == initializerDeclaration } == true) return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
// PROBLEM: none
|
||||
// WITH_RUNTIME
|
||||
fun findCredentials() = 1
|
||||
|
||||
fun println(i: Int) {}
|
||||
|
||||
fun test() {
|
||||
val data = run {
|
||||
val credentials = findCredentials()
|
||||
object {
|
||||
val <caret>foundCredentials = credentials
|
||||
}
|
||||
}
|
||||
|
||||
println(data.foundCredentials)
|
||||
}
|
||||
@@ -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 <caret>foundCredentials = credentials
|
||||
}
|
||||
|
||||
Foo()
|
||||
}
|
||||
|
||||
val foundCredentials = data.foundCredentials
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
// WITH_RUNTIME
|
||||
fun findCredentials() = 1
|
||||
|
||||
fun println(i: Int) {}
|
||||
|
||||
fun test() {
|
||||
val credentials = findCredentials()
|
||||
|
||||
val data = run {
|
||||
object {
|
||||
val <caret>foundCredentials = credentials
|
||||
}
|
||||
}
|
||||
|
||||
println(data.foundCredentials)
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
// WITH_RUNTIME
|
||||
fun findCredentials() = 1
|
||||
|
||||
fun println(i: Int) {}
|
||||
|
||||
fun test() {
|
||||
val credentials = findCredentials()
|
||||
|
||||
val data = run {
|
||||
object {
|
||||
}
|
||||
}
|
||||
|
||||
println(credentials)
|
||||
}
|
||||
+15
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user