Fix false positive in class literal #KT-16338 Fixed

This commit is contained in:
Toshiaki Kameyama
2018-09-21 18:54:45 +09:00
committed by Mikhail Glukhikh
parent 6cd13341ee
commit 65f23f3c4e
3 changed files with 27 additions and 1 deletions
@@ -19,6 +19,7 @@ import org.jetbrains.kotlin.idea.quickfix.AddModifierFix
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.containingClassOrObject
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
import org.jetbrains.kotlin.resolve.BindingContext.LEAKING_THIS
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
@@ -35,7 +36,7 @@ class LeakingThisInspection : AbstractKotlinInspection() {
if (leakingThisDescriptor.classOrObject != klass) continue@these
val description = when (leakingThisDescriptor) {
is NonFinalClass ->
if (expression is KtThisExpression)
if (expression is KtThisExpression && expression.getStrictParentOfType<KtClassLiteralExpression>() == null)
"Leaking 'this' in constructor of non-final class ${leakingThisDescriptor.klass.name}"
else
continue@these // Not supported yet
@@ -0,0 +1,20 @@
// PROBLEM: none
import kotlin.reflect.KClass
open class Foo {
init {
test(<caret>this::class)
}
}
private fun test(c: KClass<out Foo>) {
// println(c)
}
class Bar : Foo()
fun main(args: Array<String>) {
Foo()
Bar()
}
@@ -2587,6 +2587,11 @@ public class LocalInspectionTestGenerated extends AbstractLocalInspectionTest {
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("idea/testData/inspectionsLocal/leakingThis"), Pattern.compile("^([\\w\\-_]+)\\.(kt|kts)$"), TargetBackend.ANY, true);
}
@TestMetadata("inClassLiteral.kt")
public void testInClassLiteral() throws Exception {
runTest("idea/testData/inspectionsLocal/leakingThis/inClassLiteral.kt");
}
@TestMetadata("noOpenForInterface.kt")
public void testNoOpenForInterface() throws Exception {
runTest("idea/testData/inspectionsLocal/leakingThis/noOpenForInterface.kt");