KT-12152 : leaking this inspection: accessing non-final member / this in non-final class

This commit is contained in:
Mikhail Glukhikh
2016-05-24 16:38:00 +03:00
parent 3d6bd81933
commit a22e7d3bcf
10 changed files with 301 additions and 0 deletions
@@ -0,0 +1,26 @@
<problems>
<problem>
<file>test.kt</file>
<line>18</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Leaking 'this' in constructor</problem_class>
<description>Leaking 'this' in constructor of non-final class Second</description>
</problem>
<problem>
<file>test.kt</file>
<line>20</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Leaking 'this' in constructor</problem_class>
<description>Calling non-final function foo in constructor</description>
</problem>
<problem>
<file>test.kt</file>
<line>40</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Leaking 'this' in constructor</problem_class>
<description>Accessing non-final property x in constructor</description>
</problem>
</problems>
@@ -0,0 +1 @@
// INSPECTION_CLASS: org.jetbrains.kotlin.idea.inspections.LeakingThisInspection
+56
View File
@@ -0,0 +1,56 @@
class First {
val x: String
init {
use(this) // NPE! Leaking this
x = foo() // NPE! Own function
}
fun foo() = x
}
fun use(first: First) = first.x.hashCode()
abstract class Second {
val x: String
init {
use(this) // Leaking this in non-final
x = bar() // Own function in non-final
foo() // Non-final function call
}
private fun bar() = foo()
abstract fun foo(): String
}
fun use(second: Second) = second.x
class SecondDerived : Second() {
val y = x // null!
override fun foo() = y
}
open class Third {
open var x: String
constructor() {
x = "X" // Non-final property access
}
}
class ThirdDerived : Third() {
override var x: String = "Y"
set(arg) { field = "$arg$y" }
val y = ""
}
class Fourth {
val x: String
get() = y
val y = x // null!
}