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,24 @@
<html>
<body>
This inspection reports dangerous operations inside constructors including:
<ul>
<li>Accessing non-final property in constructor</li>
<li>Calling non-final function in constructor</li>
<li>Using 'this' as function argument in constructor of non-final class</li>
</ul>
These operations are dangerous because your class can be inherited,
and derived class is not yet initialized at this moment. Typical example:
<code><pre>
<b>abstract class</b> Base {
<b>val</b> code = calculate()
<b>abstract fun</b> calculate(): Int
}
<b>class</b> Derived(<b>private val</b> x: Int) : Base() {
<b>override fun</b> calculate() = x
}
<b>fun</b> testIt() {
println(Derived(42).code) <i>// Expected: 42, actual: 0</i>
}
</pre></code>
</body>
</html>