Introduce inspection for redundant not-null extension receiver of inline

Related to KT-22303
This commit is contained in:
Mikhail Glukhikh
2018-01-19 16:14:30 +03:00
parent ad48099ca6
commit 7995ae05c5
8 changed files with 231 additions and 0 deletions
@@ -0,0 +1,42 @@
<problems>
<problem>
<file>test.kt</file>
<line>2</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Not-null extension receiver of inline function can be made nullable</problem_class>
<description>This type probably can be changed to nullable</description>
</problem>
<problem>
<file>test.kt</file>
<line>4</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Not-null extension receiver of inline function can be made nullable</problem_class>
<description>This type probably can be changed to nullable</description>
</problem>
<problem>
<file>test.kt</file>
<line>6</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Not-null extension receiver of inline function can be made nullable</problem_class>
<description>This type probably can be changed to nullable</description>
</problem>
<problem>
<file>test.kt</file>
<line>7</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Not-null extension receiver of inline function can be made nullable</problem_class>
<description>This type probably can be changed to nullable</description>
</problem>
<problem>
<file>test.kt</file>
<line>8</line>
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/test.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">Not-null extension receiver of inline function can be made nullable</problem_class>
<description>This type probably can be changed to nullable</description>
</problem>
</problems>
@@ -0,0 +1 @@
// INSPECTION_CLASS: org.jetbrains.kotlin.idea.inspections.RedundantNotNullExtensionReceiverOfInlineInspection
@@ -0,0 +1,51 @@
// Typical true positive
inline fun String.foo() = foo(this)
fun String.notInlineFoo() = foo(this)
inline fun String.eq(other: Any?) = this == other
// Other positives
inline fun String.fooo1() = fooo()
inline fun String.fooo2() = this.fooo()
inline fun String.fooo3() = this?.foo()
// Just functions
fun foo(s: String?) {}
fun String?.fooo() {}
fun bar(s: String) {}
fun String.baz() = this
inline fun String.bar() = bar(this) // No problem
inline fun String.bazz() = baz() // No problem
inline fun String.bazzx() = baz().baz() // No problem
inline fun String.bazzz() = this.baz() // No problem
interface My {
inline fun String.bar() // No problem (no body)
}
inline fun String.myLength() = length // No problem (this.length)
// No problem (this.size)
inline fun <reified T> List<T>.count() {
return size
}
// No problem (in this)
inline fun <reified T> List<T>.foo() {
for (element in this) {}
}
// No problem (cast)
inline fun String.cast(): Any = this as Any
class Some {
companion object {}
}
fun Some.Companion.foo() {} // No problem (on companion)
inline fun Int.baz(x: Int) = this + x // No problem
inline fun Int.bar(x: Int) = x - this // No problem
inline fun Int.unary() = -this // No problem
inline fun String.indexed(arg: Int) = this[arg] // No problem