Use "if-then to" even for 'if (arg != null) arg' removing 'if'

This commit is contained in:
Mikhail Glukhikh
2017-05-11 19:59:51 +03:00
parent 977d8e1cd7
commit 326d850760
4 changed files with 18 additions and 4 deletions
@@ -41,7 +41,12 @@ class IfThenToSafeAccessIntention : SelfTargetingOffsetIndependentIntention<KtIf
val ifThenToSelectData = element.buildSelectTransformationData() ?: return false
if (!ifThenToSelectData.receiverExpression.isStableVariable(ifThenToSelectData.context)) return false
if (ifThenToSelectData.baseClause !is KtDotQualifiedExpression) {
text = "Replace 'if' expression with safe cast expression"
if (ifThenToSelectData.condition is KtIsExpression) {
text = "Replace 'if' expression with safe cast expression"
}
else {
text = "Remove redundant 'if' expression"
}
}
return ifThenToSelectData.clausesReplaceableBySafeCall()
@@ -66,7 +71,7 @@ class IfThenToSafeAccessIntention : SelfTargetingOffsetIndependentIntention<KtIf
private fun IfThenToSelectData.clausesReplaceableBySafeCall(): Boolean {
if (baseClause == null || negatedClause != null && !negatedClause.isNullExpression()) return false
return baseClause.evaluatesTo(receiverExpression) && condition !is KtBinaryExpression ||
return baseClause.evaluatesTo(receiverExpression) ||
baseClause.hasFirstReceiverOf(receiverExpression) && !baseClause.hasNullableType(context)
}
}
@@ -135,4 +135,12 @@
<problem_class severity="WARNING" attribute_key="WARNING_ATTRIBUTES">If-Then foldable to '?.'</problem_class>
<description>Replace 'if' expression with safe cast expression</description>
</problem>
<problem>
<file>nullCheckSimple.kt</file>
<line>2</line>
<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>
</problem>
</problems>
@@ -1,5 +1,3 @@
// IS_APPLICABLE: false
fun foo(arg: Any?): Any? {
return if (<caret>arg != null) arg else null
}
@@ -0,0 +1,3 @@
fun foo(arg: Any?): Any? {
return arg
}