Refactoring of IfThen: ToDoubleBang / ToElvis / ToSafeAccess

Some common actions were extracted to IfThenUtils.kt
Use consistent logic in all three intentions
Also fixes potential PSI consistency problems in conversions dot->safe calls
This commit is contained in:
Mikhail Glukhikh
2017-05-04 15:51:59 +03:00
parent fd6d1520c7
commit 66c5717adc
14 changed files with 230 additions and 213 deletions
@@ -111,4 +111,28 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">If-Then foldable to '?.'</problem_class>
<description>Replace 'if' expression with safe access expression</description>
</problem>
<problem>
<file>nullCheckWithSelectorCallChain.kt</file>
<line>5</line>
<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>
</problem>
<problem>
<file>isCheckWithSelectorChain.kt</file>
<line>4</line>
<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>
</problem>
<problem>
<file>isCheckSimple.kt</file>
<line>4</line>
<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 access expression</description>
</problem>
</problems>
@@ -0,0 +1,5 @@
class My(val x: Int)
fun foo(arg: Any?): My? {
return if (<caret>arg is My) arg else null
}
@@ -0,0 +1,5 @@
class My(val x: Int)
fun foo(arg: Any?): My? {
return arg as? My
}
@@ -0,0 +1,5 @@
class My(val x: Int)
fun foo(arg: Any?): Int? {
return if (<caret>arg is My) arg.x.hashCode() else null
}
@@ -0,0 +1,5 @@
class My(val x: Int)
fun foo(arg: Any?): Int? {
return (arg as? My)?.x?.hashCode()
}
@@ -0,0 +1,5 @@
// IS_APPLICABLE: false
fun foo(arg: Any?): Any? {
return if (<caret>arg != null) arg else null
}
@@ -0,0 +1,9 @@
// WITH_RUNTIME
val nullableString: String? = "abc"
val foo = if (<caret>nullableString != null) {
nullableString.toUpperCase().toLowerCase()
} else {
null
}
@@ -0,0 +1,5 @@
// WITH_RUNTIME
val nullableString: String? = "abc"
val foo = nullableString?.toUpperCase()?.toLowerCase()