Refactoring: make "if-then to safe access" an inspection

This commit is contained in:
Mikhail Glukhikh
2017-12-19 15:41:09 +03:00
parent 91bcfb97c6
commit a8b01a6b00
86 changed files with 433 additions and 410 deletions
@@ -0,0 +1 @@
org.jetbrains.kotlin.idea.inspections.branchedTransformations.IfThenToSafeAccessInspection
@@ -1,4 +1,4 @@
// IS_APPLICABLE: false
// PROBLEM: none
fun <T> doSomething(a: T) {}
fun main(args: Array<String>) {
@@ -1,4 +1,4 @@
// IS_APPLICABLE: false
// PROBLEM: none
fun main(args: Array<String>) {
var foo: String? = "foo"
var bar: String? = "bar"
@@ -1,4 +1,4 @@
// IS_APPLICABLE: false
// PROBLEM: none
fun main(args: Array<String>) {
val foo = "foo"
if (null == <caret>null) {
@@ -1,4 +1,4 @@
// IS_APPLICABLE: false
// PROBLEM: none
operator fun <T> T.compareTo(a: T): Int = 0
fun main(args: Array<String>) {
@@ -1,4 +1,4 @@
// IS_APPLICABLE: false
// PROBLEM: none
operator fun String?.times(a: Int): Boolean = a == 0
fun main(args: Array<String>) {
@@ -7,7 +7,7 @@ fun <T> doSomething(a: T) {}
fun main(args: Array<String>) {
val foo = maybeFoo()
doSomething(foo)
if (foo != null<caret>) {
i<caret>f (foo != null) {
foo.length
}
else {
@@ -5,7 +5,7 @@ fun maybeFoo(): String? {
val x = maybeFoo()
fun main(args: Array<String>) {
if (x !=<caret> null) {
<caret>if (x != null) {
x.length
} else {
null
@@ -1,4 +1,4 @@
// IS_APPLICABLE: false
// PROBLEM: none
fun main(args: Array<String>) {
val foo = "foo"
if (<caret>) {
@@ -1,4 +1,4 @@
// IS_APPLICABLE: false
// PROBLEM: none
fun maybeFoo(): String? {
return "foo"
@@ -1,4 +1,4 @@
// IS_APPLICABLE: false
// PROBLEM: none
fun maybeFoo(): String? {
return "foo"
@@ -4,7 +4,7 @@ fun maybeFoo(): String? {
fun main(args: Array<String>) {
val foo = maybeFoo()
if (foo != null<caret>) {
<caret>if (foo != null) {
foo.length
}
else {
@@ -4,7 +4,7 @@ fun maybeFoo(): String? {
fun main(args: Array<String>) {
val foo = maybeFoo()
if (foo != null<caret>)
i<caret>f (foo != null)
foo.length
else
null
@@ -4,7 +4,7 @@ fun maybeFoo(): String? {
fun main(args: Array<String>) {
val foo = maybeFoo()
val x = if (foo == null<caret>) {
val x = <caret>if (foo == null) {
null
}
else {
@@ -5,7 +5,7 @@
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/src/rhsNotEqualsNull.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">If-Then foldable to '?.'</problem_class>
<description>Replace 'if' expression with safe access expression</description>
<description>Foldable if-then</description>
</problem>
<problem>
<file>rhsEqualsNull.kt</file>
@@ -13,7 +13,7 @@
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/src/rhsEqualsNull.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">If-Then foldable to '?.'</problem_class>
<description>Replace 'if' expression with safe access expression</description>
<description>Foldable if-then</description>
</problem>
<problem>
<file>noThenBlock.kt</file>
@@ -21,7 +21,7 @@
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/src/noThenBlock.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">If-Then foldable to '?.'</problem_class>
<description>Replace 'if' expression with safe access expression</description>
<description>Foldable if-then</description>
</problem>
<problem>
<file>noElseBlock.kt</file>
@@ -29,7 +29,7 @@
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/src/noElseBlock.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">If-Then foldable to '?.'</problem_class>
<description>Replace 'if' expression with safe access expression</description>
<description>Foldable if-then</description>
</problem>
<problem>
<file>lhsNotEqualsNull.kt</file>
@@ -37,7 +37,7 @@
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/src/lhsNotEqualsNull.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">If-Then foldable to '?.'</problem_class>
<description>Replace 'if' expression with safe access expression</description>
<description>Foldable if-then</description>
</problem>
<problem>
<file>lhsEqualsNull.kt</file>
@@ -45,7 +45,7 @@
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/src/lhsEqualsNull.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">If-Then foldable to '?.'</problem_class>
<description>Replace 'if' expression with safe access expression</description>
<description>Foldable if-then</description>
</problem>
<problem>
<file>ifAsExpression.kt</file>
@@ -53,7 +53,7 @@
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/src/ifAsExpression.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">If-Then foldable to '?.'</problem_class>
<description>Replace 'if' expression with safe access expression</description>
<description>Foldable if-then</description>
</problem>
<problem>
<file>ifAndElseNotInBlocks.kt</file>
@@ -61,7 +61,7 @@
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/src/ifAndElseNotInBlocks.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">If-Then foldable to '?.'</problem_class>
<description>Replace 'if' expression with safe access expression</description>
<description>Foldable if-then</description>
</problem>
<problem>
<file>ifAndElseBothInBlocks.kt</file>
@@ -69,7 +69,7 @@
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/src/ifAndElseBothInBlocks.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">If-Then foldable to '?.'</problem_class>
<description>Replace 'if' expression with safe access expression</description>
<description>Foldable if-then</description>
</problem>
<problem>
<file>doesNotinlineValueOutsideOfScope.kt</file>
@@ -77,7 +77,7 @@
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/src/doesNotInlineValueOutsideOfScope.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">If-Then foldable to '?.'</problem_class>
<description>Replace 'if' expression with safe access expression</description>
<description>Foldable if-then</description>
</problem>
<problem>
<file>doesNotinlineValueIfUsedMoreThanOnce.kt</file>
@@ -85,7 +85,7 @@
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/src/doesNotInlineValueIfUsedMoreThanOnce.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">If-Then foldable to '?.'</problem_class>
<description>Replace 'if' expression with safe access expression</description>
<description>Foldable if-then</description>
</problem>
<problem>
<file>willNotInlineClassProperty.kt</file>
@@ -93,7 +93,7 @@
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/src/willNotInlineClassProperty.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">If-Then foldable to '?.'</problem_class>
<description>Replace 'if' expression with safe access expression</description>
<description>Foldable if-then</description>
</problem>
<problem>
<file>isCondition.kt</file>
@@ -101,7 +101,7 @@
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/src/isCondition.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">If-Then foldable to '?.'</problem_class>
<description>Replace 'if' expression with safe access expression</description>
<description>Foldable if-then</description>
</problem>
<problem>
<file>isNotCondition.kt</file>
@@ -109,7 +109,7 @@
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/src/isNotCondition.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">If-Then foldable to '?.'</problem_class>
<description>Replace 'if' expression with safe access expression</description>
<description>Foldable if-then</description>
</problem>
<problem>
<file>nullCheckWithSelectorCallChain.kt</file>
@@ -117,7 +117,7 @@
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/nullCheckWithSelectorCallChain.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">If-Then foldable to '?.'</problem_class>
<description>Replace 'if' expression with safe access expression</description>
<description>Foldable if-then</description>
</problem>
<problem>
<file>isCheckWithSelectorChain.kt</file>
@@ -125,7 +125,7 @@
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/isCheckWithSelectorChain.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">If-Then foldable to '?.'</problem_class>
<description>Replace 'if' expression with safe access expression</description>
<description>Foldable if-then</description>
</problem>
<problem>
<file>isCheckSimple.kt</file>
@@ -133,7 +133,7 @@
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/isCheckSimple.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">If-Then foldable to '?.'</problem_class>
<description>Replace 'if' expression with safe cast expression</description>
<description>Foldable if-then</description>
</problem>
<problem>
<file>nullCheckSimple.kt</file>
@@ -141,7 +141,7 @@
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/nullCheckSimple.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">If-Then foldable to '?.'</problem_class>
<description>Remove redundant 'if' expression</description>
<description>Foldable if-then</description>
</problem>
<problem>
<file>implicitReceiver.kt</file>
@@ -149,7 +149,7 @@
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/implicitReceiver.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">If-Then foldable to '?.'</problem_class>
<description>Replace 'if' expression with safe access expression</description>
<description>Foldable if-then</description>
</problem>
<problem>
<file>property.kt</file>
@@ -157,7 +157,7 @@
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/property.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">If-Then foldable to '?.'</problem_class>
<description>Replace 'if' expression with safe cast expression</description>
<description>Foldable if-then</description>
</problem>
<problem>
<file>propertyNotNull.kt</file>
@@ -165,6 +165,6 @@
<module>light_idea_test_case</module>
<entry_point TYPE="file" FQNAME="temp:///src/propertyNotNull.kt" />
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">If-Then foldable to '?.'</problem_class>
<description>Remove redundant 'if' expression</description>
<description>Foldable if-then</description>
</problem>
</problems>
@@ -0,0 +1,2 @@
// INSPECTION_CLASS: org.jetbrains.kotlin.idea.inspections.branchedTransformations.IfThenToSafeAccessInspection
// WITH_RUNTIME
@@ -1,5 +1,5 @@
class My(val x: Int)
fun foo(arg: Any?): My? {
return if (<caret>arg is My) arg else null
return <caret>if (arg is My) arg else null
}
@@ -0,0 +1,5 @@
class My(val x: Int)
fun foo(arg: Any?): Int? {
return i<caret>f (arg is My) arg.x.hashCode() else null
}
@@ -0,0 +1 @@
fun foo(arg: Any) = <caret>if (arg is String) arg.length else null
@@ -0,0 +1 @@
fun foo(arg: Any) = <caret>if (arg !is String) null else arg.length
@@ -1,2 +1,2 @@
// IS_APPLICABLE: false
// PROBLEM: none
fun foo(arg: Any) = if (arg !is String?<caret>) null else arg?.length
@@ -1,2 +1,2 @@
// IS_APPLICABLE: false
// PROBLEM: none
fun foo(arg: Any) = if (arg is String?<caret>) arg?.length else null
@@ -4,7 +4,7 @@ fun maybeFoo(): String? {
fun main(args: Array<String>) {
val foo = maybeFoo()
if (foo == null<caret>)
<caret>if (foo == null)
null
else
foo.length
@@ -4,7 +4,7 @@ fun maybeFoo(): String? {
fun main(args: Array<String>) {
val foo = maybeFoo()
if (foo != null<caret>)
<caret>if (foo != null)
foo.length
else
null
@@ -1,4 +1,4 @@
// IS_APPLICABLE: false
// PROBLEM: none
fun main(args: Array<String>) {
val foo = null
if (foo == null<caret>) {
@@ -1,4 +1,4 @@
// IS_APPLICABLE: false
// PROBLEM: none
fun main(args: Array<String>) {
val foo = null
if (foo != null<caret>)
@@ -1,4 +1,4 @@
// IS_APPLICABLE: false
// PROBLEM: none
// ERROR: Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type String?
fun main(args: Array<String>) {
val foo: String? = "foo"
@@ -4,7 +4,7 @@ fun maybeFoo(): String? {
fun main(args: Array<String>) {
val foo = maybeFoo()
if (foo != null<caret>) {
<caret>if (foo != null) {
foo.length
}
}
@@ -1,4 +1,4 @@
// IS_APPLICABLE: false
// PROBLEM: none
// ERROR: 'if' must have both main and 'else' branches if used as an expression
// ERROR: Type mismatch: inferred type is Unit but Int was expected
@@ -1,4 +1,4 @@
// IS_APPLICABLE: false
// PROBLEM: none
fun main(args: Array<String>) {
val foo: String? = "foo"
val bar: String? = null
@@ -1,4 +1,4 @@
// IS_APPLICABLE: false
// PROBLEM: none
fun main(args: Array<String>) {
val foo: String? = "foo"
val bar: String? = null
@@ -4,7 +4,7 @@ fun maybeFoo(): String? {
fun main(args: Array<String>) {
val foo = maybeFoo()
if (foo == null<caret>) else {
<caret>if (foo == null) else {
foo.length
}
}
@@ -1,4 +1,4 @@
//IS_APPLICABLE: false
// PROBLEM: none
fun maybeFoo(): String? {
return "foo"
}
@@ -1,4 +1,4 @@
//IS_APPLICABLE: false
// PROBLEM: none
fun maybeFoo(): String? {
return "foo"
}
@@ -0,0 +1,3 @@
fun foo(arg: Any?): Any? {
return <caret>if (arg != null) arg else null
}
@@ -2,7 +2,7 @@
val nullableString: String? = "abc"
val foo = if (<caret>nullableString != null) {
val foo = <caret>if (nullableString != null) {
nullableString.toUpperCase().toLowerCase()
} else {
null
@@ -1,4 +1,4 @@
// IS_APPLICABLE: false
// PROBLEM: none
fun <T> doSomething(a: T) {}
fun main(args: Array<String>) {
@@ -1,5 +1,5 @@
// IS_APPLICABLE: true
// INTENTION_TEXT: Replace 'if' expression with safe cast expression
// FIX: Replace 'if' expression with safe cast expression
interface Foo
interface Bar : Foo
@@ -1,5 +1,5 @@
// IS_APPLICABLE: true
// INTENTION_TEXT: Replace 'if' expression with safe cast expression
// FIX: Replace 'if' expression with safe cast expression
interface Foo
interface Bar : Foo
@@ -1,5 +1,5 @@
// IS_APPLICABLE: true
// INTENTION_TEXT: Remove redundant 'if' expression
// FIX: Remove redundant 'if' expression
interface Bar
@@ -1,5 +1,5 @@
// IS_APPLICABLE: true
// INTENTION_TEXT: Remove redundant 'if' expression
// FIX: Remove redundant 'if' expression
interface Bar
@@ -1,4 +1,4 @@
// IS_APPLICABLE: false
// PROBLEM: none
interface Foo
interface Bar : Foo {
@@ -4,7 +4,7 @@ fun maybeFoo(): String? {
fun main(args: Array<String>) {
val foo = maybeFoo()
if (null == foo<caret>)
i<caret>f (null == foo)
null
else
foo.length
@@ -4,7 +4,7 @@ fun maybeFoo(): String? {
fun main(args: Array<String>) {
val foo = maybeFoo()
if (null != foo<caret>)
<caret>if (null != foo)
foo.length
else
null
@@ -1,4 +1,4 @@
// IS_APPLICABLE: false
// PROBLEM: none
fun main(args: Array<String>) {
val foo: String? = "foo"
<caret>if (foo == null) {
@@ -1,4 +1,4 @@
// IS_APPLICABLE: false
// PROBLEM: none
// ERROR: Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type String?
fun main(args: Array<String>) {
val foo: String? = "foo"
@@ -1,4 +1,4 @@
// IS_APPLICABLE: false
// PROBLEM: none
fun main(args: Array<String>) {
val foo: String? = "foo"
if (foo == null<caret>) {
@@ -1,4 +1,4 @@
// IS_APPLICABLE: false
// PROBLEM: none
fun main(args: Array<String>) {
val foo: String? = "foo"
if (foo != null<caret>) {
@@ -1,4 +1,4 @@
// IS_APPLICABLE: false
// PROBLEM: none
fun main(args: Array<String>) {
val foo: String? = "foo"
if (foo == null<caret>) {
@@ -1,4 +1,4 @@
// IS_APPLICABLE: false
// PROBLEM: none
fun main(args: Array<String>) {
val foo: String? = "foo"
if (foo != null<caret>)
@@ -1,6 +1,6 @@
class F(a: Int?) {
val b = a
val c = if (b !=<caret> null) b.toString() else null
val c = i<caret>f (b != null) b.toString() else null
}
fun main(args: Array<String>) {
@@ -1 +0,0 @@
org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions.IfThenToSafeAccessIntention
@@ -1,2 +0,0 @@
// INSPECTION_CLASS: org.jetbrains.kotlin.idea.intentions.branchedTransformations.intentions.IfThenToSafeAccessInspection
// WITH_RUNTIME
@@ -1,5 +0,0 @@
class My(val x: Int)
fun foo(arg: Any?): Int? {
return if (<caret>arg is My) arg.x.hashCode() else null
}
@@ -1 +0,0 @@
fun foo(arg: Any) = if (arg is String<caret>) arg.length else null
@@ -1 +0,0 @@
fun foo(arg: Any) = if (arg !is String<caret>) null else arg.length
@@ -1,3 +0,0 @@
fun foo(arg: Any?): Any? {
return if (<caret>arg != null) arg else null
}